uml class diagrams - retis lab
TRANSCRIPT
![Page 1: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/1.jpg)
UML class diagrams
Giuseppe Liparihttp://retis.sssup.it
Scuola Superiore Sant’Anna – Pisa
March 13, 2011
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 1 / 31
![Page 2: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/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 communication andcomprehension
Lots of redundancy and detail irrelevant for someprogram-understanding tasksEspecially poor at depicting relationships among classes in OOprogramsTo understand an OO design, one must be able to visualize theserelationships
Solution: Use abstract, visual representations - UML
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 2 / 31
![Page 3: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/3.jpg)
UML diagrams
Collection of notations representing software designs from threepoints of view:
Class model describes the static structure of objects andrelationships in a systemState model describes the dynamics aspects of objects and thenature of control in a systemInteraction model describes how objects in a system cooperate toachieve broader results
Generally, we need all three models to describe a system
No single model says everything
Here we focus on class model
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 3 / 31
![Page 4: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/4.jpg)
Outline
1 UML Class diagram notation
2 Objects
3 Association, Aggregation, Composition
4 Template notation
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 4 / 31
![Page 5: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/5.jpg)
UML Class diagram notation
Boxes denote classesEach box comprises:
Class nameList of data attributesList of operations
More compact than code andmore amenable to depictingrelationship among classes
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 5 / 31
![Page 6: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/6.jpg)
Notation
Visibility:
- is private
# is protected
+ is public
Specification
member type follows definition
parameter type follow name
optionally, can specify parameterdirection (in/out)
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 6 / 31
![Page 7: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/7.jpg)
Abstraction in class diagrams
Class diagrams often elide detailsMethod associated with anoperationAttribute and operations may behidden in diagrams to improvereadability
even if they exist in C++ code
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 7 / 31
![Page 8: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/8.jpg)
Relationships between classes
Two classes can be related by:InheritanceAssociationAggregationComposition
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 8 / 31
![Page 9: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/9.jpg)
Inheritance
DerivedClass is derivedfrom BaseClass
BaseClass class has anabstract method (in italic)
operation() is a pure virtualmethod
DerivedClass implementsthe virtual method
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 9 / 31
![Page 10: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/10.jpg)
Outline
1 UML Class diagram notation
2 Objects
3 Association, Aggregation, Composition
4 Template notation
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 10 / 31
![Page 11: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/11.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 name and classname 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.
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 11 / 31
![Page 12: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/12.jpg)
Example
We can also remove themember values, and even theobject name
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 12 / 31
![Page 13: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/13.jpg)
Associations - II
A Link is represented as a line connecting two or more objectboxes
It can be shown on an object diagram or class diagram.
A link is an instance of an association
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 13 / 31
![Page 14: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/14.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 application domainOften appears as a proper noun or specific reference in discussionswith users.May be attributed with valuesHas identity
Two objects containing the “same values” are not the same object!
They are distinct objectsThey may be considered “equivalent” under a certain definition of“equality”
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 14 / 31
![Page 15: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/15.jpg)
What’s the big deal about identity?
Useful in reasoning about “goodness” of a designMany poor designs result from an “encoding” of one object withinanother, using attribute valuesBy reasoning about identity, one may identify such a design flawearlyBest illustrated by example
Also allows us to model relationships among objects and classesmore explicitly
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 15 / 31
![Page 16: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/16.jpg)
Exercise: Travel-planning system
A city has a name, a certain population, and a specific time zone
A city has one or more airports
An airport has a name and a unique code
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 16 / 31
![Page 17: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/17.jpg)
Exercise: Travel-planning system
A city has a name, a certain population, and a specific time zone
A city has one or more airports
An airport has a name and a unique code
How many classes should you design?
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 16 / 31
![Page 18: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/18.jpg)
Is this design correct?
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?
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 17 / 31
![Page 19: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/19.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 know detailsassociated with cities, such as population
When designing a class:Apply the identity test to each attribute (including attributes incombination)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 asCities and Airports?
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 18 / 31
![Page 20: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/20.jpg)
Outline
1 UML Class diagram notation
2 Objects
3 Association, Aggregation, Composition
4 Template notation
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 19 / 31
![Page 21: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/21.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
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 20 / 31
![Page 22: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/22.jpg)
Examples of links
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 21 / 31
![Page 23: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/23.jpg)
From links to association
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 22 / 31
![Page 24: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/24.jpg)
Association
Association represents the ability of one instance to send amessage to another instanceThis is typically implemented with a pointer or reference instancevariable, although it might also be implemented as a methodargument, or the creation of a local variable.
class Researcher {vector<Paper *> paper_list;...
};class Paper {
vector<Researcher *> author_list;...
};
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 23 / 31
![Page 25: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/25.jpg)
Directional association
Association can be directed if the link only goes in one direction
class Abc {private:
Cde *c;public:
...};
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 24 / 31
![Page 26: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/26.jpg)
Bidirectionality
During early design, it is often difficult to predict the navigationdirections that will be needed
Especially true for many-to-many associationsBetter to model connections as bidirectional associations and laterrefine these associations into more implementation-level structures(e.g., pointers, vectors of pointers, maps, etc.)
Often several ways to implement an association and the detailsare not salient to the “essence” of the design
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 25 / 31
![Page 27: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/27.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;
};
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 26 / 31
![Page 28: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/28.jpg)
Alternative implementation of “serves” association
class City {...
protected:string cityName;unsigned population;
};
class Airport {...
protected:string airportName;CODE airportCode;ZONE timeZone;
};
multimap<City*, Airport*> cityServes;multimap<Airport*, City*> airportServes;
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 27 / 31
![Page 29: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/29.jpg)
Aggregation
Association is a general relationship
When we have a “whole/part” relationship, we can use a specialkind of association, called “aggregation”
instances cannot have cyclic aggregation relationships (i.e. a partcannot contain its whole)
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 28 / 31
![Page 30: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/30.jpg)
Composition
Composition is the same as aggregation, but in addition the“whole” controls the “part”
class Car {public:virtual ~Car() {delete itsCarb;}
private:Carburetor* itsCarb
};
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 29 / 31
![Page 31: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/31.jpg)
Outline
1 UML Class diagram notation
2 Objects
3 Association, Aggregation, Composition
4 Template notation
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 30 / 31
![Page 32: UML class diagrams - ReTiS Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081622/613d2d52736caf36b75a3ad9/html5/thumbnails/32.jpg)
Template notation
Equivalent to:
template<class T>class MyClass {
T var;int number;
public:...T operator[](int index);
};
Giuseppe Lipari (Scuola Superiore Sant’Anna) UML class diagrams March 13, 2011 31 / 31