Sunteți pe pagina 1din 16
Pattern-Oriented Software Design Introduction to UML Class Diagrams CSIE Department, NTUT Woei-Kae Chen UML: Unified

Pattern-Oriented Software Design

Introduction to UML Class Diagrams

CSIE Department, NTUT Woei-Kae Chen

to UML Class Diagrams CSIE Department, NTUT Woei-Kae Chen UML: Unified Modeling Language Successor to OOA&D
to UML Class Diagrams CSIE Department, NTUT Woei-Kae Chen UML: Unified Modeling Language Successor to OOA&D

UML: Unified Modeling Language

Successor to OOA&D methods

late 1980s and early 1990s

Successor to OOA&D methods late 1980s and early 1990s GoF Book Unifies Jacobson & OMT (Booch

GoF Book

Unifies

Jacobson & OMT (Booch & Rumbaugh)

Graphical notation used to express designs

Use cases Class diagrams Interaction diagrams

Sequence diagrams Collaboration diagrams

Package diagrams State diagrams Activity diagrams Deployment diagrams

Sequence diagrams Collaboration diagrams Package diagrams State diagrams Activity diagrams Deployment diagrams 1
UML class diagrams Three perspectives Conceptual represents of the domain under study relate to the
UML class diagrams
Three perspectives
Conceptual
represents of the domain under study
relate to the class that implement them, but often
no direct mapping
Specification
looking at types rather than classes
a type represents an interface that may have
different implementations
Implementation
looking at classes
for our POSD class
UML: a class
+
public
#
protected
-
private
Abstract
Concrete
•data type
•parameter
Example: OBSort1.cpp Example: OBSort1.cpp Let’s think of main() as a class relationship
Example: OBSort1.cpp
Example: OBSort1.cpp
Let’s think of
main() as a class
relationship
(knows a) (uses a) (has a) (has a) (is a) (parameterized class) UML: class relationship

(knows a)

(uses a)

(has a)

(has a)

(is a)

(parameterized

class)

UML: class relationship

Association Dependency Composition Aggregation

X

Inheritance

Class template

Y

Y

X

X Y
X
Y
X Y
X
Y
Y X
Y
X
Y X
Y
X
X Inheritance Class template Y Y X X Y X Y Y X Y X “Uses
X Inheritance Class template Y Y X X Y X Y Y X Y X “Uses
“Uses a” “Knows a” relationship “ Uses a ” Dependency One object issues a function

“Uses a” “Knows a” relationship

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

X Y

Y

X

X Y

Y

One object is aware of another; it contains a pointer or reference to another object X
One object is aware of another; it contains a pointer or reference to another object X
 

“Is a” “Has a” relationship

“Is a” “Has a” relationship

Is a” relationships

X Y
X
Y
 

Inheritance A class is derived from another class

Has a” relationships

Composition or Aggregation A class contains other classes as members

X Y Y
X
Y
Y
X
X
X Y Y X
 
 
 

Aggregation Composition

X
X
Y X
Y
X
Y
Y
Aggregation Composition X Y X Y

Both are “Has a” or “part-of” relationship Composition

 

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 part

delete whole delete part Aggregation Cascading delete is often

Aggregation

Cascading delete is often

 

Following Larman OOAD:

use of aggregation is NOT recommended

An aggregated instance can be shared

 
 
Example: “has a” relationship Delete Polygon delete Point Delete Polygon X delete Style a Point
Example: “has a” relationship
Delete Polygon delete Point
Delete Polygon
X delete Style
a Point may appear in only
one Polygon or Circle
Larman: use
association instead
of aggregation
Multiplicity
a Style may be shared by
many Polygons and Circles
 

Relationship Examples

  Relationship Examples

Car Engine ? Person Cell Phone ? Human Brain ? Fighter Bomb ? Fighter F16 Bomb Explosive Bomb Nuclear Bomb MyComplex Math ? Tree Node Child Node ? Tree Node Parent Node ? Hero Life ? Hero Score ? Hero Map ?

Composition

Association/Composition/Dependency

Composition

Association

Inheritance

Composition/Association/Inheritance

Inheritance Dependency Composition Association (if needed) Composition/Attribute Association/Dependency Association/Dependency

 

 
 
Relationship Examples Flight 123 Airplane ? Flight 123 Airport ? Flight 123 Passenger ? Flight

Relationship Examples

Flight 123 Airplane ? Flight 123 Airport ? Flight 123 Passenger ? Flight 123 Flight Captain ? Flight 123 Flight Attendant ? Airplane Boeing 747 ? Airplane Seat ? Airplane Fuel ? Passenger Flight ? Passenger Ticket ? Passenger Travel Agent ? Ticket Price ?

Association/Dependency Association Association/Dependency Association/Dependency Association/Dependency Inheritance Composition Composition/Attribute Association/Dependency Association/Dependency Association/Dependency Composition/Attribute

Association/Dependency Composition/Attribute UML Example (C++): Association X Y class X { X(Y
UML Example (C++): Association X Y class X { X(Y *y) : y_ptr(y) {} void

UML Example (C++): Association

X

X Y

Y

class X { X(Y *y) : y_ptr(y) {} void SetY(Y *y) {y_ptr = y;} void f() {y_ptr->Foo();}

Y *y_ptr; // pointer

};

class X { X(Y *y) : y_ptr(y) {} void SetY(Y * y) {y_ptr = y;} void
class X { X(Y *y) : y_ptr(y) {} void SetY(Y * y) {y_ptr = y;} void
UML Example (C++): Association X Y How is an as sociation created? Example #1 Example

UML Example (C++): Association

X

X Y

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();

X an_x(&an_y); X an_x(); an_x.f(); … … an_x.SetY(&y); … … an_x.f();
X an_x(&an_y); X an_x(); an_x.f(); … … an_x.SetY(&y); … … an_x.f();

X

X Y

Y

X Y class X { void f1(Y y) {…; y.Foo();} void f2(Y *y) {…; y->Foo();} void

class X {

void f1(Y y) {…; y.Foo();} void f2(Y *y) {…; y->Foo();} void f3(Y &y) {…; y.Foo();} void f4() {Y y; y.Foo();…} void f5() {…; Y::StaticFoo();}

};

UML Example (C++): Dependency

&y ) {…; y.Foo();} void f4() {Y y; y.Foo();…} void f5() {…; Y::StaticFoo();} }; UML Example
&y ) {…; y.Foo();} void f4() {Y y; y.Foo();…} void f5() {…; Y::StaticFoo();} }; UML Example
Example: OBSort3.cpp uses •getSize() •operator[]
Example: OBSort3.cpp
uses
•getSize()
•operator[]

X

X Y

Y

X Y class X { Java? Y a ; / / 1; Composition Y b[10]; //

class X {

Java?

Y

a;

// 1; Composition

Y

b[10];

// 0

10;

Composition

};

UML Example (C++): Composition 1

a ; / / 1; Composition Y b[10]; // 0 10; Composition }; UML Example (C++)
a ; / / 1; Composition Y b[10]; // 0 10; Composition }; UML Example (C++)

X

X Y

Y

X Y class X { X() { a = new Y[10]; } ~X(){ delete [] a;

class X { X() { a = new Y[10]; } ~X(){ delete [] a; }

};

Y *a;

X Y class X { X() { a = new Y[10]; } ~X(){ delete [] a;

// 0

NOT Association

10;

Composition

UML Example (C++): Composition 2

delete [] a; } }; Y * a ; / / 0 NOT Association 10; Composition
delete [] a; } }; Y * a ; / / 0 NOT Association 10; Composition
UML Example (C++): Composition 3 X vector<Y> Y Implementation detail X Y class X {
UML Example (C++): Composition 3
X
vector<Y>
Y
Implementation detail
X
Y
class X {
Hiding
implementation detail
vector<Y> a;
};
Composition of
NOT Composition
vector<Y>
of Y
UML Example: OBSort3.cpp
UML Example: OBSort3.cpp

No example here

No example here Use Association instead of Aggregation X Y X Y

Use Association instead of Aggregation

X

X Y

Y

X Y
X
Y

UML Example (C++): Aggregation

No example here Use Association instead of Aggregation X Y X Y UML Example (C++): Aggregation
No example here Use Association instead of Aggregation X Y X Y UML Example (C++): Aggregation
UML Example (C++): Inheritance class Y { Y }; class X : public Y {
UML Example (C++): Inheritance
class Y {
Y
};
class X : public Y {
X
};
“is a” relationship
Example: OOSort2.cpp
Example: OOSort2.cpp
UML Example (C#): Implementation public interface Y { int Foo(); } No fields Y class
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
UML Example (C++): Implementation class Y { Y }; No variables Only pure virtual functions
UML Example (C++): Implementation
class Y {
Y
};
No variables
Only pure virtual functions
class X : public Y {
X
};
C++ allows multiple inheritance
UML Example (C++): Template Class template <class T> class X { Y X X<Y> a;
UML Example (C++):
Template Class
template <class T>
class X {
Y
X
X<Y> a;
};
UML Example (C++): Template Class template <class T> class X { Y X X<Y> a; };
Abstract class 15
Abstract class
Abstract class
C++ static member
C++ static member