Documente Academic
Documente Profesional
Documente Cultură
Introduction to
UML Class Diagrams
1
UML class diagrams
z Three perspectives
z Conceptual
z represents of the domain under study
z relate to the class that implement them, but often
no direct mapping
z Specification
z looking at types rather than classes
z a type represents an interface that may have
different implementations
z Implementation
z looking at classes for our POSD class
UML: a class
+ public
# protected
- private
Abstract
Concrete
•data type
•parameter
2
Example: OBSort1.cpp
Example: OBSort1.cpp
Let’s think of
main() as a class
relationship
3
UML: class relationship
z Association X Y (knows a)
z Dependency X Y (uses a)
z Composition X Y (has a)
z Aggregation (has a)
X Y
z Inheritance Y (is a)
X
z Class template Y (parameterized
X class)
z “Uses a”
z Dependency
z One object issues a function X Y
call to a member function of
another object
z “Knows a”
z Association
z One object is aware of
another; it contains a pointer X Y
or reference to another object
4
“Is a” Ù “Has a” relationship
z “Is a” relationships X
z Inheritance
z A class is derived from
another class Y
z “Has a” relationships
z Composition or Aggregation X Y
z A class contains other
classes as members
X Y
Aggregation Ù Composition
X Y X Y
z Both are “Has a” or “part-of” relationship
z Composition
z A stronger variety of aggregation
z The part object may belong to only one
whole
z Expected to live and die with the whole
z delete whole Æ delete part Following Larman OOAD:
z Aggregation use of aggregation is NOT
z Cascading delete is often recommended
z An aggregated instance can be shared
5
Example: “has a” relationship
Delete Polygon Æ delete Point a Point may appear in only
Delete Polygon Æ
X delete Style one Polygon or Circle
Larman: use
association instead
of aggregation
Relationship Examples
z Car Ù Engine ? Æ Composition
z Person Ù Cell Phone ? Æ Association/Composition/Dependency
z Human Ù Brain ? Æ Composition
z Fighter Ù Bomb ? Æ Association
z Fighter Ù F16 Æ Inheritance
z Bomb Ù Explosive Æ Composition/Association/Inheritance
z Bomb Ù Nuclear Bomb Æ Inheritance
z MyComplex Ù Math ? Æ Dependency
z Tree Node Ù Child Node ? Æ Composition
z Tree Node Ù Parent Node ? Æ Association (if needed)
z Hero Ù Life ? Æ Composition/Attribute
z Hero Ù Score ? Æ Association/Dependency
z Hero Ù Map ? Æ Association/Dependency
6
Relationship Examples
z Flight 123 Ù Airplane ? Æ Association/Dependency
z Flight 123 Ù Airport ? Æ Association
z Flight 123 Ù Passenger ? Æ Association/Dependency
z Flight 123 Ù Flight Captain ? Æ Association/Dependency
z Flight 123 Ù Flight Attendant ? Æ Association/Dependency
z Airplane Ù Boeing 747 ? Æ Inheritance
z Airplane Ù Seat ? Æ Composition
z Airplane Ù Fuel ? Æ Composition/Attribute
z Passenger Ù Flight ? Æ Association/Dependency
z Passenger Ù Ticket ? Æ Association/Dependency
z Passenger Ù Travel Agent ? Æ Association/Dependency
z Ticket Ù Price ? Æ Composition/Attribute
7
UML Example (C++): Association
X Y
How is an association created?
Example #1 Example #2
… …
Y an_y(); Y an_y();
X an_x(&an_y); X an_x();
an_x.f(); …
… an_x.SetY(&y);
… …
an_x.f();
8
Example: OBSort3.cpp
uses
•getSize()
•operator[]
X Y
class X {
Java?
...
Y a; // 1; Composition
Y b[10]; // 0..10; Composition
};
9
UML Example (C++): Composition 2
X Y
class X {
X() { a = new Y[10]; }
~X(){ delete [] a; }
...
Y *a; // 0..10; Composition
};
NOT Association
X Y
class X {
Hiding
... implementation detail
vector<Y> a;
}; Composition of NOT Composition
vector<Y> of Y
10
UML Example: OBSort3.cpp
z No example here
z Use Association instead of Aggregation
X Y
X Y
11
UML Example (C++): Inheritance
class Y {
... Y
};
class X : public Y {
... X
};
“is a” relationship
Example: OOSort2.cpp
12
UML Example (C#): Implementation
public interface Y {
int Foo();
}
No fields
Y
class X : Y
{ X implements Y
public int Foo()
{ X
…
}
} “Can do” relationship
class Y {
... Y
No variables
}; Only pure virtual functions
class X : public Y {
... X
};
C++ allows multiple inheritance
13
UML Example (C++):
Template Class
14
Abstract class
15
C++ static member
16