object-oriented programming introduction to uml …yccheng/oop2006f/uml-lecture04.pdf ·...

26
Object-Oriented Programming Introduction to UML Class Diagrams CSIE Department, NTUT Woei-Kae Chen

Upload: nguyendung

Post on 18-Aug-2018

233 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

Object-Oriented Programming Introduction toUML Class Diagrams

CSIE Department, NTUT

Woei-Kae Chen

Page 2: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

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

Page 3: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

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

Page 4: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

UML: a class

+ public# protected- private

AbstractConcrete

•data type•parameter

Page 5: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

Example: OBSort1.cpp

Page 6: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

Example: OBSort1.cpp

Let’s assume main() is a class

relationship

Page 7: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

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)

Page 8: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

“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

Page 9: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

“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

Page 10: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

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

Page 11: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

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

Page 12: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

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

};

Page 13: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

UML Example (C++): Dependency

X Y

class X {

...

void f1(Y y) {...; y.doIt();}

void f2(Y *y_ptr);

void f3(Y &y_ref);

};

Page 14: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

Example: OBSort3.cpp

uses•getSize()•operator[]

Page 15: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

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?

Page 16: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

UML Example (C++): Composition 2

class X {

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

~X(){ delete [] a; }

...

Y *a; // 0..10; Composition

};NOT Association

X Y

Page 17: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

UML Example: OBSort3.cpp

Page 18: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

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?

Page 19: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

UML Example (C++): Aggregation 2

class X {...vector<Y> b;

};

X vector<Y> Y

X Y

Hiding implementation detail

Implementation detail

Page 20: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

UML Example (C++): Inheritance

class Y {...};

class X : public Y {...};

X

Y

“is a” relationship

Page 21: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

Example: OOSort2.cpp

Page 22: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

UML Example (C++):Template Class

template <class T>

class X {

...

...

...

};

XY

...

X<Y> a;

...

Page 23: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance
Page 24: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance
Page 25: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

Abstract class

Page 26: Object-Oriented Programming Introduction to UML …yccheng/oop2006f/UML-lecture04.pdf · Object-Oriented Programming Introduction to UML Class Diagrams ... UML Example (C++): Inheritance

C++ static member