object-oriented programming introduction to uml class...
TRANSCRIPT
ObjectObject--Oriented Programming Oriented Programming Introduction toIntroduction to
UML Class DiagramsUML Class Diagrams
CSIE Department, NTUT
Woei-Kae Chen
UML: UML: Unified Modeling LanguageUnified Modeling LanguageSuccessor to OOA&D methods– late 1980s and early 1990s
Unifies– Jacobson & OMT (Booch & Rumbaugh)
Graphical notation used to express designs– Use cases– Class diagrams– Interaction diagrams
Sequence diagramsCollaboration diagrams
– Package diagrams– State diagrams– Activity diagrams– Deployment diagrams
GoF Book
UML class diagramsUML class diagramsThree perspectives– Conceptual
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 classUML: a class
function1()function2()
variable1variable2
class_name+ public# protected- private
AbstractConcrete
•data type•parameter
Example: OBSort1Example: OBSort1.cpp.cpp
+getInput()+printOutput()+Sort()+cleanUp()+getSize()
-a-size
IntArray
Example: OBSort1Example: OBSort1.cpp.cpp
+getInput()+printOutput()+Sort()+cleanUp()+getSize()
-a-size
IntArray
Context(main)
Let’s assume main() is a class
relationship
UML: class relationshipUML: class relationshipAssociation (knows a)Dependency (uses a)Composition (has a)Aggregation (has a)
Inheritance (is a)
Class template XY
X Y
X Y
X Y
X Y
Y
X
(parameterizedclass)
““Uses aUses a”” ““Knows aKnows a”” relationshiprelationship
“Uses a”– Dependency– One object issues a function
call to a member function of another object
“Knows a”– Association– One object is aware of
another; it contains a pointer or reference to another object
X Y
X Y
““Is aIs a”” ““Has aHas a”” relationshiprelationship“Is a” relationships– Inheritance– a class is derived from
another class
“Has a” relationships– Composition or Aggregation– a class contains other
classes as members
X
Y
X Y
X Y
Aggregation Aggregation CompositionComposition
Both are “Has a” or “part-of” relationshipComposition– a stronger variety of aggregation– the part object may belong to only one whole– expected to live and die with the whole
delete whole delete partAggregation– cascading delete is often– an aggregated instance can be shared
X Y X Y
Example: Example: ““has ahas a”” relationshiprelationship
colorisFilled
Style
Polygon
13..*
Point
*
1
{ordered}
radiusCircle
1
1
*1
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++): AssociationUML 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; // pointerY &y_ref; // reference
};
UML Example (C++): DependencyUML Example (C++): Dependency
X Y
class X {
...
void f1(Y y) {...; y.doIt();}
void f2(Y *y_ptr);
void f3(Y &y_ref);
};
Example: OBSort3Example: OBSort3.cpp.cpp
+getInput()+printOutput()+cleanUp()+getSize()+operator[]()
-a-size
IntArray
+sort()
Sorter
uses•getSize()•operator[]
UML Example (C++)UML Example (C++): Composition 1: Composition 1
X Y
class X {
...
Y a; // 1; Composition
Y b[10]; // 0..10; Composition
vector<Y> c; // ??
};
Java?
Composition of vector<Y>
NOT Composition of Y
UML Example (C++):UML Example (C++): Composition 2Composition 2
X Y
class X {X() { a = new Y[10]; }~X(){ delete [] a; }
...Y *a; // 0..10; Composition
};NOT Association
UML Example: OBSort3UML Example: OBSort3.cpp.cpp
+getInput()+printOutput()+cleanUp()+getSize()+operator[]()
-a-size
IntArray
+sort()
SorterContext(main)
UML Example (C++):UML Example (C++): Aggregation 1Aggregation 1
X Y
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};
The same as composition?
May be considered as aggregation of Y
UML Example (C++):UML Example (C++): Aggregation 2Aggregation 2
X vector<Y> Y
class X {...vector<Y> b;
};
X Y
Hiding implementation detail
Implementation detail
UML Example (C++): InheritanceUML Example (C++): Inheritance
class Y {...};
class X : public Y {...};
X
Y
“is a” relationship
Example: OOSort2Example: OOSort2.cpp.cpp
+getInput()+printOutput()+cleanUp()+getSize()+operator[]()
-a-size
IntArray
+sort()
SorterContext(main)
+sort()
-compareCount-exchangeCount
CountingSorter
UML Example (C++):UML Example (C++):Template ClassTemplate Class
template <class T>class X {.........};
XY
...X<Y> a;...
Abstract class
C++ static member