Title: Software Engineering, CPSC-4360-01, CPSC-5360-01, Lecture 6
1Software Engineering, CPSC-4360-01,
CPSC-5360-01,Lecture 6
2Review of Last Lecture
- Design
- Major activities during design phase.
- Tools
- Class Diagram.
- Object Diagram.
3Overview of This Lecture
- Class Diagram
- Association Class, Reification
- N-Ary Association
- Qualified Association
- Interface
- Interactions diagrams
- Collaborations, classifier and association roles
- Interaction diagrams, object creation and
destruction - Role multiplicity and iterated messages
- Multi-objects
- Conditional messages, messages to self
4Where are we now?
- In depth look at the relevant tools
- Class Diagram
- Interaction Diagram
5Association Review
- Annotation for Association
Role Name The role of the instance in the
relationship. Represented as reference name
Multiplicity Number of instances associated
Association Name Description of the relationship
6Association Review
- Role names can also help to distinguish multiple
associations, or self association.
7Properties of Links
- There are cases where information about the link
need to be kept.
- Example
- A student takes a module and gets a mark for it.
- The mark only makes sense if we know the student
and the module. - The mark is not simply an attribute of either
class.
8Association Class
- Association Classes can be added.
- Behave like a combination of both association and
class - Association Can connect two classes.
- Class Allow attribute to be stored.
- Syntax dashed line between association and class
icon.
Association Class
9Association Class Example
- Example
- John took CPSC4360, and scored 25 marks.
John Student
CPSC4360 Module
- The Takes instance is uniquely defined by both
JohnStudent and CPSC4360Module. - Follow Up
- What if John retakes CPSC4360, and scored 99
marks?
10Reification
- Another technique to capture association
information is reification. - Reification means Replacing an association with
a Class.
Apply Reification. An intermediate class is added
11Reification Example
- Draw an object diagram for the two examples given
previously - John takes CPSC4360 and score 25
- John retakes CPSC4360 and score 99
- The reification has the property of allowing
students to take a module more than once, in case
they failed first time. - This property was not possible for previous
slide, when there is exactly one link between a
student and a module. - Question to ponder
- Is reification and association class the same?
Draw the Object Diagram before Lecture
12Association Class Property
- Association classes are classes
- So they can participate in associations.
- Example MarkSheet lists all students taking a
module with the marks they got.
Can participate in the relationship just like a
normal class
13N-ary Association
- Associations can connect more than two classes
- A 3-way association could be used to store marks.
- Multiplicity at a given association end defines
the number of instances of the class at that end
that can be linked to a tuple of instances, one
from each of the other ends of association. - Example specifies that each pair of student
and module object can be linked to zero or more
Attempt objects.
14Qualified Association
- In the real world, objects are usually
identified by a unique value (also known as key). - Example
- University uses Matriculation Number to identify
a student.
- Problems
- Is matric number unique? Is it represented as
such? - How do we know the matric can be used as a key?
15Qualified Association
- Syntax
- The key is known as a qualifier.
- The qualifier is written in a square box attached
to the class used to identify the other party. - The multiplicity is changed to reflect the
relationship with the qualifier instead of the
class.
Changed Multiplicity
Qualifier
16Qualified Association
- Semantics
- The set of qualifiers is unique in the context of
the attached class - E.g., the University has a set of unique matric
values. - The multiplicity reflects the relationship
between the identified class and the qualifier - E.g., a Student is given exactly one matric in
the University. - E.g., an University object will use matric to
uniquely identify zero or one Student (why zero?).
17Qualified Association Example
- Draw an object diagram to show
- A student John in LU university with matric. no.
007. - A student Helen in LU university with matric. no.
1234.
18Interface
- An interface in UML is a named set of operations.
- Interfaces are used to characterize the behaviour
of an entity. - shown as a stereotyped class
- Generalization can be defined between interfaces.
19Realizing an Interface
- A class realizes an interface if it provides
implementations of all the operations. - Similar to the implements keyword in Java.
- UML provides two equivalent ways of denoting this
relationship
ltltinterfacegtgtList
CircularLinkedList
CircularLinkedList
- Both represent CircularLinkedList implements all
operations defined by the List interface.
20Interface Dependency
- A class can be dependent on an interface.
- This means that it makes use of the operations
defined in that interface. - E.g., the Restaurant class makes use of the List
interface
21Interface Dependency
- If the class realizing the interface is known,
the dependency can be specified by
- This information can also be attached to the role
name (known as the interface specifier)
Restaurant
CircularLinkedList
bookingList List
22Evolution of Class Diagram
- Class Diagram evolves with the development
process - Starts out as a Domain Model
- Classes with name and attribute only.
- Refined to an Analysis Class Model
- Classes with name, attribute and simplified
operation. - Refined to a Design Class Model
- Classes with name, attribute and operation.
- Type information and Scope defined for attribute
and operation.
23Interaction Diagrams
24Interaction Diagrams
- When a system is running, objects interact by
passing messages. - The messages define the systems behaviour, but
they are not shown on static diagrams (such as
class diagrams). - UML defines two types of diagram for showing
interactions - collaboration diagram.
- sequence diagram.
25Using Object Diagram Interaction
- A message can be added to an Object Diagram.
- Syntax
- An arrow with message name and parameter (
) - Example
- The Bank performs Fund Transfer by withdrawing
from one account, and deposit to another account.
message
Object Diagram with Message
26Problems with Object Diagrams
- Object diagrams show a specific scenario
- They show specific objects, not the general case
- Can we withdraw from a2 and deposit in a1
instead? - They show a fixed number of objects and links
- Can we withdraw from a1 and deposit into a1 again
(for whatever reason)? - They cannot show alternative functionality
- What if the withdraw causes overdraft in a1? Can
we proceed with the deposit? - In a nutshell, we need something more general.
27Solution to this Problem
- A more general method is needed to specify
behaviour. - Collaborations in UML in general do not show
individual objects, but rather the roles that
objects can play in the interaction. - An object diagram used to illustrate a
collaboration is known as a collaboration
instance set.
28Classifier Roles
- Define collaborations using classifier roles
- Represent any object of a class.
- Can have a name describing the role.
- Syntax
- object_name /role_name base_class
- Specify that any object of Account class can be
substituted. - /Debtor describes that money is taken out from
this account.
29Classifier Role
- Syntax Guide
- The classifier role is not underlined, to
distinguish from the object diagram usage. - The object_name can be used to label a classifier
role instead of a role name, when the role is not
clear/ not important. - The Object Diagram on Slide 25 is a collaboration
instance set of the Collaboration Diagram on
Slide 28 - Substitute an object a1 for the /Debtor role.
- Substitute an object a2 for the /Creditor role.
30Roles and Objects
- Objects can play different roles in interactions.
- An object can be substituted for a role if
- it is an instance of the base class of the role
- or one of its subclasses.
- In a given interaction, an object playing a
particular role will not normally make use of all
the features provided by the base class of the
role - E.g., an Account object in the /Debtor role will
only receive withdraw() message, but not
deposit() message.
31Association Role
- Similar to Classifier Role, an Association Role
generalizes the links in the Object Diagram. - An association role connecting two classifier
roles indicates that objects playing those roles - Can establish links to each other.
- Can exchange messages during interactions.
- Question
- What are the possible ways to set up a
communication (link) between two objects?
32Association Stereotypes
- There are five ways to establish a link between
two objects - Base Association
- Parameter
- Local Instantiation
- Global Variable
- Self-directed Link
- In UML, five corresponding stereotypes are used
to denote the above - ltltAssociationgtgt
- ltltParametergtgt
- ltltLocalgtgt
- ltltGlobalgtgt
- ltltSelfgtgt
33Association Role Base Association
- The most common kind of association.
- The association is defined between the
corresponding classes (i.e., show up in the class
diagram). - More permanent compared to other kind of
association. - Usually kept as an attribute in the class.
- Syntax
- Label the association role with stereotype
ltltAssociationgtgt. - Example
Class Diagram
34Base Association Example
ltltAssociationgtgt
ltltAssociationgtgt
- The interaction is possible because the Bank
object holds the IsHeldAt to the two account
objects. - As this is the most common case, the
ltltassociationgtgt stereotype is usually omitted.
35Association Role Parameter
- One object is passed to another as a parameter of
a message. - In programming languages, this is implemented by
passing a reference to the object. - The object receiving the message knows the
identity of the parameter object, and can, in the
method body, send messages to that object. - This link is temporary, being available while the
operation is executing. - Syntax
- Label with the stereotype ltltparametergtgt.
36Parameter Example
- class A
- //No association to class B
- void methodA(B objB)
- objB.methodB( )
- During the execution of methodA(), an object of
class A can pass a message to an object of class
B, because the reference is passed as parameter. - When methodA() terminates, the link will be over.
methodB( )
A
B
ltltparametergtgt
Collaboration Diagram
37Association Role Local Instantiation
- Implementations of operations can create local
instances of any class. - Sending messages to these objects during the
execution of the operation is now possible. - A link corresponding to a local variable only
lasts for the duration of an operation call. - Syntax
- Label with the stereotype ltltlocalgtgt.
38Local Instantiation Example
- class A
- //No association to class B
- void methodA( )
- B objB new B()
- objB.methodB( )
- During the execution of methodA(), an object of
class A can pass a message to an object of class
B, because a local object is created. - When methodA() terminates, the link will be gone.
39Association Role Global Variable
- If any global variables exist and are visible, an
object can send messages to an object stored in
such a variable. - Example in Java
- Nested class
- Example in C
- Global object pointer
- Syntax
- Label with the stereotype ltltglobalgtgt
40Global Variable Example
- class OuterA
- B objB
- class A //Nested class
- //No association to class B
- void methodA( )
- objB.methodB( )
-
- During the execution of methodA(), an object of
class A can pass a message to an object of class
B, because an attribute of the parent class is
accessible to all nested classes. - When methodA() terminates, the link remains
(why?).
methodB( )
A
B
ltltglobalgtgt
Collaboration Diagram
41Association Role Self-Directed
- An object can always send messages to itself,
even though no explicit link to self is
defined. - In programming languages, this capability is
provided by defining a pseudo-variable called
this or self. - Syntax
- Label with the stereotype ltltselfgtgt.
42Self-Directed Example
- class A
- //No association to class A
- void methodA( )
-
- this.methodA( )
- During the execution of methodA(), an object of
class A can pass a message to itself, because a
self reference (this in Java) is always
available. - When methodA() terminates, the link remains.
A
ltltselfgtgt
methodA( )
Collaboration Diagram
43Sequence Diagram Review
- Review sequence diagram introduced earlier in the
light of classifier role.
Classifier Role is the more formal description
Classifier Role
Classifier Role
Message With parameter
Message ( Para )
Activation Bar
Lifeline
Time Passes
Return value
Return Control and possibly value are returned
44Sequence Diagrams
- Show classifier roles only (no association
roles). - The vertical dimension is the time and the
messages are drawn from top to bottom, in the
order they are sent. - Each role has a lifeline
- indicating the period of time during which
objects playing that role exist. - Syntax vertical dashed line below the classifier
role. - Messages are shown as arrows leading from the
lifeline of the messages sender to that of the
receiver.
45Sequence Diagrams
- The period of time during which an object is
processing a message is called activation. - Syntax narrow rectangle whose top is connected
to a message. - When an object finishes processing a message, the
control returns to the sender of the message. - Syntax dashed arrow from the bottom of
activation rectangle back to lifeline of the role
that sent the message. - The messages with solid arrowhead denote
synchronous messages, such as normal procedure
calls (the object that sends the message is
suspended until the called object returns the
control to the caller).
46Sequence Diagram Simple Example
- Suppose statements are to be printed for bank
accounts - Bank passes the relevant Account to a Statement
object for printing.
Sequence Diagram
47Anything Missing?
- The link between classifier roles is not
indicated - How can a statement object contacts the relevant
account object ? - You have to read the diagram carefully to deduce
that the link may be established by the
parameter. - In some cases, such deductions are
impossible/prone to error when the information is
not enough. - A Collaboration Diagram can show the same
exchange, but also includes the association role
used for communication.
48Collaboration Diagram
- Show classifier and association roles.
- Compared with diagram on Slide 46, messages also
have - sequence numbers to indicate order
- optional returned values with notation.
Collaboration Diagram
Return Value
Sequence Number
49Collaboration versus Sequence Diagrams
- Unlike sequence diagrams, collaboration diagrams
show the association role. - Message sequencing cannot be shown graphically
and messages are numbered to indicate the order
in which they are sent. - Messages can be numbered sequentially, but more
commonly a hierarchical numbering scheme is used
(i.e., to reflect the nesting activation made
explicit in sequence diagrams).
50Hierarchical Numbering
- Within each activation, messages are numbered
sequentially starting from 1. - A unique label can then be generated for each
message by adding the number of the message to
the end of the number of the activation sending
the message. - Syntax use a .
- to separate the numbers and
- to reflect that another level of nesting of
control flow has been initiated.
51Hierarchical Numbering. Example
1 because it is the first message
1.1 because it is the first message sent during
the computation of message 1
1.2 because it is the second message sent during
the computation of message 1
2.1.2 because it is the second message sent
during the computation of message 2.1
52Interaction Diagram Additional Notations
- Subsequent examples illustrate the notations for
- Object Creation
- Object Destruction
- Iterated Messages
- Multiobjects
- Conditional Messages
- Message to Self
- Should take note of how to portrait certain
interaction in both Sequence Diagram (SD) and
Collaboration Diagram (CD).
53Sequence Diagram Object Creation
- As time is explicitly represented in a sequence
diagram, the object creation is easy to draw
A
class A void methodA( ) B objB new
B() objB.methodB( )
B( )
B
methodB( )
Sequence Diagram
54Collaboration Diagram Object Creation
- In a Collaboration Diagram, new objects and new
associations have to be labeled with new.
A
class A void methodA( ) B objB new
B() objB.methodB( )
1 B( )
new
Bnew
ltltlocalgtgt
2 methodB( )
Collaboration Diagram
55Sequence Diagram Object Destruction
- In languages with automatic garbage collection
(like Java), you cannot explicitly delete an
object. - Instead, remove all references to the object for
it to be collected. - Label the message as ltltdestroygtgt.
- Mark the lifeline of the destroyed object with a
cross.
class A void methodA( ) B objB new
B() ... ... ... objB null ... ...
...
A
B
ltltdestroygtgt
Sequence Diagram
Reminder Only the circled part of code is
represented.
56Collaboration Diagram Object Destruction
- In a Collaboration Diagram
- Similarly, label the message as ltltdestroygtgt.
- Label the destroyed objects and links with
destroyed.
class A void methodA( ) B objB new
B() ... ... ... objB null ... ...
...
A
destroyed
Bdestroyed
ltltlocalgtgt
1 ltltdestroygtgt
Collaboration Diagram
57Role Multiplicity
- The number of objects playing a role can vary
from one occasion to another. - Example (Restaurant Case Study)
- Looking for bookings for a particular date will
depend on how many total bookings are there.
Restaurant
Booking
Need to show many booking objects will be
involved.
getDate( )
Return date
Sequence Diagram
58Role Multiplicity
- Role multiplicity can be added to classifier role
to indicate the number of objects involved. - Syntax
- Same as the multiplicity notation in class
diagram (e.g., 1..8, , 2.. , etc).
Role Multiplicity Shows that zero or more Booking
objects will be involved
Restaurant
Booking
getDate( )
Return date
Sequence Diagram
59Role Multiplicity
- The notation is the same in both Sequence Diagram
and Collaboration Diagram. - However, the fact that the message is iterated is
still not represented.
Restaurant
Booking
getDate( )
Sent Multiple times
Return date
Sequence Diagram
60Iterated Messages
- We can clarify this by
- adding a multiplicity to the affected role.
- specifying that the message is iterated.
- Syntax
- Recurrence consists in written after the
sequence number, possibly followed by an
iteration clause. - There is no formal syntax for iteration clause.
Pseudo code-like condition is usually used, e.g.,
i 1 .. N or i 1 to N. - Example
Denote Recurrence
Denote the Recurrence Condition
1 i 1.. N getDate( )
Restaurant
Booking
Collaboration Diagram
61Multiobject
- A multiobject denotes a collection of objects
- It is a role with a multiplicity of zero or more.
Denote Multiobject
- It implies an intermediate data structure, e.g.
Restaurant
Booking
Vector
Collaboration Diagram
62Property of Multiobject
- Using multiobject prevents a premature commitment
to a particular data structure - E.g., What if vector is not a good data structure
for this case? - Semantically, a multiobject is a single object
representing a collection of objects. - A single message sent to it implies an operation
involving the collection of objects. - A good example of such operation would be looking
for a particular object in the collection.
63Multiobject Example
- Assume that there is only one booking per date to
simplify the discussion - A single message findBooking(Date) is sent to the
Booking multiobject. - The multiobject inspects all its Booking object
and returns the appropriate booking B (to
indicate that B is not a new object, but one from
the multiobject, a composition link is used). - The Restaurant stores B as a local reference for
subsequent operations.
64Multiobject Message for all objects
- To send a message to all objects in the
multiobject - Send a single message to the multiobject.
- The multiobject goes through some iterative
process and sends the message to each object in
the collection. - By convention, such interactions can be
abbreviated by using iterative messages. - Iterated messages to a multiobject are understood
to be sent to individual objects.
Each booking in the multiobject will receive the
Print( ) message
Print( )
Booking
Restaurant
Booking
Collaboration Diagram
65Sequence Diagram Conditional Message
- Conditions can be added to messages to show the
situations when they are sent. - Syntax
- Write the condition in preceding a message.
class A void methodA( ) B objB new
B() ... ... ... if ( x lt y )
objB.methodB( )
A
B
x lt y methodB( )
Sequence Diagram
66Collaboration Diagram Conditional Message
- Same syntax can be used for a collaboration
diagram.
class A void methodA( ) B objB new
B() ... ... ... if ( x lt y )
objB.methodB( )
A
B
Collaboration Diagram
67Alternative Flows
- Sequence diagrams can show alternative message
sequences in one diagram - Two or more messages start at the same point
(fork). - They are distinguished by conditions (only one
will be sent). - Return messages come together later (join).
- Objects that receive messages may need branching
lifelines to represent alternative possibilities. - Should draw two separate diagrams instead.
68Sequence Diagram Alternative Flow
- Pay attention to
- Branching of the messages (Fork).
- Joining of the return messages (Join).
B
A
class A void methodA( ) B objB new
B() ... ... ... if ( x lt y )
objB.methOne( ) else objB.methTwo( )
x lt y methOne( )
x gt y methTwo( )
Sequence Diagram
69Sequence Diagram Message to Self
- An object can send message to itself
- Invoking another operation of its own.
- Usually represents implementation details.
class A void methodA( ) B objB new
B() objB.methOne( ) class B void
methOne( ) methTwo( ) void methTwo(
) ... ... ...
A
B
methOne( )
methTwo( )
Sequence Diagram
70Collaboration Diagram Message to Self
- Make use of the ltltselfgtgt stereotype for
collaboration diagram.
class A void methodA( ) B objB new
B() objB.methOne( ) class B void
methOne( ) methTwo( ) void methTwo(
) ... ... ...
71Summary
- Class Diagram
- Association Class, Reification
- N-Ary Association
- Qualified Association
- Interface
- Interactions diagrams
- Collaborations, classifier and association roles
- Interaction diagrams, object creation and
destruction - Role multiplicity and iterated messages
- Multi-objects
- Conditional messages, messages to self
72Where are we now?
- Topics Covered
- Detailed Class Diagram
- Object Diagram
- Collaboration Diagram
- Sequence Diagram
73Reading Suggestions
- Chapter 5 of Bimlesh, Andrei, Soo 2007
- Chapters 8 and 9 of Priestley 2004
74Coming up next
- Chapter 6 of Bimlesh, Andrei, Soo 2007
- Chapter 10 of Priestley 2004
75Thank you for your attention!Questions?