object-oriented programming introduction to uml …yccheng/oop2006f/uml-lecture04.pdf ·...
TRANSCRIPT
Object-Oriented Programming Introduction toUML Class Diagrams
CSIE Department, NTUT
Woei-Kae Chen
UML: Unified Modeling LanguageSuccessor to OOA&D methods
late 1980s and early 1990sUnifies
Jacobson & OMT (Booch & Rumbaugh)Graphical notation used to express designs
Use casesClass diagramsInteraction diagrams
Sequence diagramsCollaboration diagrams
Package diagramsState diagramsActivity diagramsDeployment diagrams
GoF Book
UML class diagrams
Three perspectivesConceptual
represents of the domain under studyrelate to the class that implement them, but often no direct mapping
Specificationlooking at types rather than classesa type represents an interface that may have different implementations
Implementationlooking at classes for our OOP class
UML: a class
+ public# protected- private
AbstractConcrete
•data type•parameter
Example: OBSort1.cpp
Example: OBSort1.cpp
Let’s assume main() is a class
relationship
UML: class relationshipAssociation (knows a)Dependency (uses a)Composition (has a)Aggregation (has a)
Inheritance (is a)
Class templateXY
X Y
X Y
X Y
X Y
Y
X
(parameterizedclass)
“Uses a” “Knows a” relationship
“Uses a”DependencyOne object issues a function call to a member function of another object
“Knows a”AssociationOne object is aware of another; it contains a pointer or reference to another object
X Y
X Y
“Is a” “Has a” relationship
“Is a” relationshipsInheritancea class is derived from another class
“Has a” relationshipsComposition or Aggregationa class contains other classes as members
X
Y
X Y
X Y
Aggregation Composition
Both are “Has a” or “part-of” relationshipComposition
a stronger variety of aggregationthe part object may belong to only one wholeexpected to live and die with the whole
delete whole delete partAggregation
cascading delete is oftenan aggregated instance can be shared
X Y X Y
Example: “has a” relationship
Multiplicity
a Point may appear in only one Polygon or Circle
a Style may be shared by many Polygons and Circles
Delete Polygon delete PointDelete Polygon delete StyleX
UML Example (C++): AssociationX Y
class X {
X(Y &y) : y_ref(y) {}
void SetY(Y *y) {y_ptr = y;}
void f() {y_ptr->doIt();}
...
Y *y_ptr; // pointer
Y &y_ref; // reference
};
UML Example (C++): Dependency
X Y
class X {
...
void f1(Y y) {...; y.doIt();}
void f2(Y *y_ptr);
void f3(Y &y_ref);
};
Example: OBSort3.cpp
uses•getSize()•operator[]
UML Example (C++): Composition 1
class X {
...
Y a; // 1; Composition
Y b[10]; // 0..10; Composition
vector<Y> c; // ??
};
X Y
Composition of vector<Y>
NOT Composition of Y
Java?
UML Example (C++): Composition 2
class X {
X() { a = new Y[10]; }
~X(){ delete [] a; }
...
Y *a; // 0..10; Composition
};NOT Association
X Y
UML Example: OBSort3.cpp
UML Example (C++): Aggregation 1
class X {X() { a = new Y[10]; }~X(){ delete [] a; }
...Y *a; // 0..n; Aggregationvector<Y> b;// Y’s are instantiated
// and destroyed by X};
X Y
May be considered as aggregation of Y
The same as composition?
UML Example (C++): Aggregation 2
class X {...vector<Y> b;
};
X vector<Y> Y
X Y
Hiding implementation detail
Implementation detail
UML Example (C++): Inheritance
class Y {...};
class X : public Y {...};
X
Y
“is a” relationship
Example: OOSort2.cpp
UML Example (C++):Template Class
template <class T>
class X {
...
...
...
};
XY
...
X<Y> a;
...
Abstract class
C++ static member