Oop09 61. Object Oriented Programming License: http://creativecommons.org/licenses/by-nc-nd/3.0/ Object Oriented Programming
Class Diagrams
A class diagram shows the structure of the classes and the relationships
The Unified Modeling Language - UML between them.
Comment Class
The UML is a visual language for specifying, constructing, and documenting
the artifacts of a software. Student Class Name
The UML is not a method to design systems, it is used to visualize the
analysis and the design. name
number Attributes
It makes easier to understand and document software systems.
getName()
It supports teamwork because UML diagrams are more understandable than
the program code. Methods
There are different kinds of UML diagrams, which are used in different If necessary, access modes and data types may be also shown.
phases of a software development process.
Student
Here, we will discuss three types of these diagrams, which are used in design private
and coding levels. - name: string
The current specification of the UML is available in the Web site of the protected # number: int
return type
Object Management Group (OMG). + getName(): string
URL: http://www.omg.org/ public
In this course, the current specification of the UML, version 2.x, is used.
http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.1 http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.2
http://www.buzluca.info http://www.buzluca.info
Object Oriented Programming Object Oriented Programming
Relationship between classes
Comments : Comments in UML are placed in dog eared rectangles.
A class diagram also shows the relationships between classes such as
You can use comments to put anything you want in a diagram. You can use
association, aggregation, composition, and inheritance.
comments to add application and program specific details.
Association: A general type of relationship. Objects of a class can send messages
Stereotypes: A stereotype is a way of extending the UML in a uniform way, to objects of another class.
and remaining within the standard. name of the association
You indicate a stereotype using: <<stereotype name>>
Principal can send messages to
Constraints: A constraint in the UML is a text string in curly braces ({usually Principal School School objects.
directs
language specific}). The UML defines a language (Object Constrain Language –
name name Association names are used in
OCL) that you can use for writing constraints. schoolName address conceptual perspective
getName() (analysis phase).
In software perspective
Static attribute Counter association names are
Constant
(underlined)
(constraint) name of data in Principal unnecessary.
- counter: int
- MAX: int = 100 {readOnly} Principal
Static method School class Pricipal{
+ set(initial:int) directedSchool
name private:
<<constructor>> name
Stereotype +Counter() schoolName directedSchool *School;
address
:
getName()
};
http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.3 http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.4
http://www.buzluca.info http://www.buzluca.info
Object Oriented Programming Object Oriented Programming
Direction of the message flow: Example:
Multiplicity One object of class A is associated
A B A with one ore more objects of class
B A 1 1..*
B B at a time.
X {List} Class A includes a list that can
Constraint contain one or more objects of
class B.
Direction of messages is A can send messages to B.
unspecified. Both may send A get a service from B.
messages to each other. B can not send messages to A. * Zero or more, many
A
Multiplicity:
Multiplicity indicates the number of any possible combination of objects of one
class associated with objects from another class. 1..*
A One or more
In other words, it shows the number of objects from that class, that can be
linked at runtime with one instance of the class at the other end of the
1..40
association line. A One to forty
Multiplicity
teaches *
Instructor 1 Course
5
A Exactly five
An instructor teaches zero or more courses.
An association may also read in reverse order. 3, 5, 8
A course is given exactly by one instructor. A Exactly 3, 5, ore 8
http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.5 http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.6
http://www.buzluca.info http://www.buzluca.info
1
2. Object Oriented Programming License: http://creativecommons.org/licenses/by-nc-nd/3.0/ Object Oriented Programming
Aggregation, Composition: Both are a type of association. They are qualified by Composition: Composition is a strong kind of aggregation where the parts cannot
a “ has a” relationship. exist independently of the "whole" object.
There is a small difference between them. Examples: A human has a head. A car has an engine.
Aggregation: It indicates a “Whole/Part” relationship. A composition relation implies that:
A department of the faculty has instructors. a) An instance of the part belongs to only one composite objects.
Parts (instructors) can still exist even if the whole (the department) does not b) An instance of the part must belong to one composite object. It can not exist
exist. without the whole-object.
The same part-object can belong to more than one objects a time. c) The composite is responsible for the creation and deletion of its parts. If
the composite is destroyed its parts must either be destroyed.
1 1..*
Department Instructor 1 1
Human Head
Aggregation
(empty diamond) Composition
(filled diamond)
http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.7 http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.8
http://www.buzluca.info http://www.buzluca.info
Object Oriented Programming Object Oriented Programming
Inheritance: The white triangular arrow should point towards the class being Example:
extended. Partial class diagram of a part of a system.
Teacher
The arrow should point upwards. This is not a rule 1 myCourses * 1..* myCourses
of UML, but it feels more logical and easier to read Teacher Course
{List}
in this form.
1
Principal 1..*
1 directedSchool 1 1
Abstract classes and pure virtual (abstract) methods Principal School Student
{Vector}
are written with italic fonts.
Abstract class Class School {
Class Teacher {
Shape (italic) private:
private:
Course * myCourses; //may be a linked list vector<Student*> students;
+draw() Pure virtual method : :
(italic) }; };
class Principal:public Teacher{ class Student{
private: private:
Rectangle Circle Real method list<Course*> myCourses;
School directedSchool;
+draw() +draw() Implementation of virtual draw // or :
School *directedSchool; };
};
http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.9 http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.10
http://www.buzluca.info http://www.buzluca.info
Object Oriented Programming Object Oriented Programming
UML Interaction Diagrams Example: Direction of
First internal message
Interaction diagrams illustrate how objects interact via messages. the message
There are two common types: communication and sequence interaction diagrams.
openCourse(crn) 1: openCourse(crn)
Both can express similar interactions. : School :Teacher
Sequence diagrams are more notationally rich, but communication diagrams have
their use as well, especially for wall sketching. 1.1: create(crn)
Link
Communication diagrams:
First message :Course
They illustrate object interactions in a graph or network format, in which objects (External)
parameter
Creates a new object+
can be placed anywhere on the diagram. Object
Constructor call
cd Example Diagram
class Teacher{
message1() private:
:ClassA
Name of the diagram Course * myCourse;
cd: Communication public:
diagram Any instance 1: message2() void openCourse(int crn){ // openCourse method of the Teacher
(object)
myCourse = new Course(crn); // An object of type Course is created
of Class A
2: message3() // Other operations ...
}
Object nb // Other members ...
of ClassB nb:ClassB
};
http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.11 http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.12
http://www.buzluca.info http://www.buzluca.info
2
3. Object Oriented Programming License: http://creativecommons.org/licenses/by-nc-nd/3.0/ Object Oriented Programming
Sequence numbers of messages: msg1()
Messages to “self” or “this”:
The external message is not numbered.
Nested message A message can be sent from an object to itself.
It is sent in the :AnyClass
method of msg2
First Second
Creation of Instances:
Third Any message can be used to create an instance, but there
msg1() 1: msg2() 1: clear()
:ClassA :ClassB is a convention in the UML to use a message named create
for this purpose (some use new).
1.1: msg3()
If another message name is used, the message may be
2.1: msg5() A B C D annotated with a stereotype, like so: «create».
2: msg4() msg1()
:ClassC msg2() The create message may include parameters, indicating the
Fifth
msg3() passing of initial values. This indicates, a constructor call
1:
1.1:
with parameters.
Fourth 2.2: msg6()
1: create()
: School :Course {new}
msg4()
Sixth :ClassD
2:
msg5()
msg6() «create»
2.1: 1: make()
2.2: : School :Course {new}
http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.13 http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.14
http://www.buzluca.info http://www.buzluca.info
Object Oriented Programming Object Oriented Programming
Conditional Messages: Iteration or Looping:
The message is only sent if the clause evaluates to true.
iteration is indicated with a * and an optional
Conditional message
iteration clause following the sequence number
message1()
runSimulation ()
1 [ color = red ] : calculate()
:ClassA : ClassB 1 * [i:=1..N ] : num := nextInt()
: Simulator :Random
Mutually Exclusive Paths:
Message flows between objects may follow different paths according to some
Iteration Over a Collection (Multiobject):
conditions.
A common algorithm is to iterate over all members of a collection (such as a list or
In the example there are two path according to condition "test" : a or b .
map), sending a message to each.
Unconditionally after :ClassE 1a and 1b are mutually
either msg2 or msg4 exclusive paths
In the UML, the term “multiobject” is used to denote a set of instances.
2: msg6()
This box represents one instance
msg1() 1a [test] : msg2() from a collection of many
:ClassA :ClassB Course objects
avg := getAverage ()
1 * [i:=1..N ] : grade := getGrade() courses[i]:
1b [not test] : msg4() 1a.1: msg3() : Student Course
1b.1: msg5()
:ClassD :ClassC optional
http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.15 http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.16
http://www.buzluca.info http://www.buzluca.info
Object Oriented Programming Object Oriented Programming
Sequence diagrams: Example: Object
Creates a new object
Sequence diagrams also illustrate the interactions between objects. Constructor call
Message
They clearly show sequence or time ordering of messages. : School :Teacher
Any instance Object nb New Object
(object) of ClassB openCourse(crn)
of Class A
openCourse(crn)
sd Example create(crn)
: Course
:ClassAInstance nb:ClassBInstance
message1()
Name of the diagram : ClassA : ClassB
sd: Sequence Illustrating Reply or Returns:
message2()
diagram A sequence diagram may optionally msg1()
show the return from a message as a reply := getSomething()
message3()
dashed open-arrowed line at the end
of an activation box. getSomething()
There are two ways to show the reply
return result from a message: msg4()
1. Using the message syntax:
Body (lifetime) Lifeline of the returnVar := message (parameter) msg5()
of the method object 2. Using a reply (return) message line.
http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.17 http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.18
http://www.buzluca.info http://www.buzluca.info
3
4. Object Oriented Programming License: http://creativecommons.org/licenses/by-nc-nd/3.0/ Object Oriented Programming
Conditional Messages:
Messages to “self” or “this”: :Counter To support conditional and looping constructs, the UML uses frames.
A message can be sent from an object to itself. msg1() Frames are regions or fragments of the diagrams; they have an operator or label
clear() (such as loop or opt) and a guard (conditional clause).
In order to illustrate conditional messages an opt frame is placed around one or
Object Destruction: more messages.
In some circumstances it is desirable to show explicit
destruction of an object (as in C++, which does not have
garbage collection). sd if-then
In this case delete operator is used and destructor of the :A :B
Label
target object is called.
msg1()
The «destroy» stereotyped msg x
:Admin message, with the large
X and short lifeline Frame opt [color = blue]
if condition is true calculate()
indicates explicit object
create(crn) destruction
:Course msg y
....
<<destroy>>
X
http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.19 http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.20
http://www.buzluca.info http://www.buzluca.info
Object Oriented Programming Object Oriented Programming
Mutually Exclusive Conditional Messages : Looping:
An alt frame is placed around the mutually exclusive alternatives.
:Simulator :Random :Programmer
runSimulation()
sd if-then-else
:A :B :C loop [i:=1..N]
msg1() hours := nextInt()
Guard
alt [x<10] Continuation condition work(hours)
calculate()
[else]
calculate() eat()
Borders of
The for loop
http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.21 http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.22
http://www.buzluca.info http://www.buzluca.info
Object Oriented Programming Object Oriented Programming
Iteration Over a Collection (Multiobject): Interaction of diagrams :
Reference frames are used to simplify a diagram and factor out a portion into another
A common algorithm is to iterate over all members of a collection (such as a list or diagram, or if there is a reusable interaction occurrence.
map), sending a message to each. It is like calling subroutines.
In the UML, the term “multiobject” is used to denote a set of instances. sd interaction
:A :B :C
myCourses[i]
:Student :Course msg1()
msg1()
avg := getAverage ()
ref
loop [i<myCourses.size] Operation
grade := getGrade()
i++ msg2()
sd Operation
:B :C
An optional activation box
may contain arbitrary msg x()
language statements
msg y()
msg z()
http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.23 http://www.faculty.itu.edu.tr/buzluca ©1999-2010 Dr. Feza BUZLUCA 9.24
http://www.buzluca.info http://www.buzluca.info
4