Download presentation
Presentation is loading. Please wait.
Published byBuck Marshall Modified over 6 years ago
1
CompSci 280 S2 2107 Introduction to Software Development
Class Diagrams
2
Today’s Agenda Topics: Reading: Introduction Class Diagrams
Relationships Associations Generalization Aggregation Class Diagram Examples How to build a class diagram Reading: Software Engineering 10th Edition, Ian Somerville Chapter 5 Fowler, Martin, UML distilled : a brief guide to the standard object modeling language, Third Edition, Boston : Addison-Wesley, c2004 – Chapter 9 Lecture08
3
1.Introduction Structural models
Structural models of software display the organization of a system in terms of the components that make up that system and their relationships. Structural models may be static models, which show the structure of the system design. Class diagrams are used when developing an object-oriented system model to show the classes in a system and the associations between these classes. An object class can be thought of as a general definition of one kind of system object. An association is a link between classes that indicates that there is some relationship between these classes. Lecture08
4
2.Class diagrams Steps: Identifying Classes Specifying Attributes
Identify classes by examining the problem statement or by performing a “Grammatical Parse” on the use-cases UML class notation is a rectangle divided into three parts: class name, attributes, and operations. write <<interface>> on top of interfaces' names use italics for an abstract class name Specifying Attributes should include all fields of the object Defining operations Super … Generalisation Attribute1: type Sub1 Operation1() Operation2(): type Sub2 Pieces 1 0..* Associated Association Aggregation Lecture08
5
2.Class diagrams Classes
A description of a group of objects all with similar roles in the system Structural features define what objects of the class know Behavioral features define what objects of the class can do Identifying object classes is often a difficult part of object oriented design. There is no 'magic formula' for object identification. It relies on the skill, experience and domain knowledge of system designers. Object identification is an iterative process. You are unlikely to get it right first time. Approaches Use a grammatical approach based on a natural language description of the system. Base the identification on tangible things in the application domain. Use a behavioural approach and identify objects based on what participates in what behaviour. Use a scenario-based analysis. The objects, attributes and methods in each scenario are identified. Lecture08
6
2.Class diagrams Specifying Attributes
Attributes (fields/instance variables) are the set of data objects that fully define the class within the context of the problem. i.e. represent the state of an object of the class To develop attributes for class, can study use-case and select those “things” that reasonably “Belong” to the class. Static members are underlined. Instance members are not. Access specifiers appear in front of each member. Private (-): can be used only by the owning class Protected (#): can be used by the owing class and descendents of the class Public (+): can be used by any other class Package/default (~) Default value: the value for a newly created object if the attribute isn't specified during creation #ID: int #Name: String #Password: String Person <access specifier> <name> : <type> <multiplicity> = <default> Lecture08
7
2.Class diagrams Defining operations
Operations define the behavior of an object. Operations are descriptions of behavioral or dynamic features of a class Four broad categories: Operations that manipulate data in some way, perform a computation, inquire about the state of an object, monitor an object for the occurrence of the controlling event. To derive a set of operations, study a use-case( or narrative) and select those operations that reasonably belongs. The parameter list shows each parameter type preceded by a colon. Access specifiers appear in front of each member. Return-type: the type of the returned value <access specifier> <name> ( <parameter list>) : <return type> -date: String -returned: boolean … Rental +rent(customerID, videoID): boolean +return(customerID, videoID): boolean Lecture08
8
2.Class diagrams Examples
Lecture08
9
3.Relationship: Association
A relationship between instances of the two classes. There is an association between two classes if an instance of one class must know about the other in order to perform its work. e.g. Passing messages and receiving responses In a diagram, an association is a solid line connecting two classes. A navigability arrow on an association shows which direction the association can be traversed or queried. Multiplicity of Associations Specifies how many instances of one class may relate to a single association Lecture08
10
3.Association Multiplicity
Multiplicity specifies how many objects an instance of the class at the other end of the association can be linked to. 1..*: 1-to-many e.g A RectangleList has many rectangles 1-to-1 e.g. each student must carry exactly one ID card 0..1 zero or one 0..* / * Zero or more 1 exactly one 1..* One or more 2..4 Specified range Lecture08
11
3.Association Connecting the Classes: Associations
Note: Connect classes with a line Specify multiplicities on each side of association: min..max (min and max of connected instances on each side) Use roles to make the model more self-explanatory A lecturer teaches an arbitrary number of courses (possibly none) 1..* 0..* 0..* 1 coordinator participant A course is taken by one or more students (the participants) A student can take an arbitrarily number of courses (possibly none) A course has exactly one coordinator Lecture08
12
3.Association Classes and associations in the MHC-PMS
Lecture08
13
4.Relationships: Generalization
Generalization: an inheritance link indicating one class is a superclass (the more general) of the child class/subclass (the more specific element) i.e. is-a relationship An inheritance link indicating one class is a superclass of the other. A generalization has a triangle pointing to the superclass. Checking Generalizations If class A is a generalization of a class B, then “Every B is an A” Lecture08
14
4.Generalization Reusing Classes
“Is-a”-relation (inheritance) Lecture08
15
4.Generalization A generalization hierarchy
Lecture08
16
4.Generalization Generalization
In modeling systems, it is often useful to examine the classes in a system to see if there is scope for generalization. If changes are proposed, then you do not have to look at all classes in the system to see if they are affected by the change. In object-oriented languages, such as Java, generalization is implemented using the class inheritance mechanisms built into the language. In a generalization, the attributes and operations associated with higher-level classes are also associated with the lower-level classes. The lower-level classes are subclasses inherit the attributes and operations from their superclasses. These lower-level classes then add more specific attributes and operations. Advantages: Treating similar objects similarly Reuse of commonalities of classes Better maintenance Lecture08
17
5.Relationships: Aggregation
An aggregation model shows how classes that are collections are composed of other classes. Aggregation models are similar to the part-of relationship in semantic data models. (i.e. has-a relationship) An aggregation has a diamond end pointing to the part containing the whole. Normal Aggregation: Represented by a line with a hollow diamond Composition (strong aggregation), a Part cannot exist once Whole is destroyed Represented by a line with a filled black diamond Lecture08
18
5.Aggregation Example Composition Vs Aggregation
If we remove the MovieTheater, we also move the box office (composition), but movies may still exist (aggregation) Lecture08
19
6.Class Diagram Example A customer can have any number of orders
-date: String -status: String Order +calcTotal() +calcTotalWeight -address: String -phone: String Customer 1 0..* An order has a collection of OrderDetails An order belongs to one customer 1 Aggregation 1..* -quantity: int -taxStatus: String OrderDetail +calcSubTotal() +calcWeight() -shippingWeight: double -description: String … Item +getWeight() +getPriceForQuantity 1 0..* An OrderDetail can be queried about its Item, but not the other way around. Lecture08
20
6.Class Diagram Example Lecture08
21
7.How to build a class diagram
Design is driven by criterion of completeness either of data or responsibility Data Driven Design identifies all the data and see it is covered by some collection of objects of the classes of the system Responsibility Driven Design identifies all the responsibilities of the system and see they are covered by a collection of objects of the classes of the system Noun identification Identify noun phrases: look at the use cases and identify a noun phrase. Do this systematically and do not eliminate possibilities Eliminate inappropriate candidates: those which are redundant, vague, outside system scope, an attribute of the system, etc. Validate the model… Lecture08
22
Exercise: ATM Machine Identifying Classes
So far: we know basic requirements for system. Now want to specify basics of how system works: identify the data & functions for a system group these into appropriate objects determine object inter-relationships: both “data” relationships & “behavioural” relationships In full system development, also develop set of “non-functional requirements” Steps: work from data/functions in English requirements or use case definitions group them into objects (often takes several goes!!!) Common approach: nouns = attributes/objects, verbs = functions... I find it easiest to work from data/functions in English requirements or use case definitions group them into objects (often takes several goes!!!) find focusing on data aspects of objects (“entities”) then adding functions works best Common approach: nouns = attributes/objects, verbs = functions... Lecture08
23
Step 1: Identify the data & functions for a system from use cases:
Grouped closely-related attributes -> entities Grouped functions by data they act on Complete the following: Tips: DON’T get carried away with objects for user/device interfaces, databases, data structures, etc. - these are DESIGN, not analysis We want to capture the “essence” of the system i.e. important data/functions Customer Account ATM Transaction Bank Lecture08
24
Step 2: Determine inter-object relationships:
Draw UML Class Diagrams for system structure Tips: Now: how are the objects related??? Three main ways: 1. generalisation e.g. staff -> person 2. association e.g. customer -> account 3. aggregation e.g. account -> transaction Lecture08
25
Step 3: Multiplicity Association #1 (“data”):
customer has 1 or 2 accounts account has bunch of transactions each transaction is made from 1 account Lecture08
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.