Professional Documents
Culture Documents
Design
1
Introduction to object and class
What’s object
– Identity
– State
– Behavior
Sequence Diagram
Statechart Diagram
Fundamentals of Object-Oriented
Programming
What’s object
Conceptually, there are many ways to think
of an object
– something that can be seen or touched
– a thing to which some action is directed
– something that performs an action
The structure and behaviour of similar
objects are defined in their common class
Objects have three properties: identity , state,
and behaviour
Object Property 1: Identity
Identity is that property of an object which
distinguishes it from all other objects
Most programming languages use variable
names to refer to objects
Keep in mind, however, that an object may
not have a name; Similarly, an object might
have multiple names (aliases)
– For this reason, there is a subtle distinction
made between the concepts of "name" and
"identity"
Object Property 2: State
The state of an object encompasses all of the
descriptions of the object plus the current
values of each of these descriptions
Object Property 3: Behaviour
Behaviour is how an object acts and reacts,
in terms of its state changes and message
passing
The state of an object represents the
cumulative results of its behaviour
In object-oriented programming, a
behaviour is invoked by sending a message
to an object
Example Objects
There are many physical objects we can
examine right in this room
– each person is an object
– any chair is not an object
– each light bulb is an object
– Any book is not an object
– this room itself is an object (full or not)
See if the followings are objects or not:
– Desk
– Lamp
– Person
– Log
– The Earth
– Clock
– Machine
– Computer
– Saving account
Answer: N, Y, Y, N, Y, Y, Y, Y, Y
What’s the relationship among
objects: Messages and Methods
A object executes a method when it
sends/receives a message
For example, we can ask/tell a dog to sit by
sending him the message "sit"
In object-oriented programming, objects
send messages asking/telling others to
perform behaviors -- those other objects
invoke the method corresponding to the
message in order to execute the desired
behavior
What’s class
A class describes the common structure
(attributes/state) and behaviour of its instances
For example,
action
state1 state2
operations/methods
attributes: {attribute1,
attribute2}
structure
operation/method
attribute1 attribute2
value
Light
Turn on/off
On Off
operations/methods:
Turn_on/off ( )
attributes: {True,
False}
structure
Turn_on
True False
Turn_off value
Class Diagram
Introduction
Class Icon
Relationships
Constraints
Introduction
The class diagram is fundamental to object-
oriented programming
UML’s class diagrams capture the attributes
and operations of each class as well the
relationships that exist between classes
Class Icon
Class Icon
Hiding Details
Visibility Notation
Attribute Specification
Operation Specification
Class Icon
The UML class icon is
a rectangle with three
compartments: Circle
– class name radius: float
– class attributes
center_x: int
– class operations
center_y: int
Attributes are specified
area()
in the following form:
– object:class name display()
Hiding Detail
You can optionally leave out the attributes,
operations, or both in a class icon:
Circle Circle Circle
radius: float
area() center_x: int
display() center_y: int
UML Member Visibility
Notation
UML has three visibility prefixes for
members:
+ for public, # for protected, and – for
private
– e.g.
Circle
-radius: float
#area()
+display()
Full UML Attribute
Specification
The full form of a UML attribute is as
follows:
[visibility] name [multiplicity] [: type] [= initial value]
[{property}]
– The property choices are changeable, addOnly, and frozen
Student Info
-id: string
-hasGraduated: bool = false
Full UML Operation
Specification
The full form of a UML operation is as
follows:
[visibility] name [(parameter-list)] [:return-type] [{property}]
– The property choices are sequential, concurrent, guarded, and isQuery
Student
Association
An association is a structural relationship that specifies
that objects of one thing are connected to objects of
another
Faculty Student
Association
Associations can be adorned with a name.
Teaching
Faculty Student
Association
Associations can be adorned with the roles.
teacher
Faculty learner Student
Association
Associations can be adorned with the multiplicity.
Faculty Student
1 4..*
Association Class (relation
attributes)
Each object of association class is one
instance of relationship (link) in an
association.
1..*
Faculty 4..*
Student
Course
Qualified Association
Qualified association relates two classes and
a qualifier.
The qualifier is a special attribute that
reduces the effective multiplicity of an
association.
Directory file File
name
Ternary Association
A ternary association is a structural relationship that
specifies that objects of one thing are connected to
objects of other two’s.
Project Language
Developer
Aggregation
An aggregation is an association that
represents whole/part relationship
The “whole” end of the association
relationship is adorned with an open
diamond shape (e.g. X is part of Y)
e.g. door:Door is part of car:Car
X Y
Generalization, Aggregation, and
Association
See if the following pairs of classes have
generalization, aggregation or association
Faculty & student (as)
Hospital & doctor (as)
Door & Car (ag --<>)
Member & Organization (ag --<>)
People & student (ge <|-- )
Circle & point (ge --|>)
Department & Faculty (as)
Employee & Faculty (ge <|--)
Item & Printer (ge <|--)
Account & Checking account (ge <|-- )
Constraints
Constraints on Object
Constraints on Relations (Ordering)
General Constraints (Dependency)
Constraints on Object
The constraints restricts the values that
objects can be.
Example: No employee’s salary can exceed
the salary of the employee’s boss.
{salary <=boss.salary}
Employee Employer
Salary Salary
Constraints on Relations
(Ordering)
{Order} indicates that the elements of the
“many” end of an association have an explicit
order that must be preserved.
{ordered}
Paper 1
1.. Author
*
General Constraints
(Dependency)
Aerodrome
Runway
checkRVR( r: Runway )
Dependencies are also often used to express general
constraints.
member-of
Person {subset} Committee
chair-of
UML Design
UML class diagram is not object diagram.
UML class diagram is not data flow chart.
How to describe the behaviors/actions of
objects in UML class diagram?
What kind of relationship is generalization?
How to keep the consistency of all the
constraints?
Common mistakes in Time Sequence Diagram
Samples
UML class diagram is not object
diagram
Customer Loan
bargain
Customer Dealer
sign
buy sell report
Product Manager
authorize
UML class diagram is not data
flow chart
Product
Customer Salesman
Dealer Manager
How to describe the
behaviors/actions of objects in
UML class diagram
Person
sSN
address
?
email
Customer
Faculty Casher
Student
How to describe the
behaviors/actions of objects in
UML class diagram
Use case (stories): Each use case defines an interaction
between an actor and the system
Use case
Use case
Order
Check
? Waiting
Failure
Authorized
Success Failure
How to describe the
behaviors/actions of objects in
UML class diagram
– In design, see
www.cs.wcupa.edu/~zjiang/intro_uc.ppt.
– In programming, see time sequence
diagram, see
www.cs.wcupa.edu/~zjiang/intro_TS.ppt.
What kind of relationship is
generalization
Position
Point
x: int
y: int
Point
Position
People
1 1..6
Ticket-buyer Traveler
1 1
0..6
Dependent 1
How to keep the consistency of
all the constraints
0..1 0..1
Ticket-buyer Traveler
1 1
0..6
Dependent 0..1
How to keep the consistency of
all the constraints
Traveler
0..1
1 0..6
Ticket-buyer Dependent