uml class diagrams - retis labretis.sssup.it/~lipari/courses/cpp09/08.uml_diagrams.pdfuml diagrams...
TRANSCRIPT
![Page 1: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/1.jpg)
UML class diagrams
Giuseppe Liparihttp://retis.sssup.it
Scuola Superiore Sant’Anna – Pisa
June 8, 2009
![Page 2: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/2.jpg)
Using UML
Goal: Be able to “reason about” a designi.e., understand designer’s intentCritique/improve the design
Claim: Source code not best medium for communicationand comprehension
Lots of redundancy and detail irrelevant for someprogram-understanding tasksEspecially poor at depicting relationships among classes inOO programsTo understand an OO design, one must be able to visualizethese relationships
Solution: Use abstract, visual representations - UML
![Page 3: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/3.jpg)
UML diagrams
Collection of notations representing software designs fromthree points of view:
Class model describes the static structure of objects andrelationships in a systemState model describes the dynamics aspects of objects andthe nature of control in a systemInteraction model describes how objects in a systemcooperate to achieve broader results
Generally, we need all three models to describe a system
No single model says everything
Here we focus on class model
![Page 4: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/4.jpg)
Outline
1 UML Class diagram notation
2 Objects
3 Relationships
4 Template notation
![Page 5: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/5.jpg)
UML Class diagram notation
Boxes denote classesEach box comprises:
Class nameList of data attributesList of operations
More compact than codeand more amenable todepicting relationshipamong classes
EmployeefirstName: stringlastName: stringhireDate: Datedepartment: shortprint(os: ostream&): void
Cityname: stringpopulation: unsigned
![Page 6: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/6.jpg)
Abstraction in class diagrams
Class diagrams often elide detailsMethod associated with an operationAttribute and operations may be hidden in diagrams toimprove readability
even if they exist in C++ code
ClassNameattr1: type1 = def1attr2: type2 = def2opName1(arg1: argtype1): restype1opName2(arg2: argtype2): restype2
EmployeefirstName: stringlastName: stringhireDate: Datedepartment: short
Employee
![Page 7: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/7.jpg)
Inheritance
BaseClassattrib1: intabsOp(arg: int): void
DerivedClassattrib2: stringabsOp(arg: int): void
DerivedClass is derivedfrom BaseClass
BaseClass class has avirtual method (in italic)
DerivedClassreimplemented the virtualmethod
![Page 8: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/8.jpg)
Outline
1 UML Class diagram notation
2 Objects
3 Relationships
4 Template notation
![Page 9: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/9.jpg)
Object notation
Notes:The UML symbol for an object is a box with an object namefollowed by a colon and the class name. The object nameand class name are both underlined.Attribute values and the object name are optional.Only list attributes that have intrinsic meaning. Attributes ofcomputer artifacts (such as pointers) should not be listed.
![Page 10: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/10.jpg)
Example
doe: Employee
firstName=”John”lastName=”Doe”hireDate=Sep:21:1998department=225
doe:Employee
:Employee
Employee doe("John","Doe", ...);
Employee * doe =new Employee("John",
"Doe",..);
eList.addEmpl(new Employee("Mary",
"Smith", ...));
![Page 11: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/11.jpg)
A More formal distinction
Value: Primitive “piece of data”E.g., the number 17, the string “Canada”Unlike objects, values lack identity
Object: Meaningful concept or “thing” in an applicationdomain
Often appears as a proper noun or specific reference indiscussions with users.May be attributed with valuesHas identity
Two objects containing the “same values” are not the sameobject!
They are distinct objectsThey may be considered “equivalent” under a certaindefinition of “equality”
![Page 12: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/12.jpg)
What’s the big deal about identity?
Useful in reasoning about “goodness” of a designMany poor designs result from an “encoding” of one objectwithin another, using attribute valuesBy reasoning about identity, one may identify such a designflaw earlyBest illustrated by example
Also allows us to model relationships among objects andclasses more explicitly
![Page 13: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/13.jpg)
Exercise: Travel-planning system
A city has a name, a certain population, and a specific timezone
A city has one or more airports
An airport has a name and a unique code
![Page 14: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/14.jpg)
Exercise: Travel-planning system
A city has a name, a certain population, and a specific timezone
A city has one or more airports
An airport has a name and a unique code
How many classes should you design?
![Page 15: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/15.jpg)
Is this design correct?
Citycity_name: stringpopulation: unsigned inttime_zone: zoneairport_name: stringairport_code: code
These attributes are “hiding” an object (the airport) that ismeaningful by itself in this domain
Why it might be bad to encode one object as a collection ofattribute values within another?
![Page 16: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/16.jpg)
Design tip
Answer:Potential for redundancy/inconsistency due to duplication
some airports serve multiple citiessome cities served by no airportssome cities served by multiple airports
Operations over Airport objects may not need to knowdetails associated with cities, such as population
When designing a class:Apply the identity test to each attribute (including attributesin combination)Never use an attribute to model an “object identifier”
UML notation helps enforce this discipline
So then how do we model connections between objects,such as Cities and Airports?
![Page 17: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/17.jpg)
Outline
1 UML Class diagram notation
2 Objects
3 Relationships
4 Template notation
![Page 18: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/18.jpg)
Relationships among objects
Link: Physical or conceptual connection between objectsMuch more abstract than pointers/referencesMost (not all) links relate exactly two objects
Association: Description of a group of links with commonstructure and semanticsA link is an instance of an association:
Links connect objects of same classesHave similar properties (link attributes)Association describes set of potential links just like a classdescribes a set of potential objects
![Page 19: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/19.jpg)
Examples of links
Houston: City
cityName = “Houston, TX”population = 3000000
HOU: Airport
airportCode = HOUairportName = “Hobby”timeZone = Central
IAH: Airport
airportCode = IAHairportName = “Intercontinental”timeZone = Central
serves
serves
![Page 20: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/20.jpg)
From links to association
CitycityName: stringpopulation: unsigned
AirportairportCode: codeairportName: stringtimeZone: zone
1..* serves
*
![Page 21: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/21.jpg)
Bidirectionality
Links may be navigated in either direction!Benefits:
During early design, it is often difficult to predict thenavigation directions that will be needed
Especially true for many-to-many associationsBetter to model connections as bidirectional associationsand later refine these associations into moreimplementation-level structures (e.g., pointers, vectors ofpointers maps etc)
Often several ways to implement an association and thedetails are not salient to the “essence” of the design
![Page 22: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/22.jpg)
Implementation of “serves” association
class City {...
protected:string cityName;unsigned population;vector<Airport * > serves;
};
class Airport {...
protected:string airportName;CODE airportCode;ZONE timeZone;vector<City * > serves;
};
![Page 23: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/23.jpg)
Implementation of “serves” association
class City {...
protected:string cityName;unsigned population;vector<Airport * > serves;
};
class Airport {...
protected:string airportName;CODE airportCode;ZONE timeZone;vector<City * > serves;
};
class City {...
protected:string cityName;unsigned population;
};
class Airport {...
protected:string airportName;CODE airportCode;ZONE timeZone;
};
multimap<City * , Airport * > cityServes;multimap<Airport * , City * > airportServes;
![Page 24: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/24.jpg)
From UML diagrams to classes
You should get comfortable with the various methods forrefining a UML association
be able to easily switch back and forth between what is saidin the diagram and what is allowable in the codestart to “think” using links/associations rather than pointersand references
This is good training in abstraction
![Page 25: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/25.jpg)
Outline
1 UML Class diagram notation
2 Objects
3 Relationships
4 Template notation
![Page 26: UML class diagrams - ReTiS Labretis.sssup.it/~lipari/courses/cpp09/08.UML_diagrams.pdfUML diagrams Collection of notations representing software designs from three points of view:](https://reader030.vdocument.in/reader030/viewer/2022040602/5e975325f0feb3552e2354b5/html5/thumbnails/26.jpg)
Template notation
MyClassvar: Tnumber: intoperator[](index: int): T
T
Equivalent to:
template<class T>class MyClass {
T var;int number;
public:...T operator[]( int index);
};