You are on page 1of 16

Pattern-Oriented Software Design

Introduction to
UML Class Diagrams

CSIE Department, NTUT


Woei-Kae Chen

UML: Unified Modeling Language


z

Successor to OOA&D methods


z

Unifies
z

late 1980s and early 1990s


GoF Book

Jacobson & OMT (Booch & Rumbaugh)

Graphical notation used to express


designs
z
z
z

Use cases
Class diagrams
Interaction diagrams
z
z

z
z
z
z

Sequence diagrams
Collaboration diagrams

Package diagrams
State diagrams
Activity diagrams
Deployment diagrams

UML class diagrams


z

Three perspectives
z

Conceptual
z
z

Specification
z
z

represents of the domain under study


relate to the class that implement them, but often
no direct mapping
looking at types rather than classes
a type represents an interface that may have
different implementations

Implementation
z

looking at classes

for our POSD class

UML: a class

+
#
-

public
protected
private

Abstract
Concrete
data type
parameter

Example: OBSort1.cpp

Example: OBSort1.cpp
Lets think of
main() as a class

relationship

UML: class relationship


Association
z Dependency
z Composition
z Aggregation
z

Inheritance

(knows a)

(uses a)

(has a)

(has a)

(is a)

X
z

Class template
X

(parameterized
class)

Uses a Knows a relationship


z

Uses a
z
z

Dependency
One object issues a function
call to a member function of
another object

Knows a
z
z

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

Is a Has a relationship
z

Is a relationships
z
z

Inheritance
A class is derived from
another class

Has a relationships
z
z

Composition or Aggregation
A class contains other
classes as members

Aggregation Composition
X
z
z

Both are Has a or part-of relationship


Composition
z
z
z

A stronger variety of aggregation


The part object may belong to only one
whole
Expected to live and die with the whole
z

delete whole delete part

Aggregation
z
z

Following Larman OOAD:


use of aggregation is NOT
recommended

Cascading delete is often


An aggregated instance can be shared

Example: has a relationship


a Point may appear in only
one Polygon or Circle

Delete Polygon delete Point


Delete Polygon
X delete Style

Larman: use
association instead
of aggregation

a Style may be shared by


many Polygons and Circles

Multiplicity

Relationship Examples
z
z
z
z
z
z
z
z
z
z
z
z
z

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
z
z
z
z
z
z
z
z
z
z
z
z

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

UML Example (C++): Association


X

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

UML Example (C++): Association


X

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

UML Example (C++): Dependency


X
class X
...
void
void
void
void
void
};

{
f1(Y
f2(Y
f3(Y
f4()
f5()

y) {; y.Foo();}
*y) {; y->Foo();}
&y) {; y.Foo();}
{Y y; y.Foo();}
{; Y::StaticFoo();}

Example: OBSort3.cpp

uses
getSize()
operator[]

UML Example (C++): Composition 1


X

class X {
Java?

...
Y a;

// 1; Composition

Y b[10];

// 0..10; Composition

};

UML Example (C++): Composition 2


X

class X {
X() { a = new Y[10]; }
~X(){ delete [] a; }
...
Y *a;
// 0..10; Composition
};
NOT Association

UML Example (C++): Composition 3


X

vector<Y>

Implementation detail

X
class X {
...
vector<Y> a;
};
Composition of
vector<Y>

Y
Hiding
implementation detail
NOT Composition
of Y

10

UML Example: OBSort3.cpp

UML Example (C++): Aggregation


z

No example here
z

Use Association instead of Aggregation

11

UML Example (C++): Inheritance

class Y {
...
};

class X : public Y {
...
};

is a relationship

Example: OOSort2.cpp

12

UML Example (C#): Implementation


public interface Y {
int Foo();
}
No fields

class X : Y
X implements Y
{
public int Foo()
{

}
}

X
Can do relationship

UML Example (C++): Implementation

class Y {
...
No variables
};
Only pure virtual functions

class X : public Y {
...
};

C++ allows multiple inheritance

13

UML Example (C++):


Template Class
template <class T>
class X {
...
...
...
};

...
X<Y> a;
...

14

Abstract class

15

C++ static member

16

You might also like