You are on page 1of 55

Readings

Chapters 3 & 4: UML for Java Programmers, Bob


C. Martin, PHI, 2002.
Chapters 4 to 8, Russ Miles and Kim Hamilton,
Learning UML 2.0 2006.
(contains Java examples)
Martin Fowler, UML Distilled, 3rd Edition, 2003.
Chapter 7: Collaboration between Objects, Java
the UML Way, Else Lervik and Vegard B. Havdal,
John Wiley & Sons Ltd, 2002.
Acknowledgements
Eran Toch, Modeling Class Architecture, Analysis
and Specification of Information Systems,
Technion http://www.technion.ac.il/~erant
Alexander Serebrenik, Structural Specification,
2IW80 Software Specification and Architecture,
Eindhoven University of Technology
J Paul Gibson, UML OO Modeling, CSC 7322:
Object Oriented Development,
http://www-public.it-
sudparis.edu/~gibson/Teaching/CSC7322
http://www.uml-diagrams.org/
Major Concepts
Specification of software design and code
through:
Class diagrams
Object diagrams
Sequence diagrams
Collaboration diagrams
Object-Oriented Approach
Objects are abstractions of real-world or
system entities
Reality Domain Model Domain

bus

models
vehicle

car

models

cup

models
UML Diagrams
Major Concepts
Specification of software design and code
through:
Class diagrams
Object diagrams
Sequence diagrams
Collaboration diagrams
Class Notation

Class
Name

Attributes

Operations
Attributes - Signature
[visibility] name [[multiplicity]] [: type] [=initial value] [{property}]

visibility: the access rights to the attribute


- multiplicity: how many instances of the attribute are they:
- middleName [0..1] : String, phoneNumber [1..*]
- Type: the type of the attribute (integer, String, Person, Course)
- initial value: a default value of the attribute
- salary : Real = 10000, position : Point = (0,0)
- property: predefined properties of the attribute
- Changeable, readOnly, addOnly, frozen (C++: const, Java: final)
Attributes - Examples
+ isLightOn : boolean = false
- numOfPeople : int
mySport
+ passengers : Customer[0..10]
- id : long {readOnly}
Operations - Signature
[visibility] name [(parameter-list)] [: return-type] [{property}]

An operation can have zero or more parameters, each has


the syntax:
[direction] name : type [=default-value]
Direction can be: in (input paremter -
(output parameter - may be modified), inout (both, may be
modified)
Property:
{leaf} concrete operation
{abstract} cannot be called directly
{isQuery} operation leaves the state of the operation unchanged
Operations - Examples
+ isLightOn() : boolean
+ addColor(newColor : Color)
+ addColor(newColor : Color) : void
# convertToPoint(x : int, y : int) : Point
- changeItem([in] key : string, [out] newItem :
Item) : int
Visibility
public (+) external objects can access the
member
private (-) only internal methods can access the
member
protected (#) only internal methods, or methods
of specialized objects can access the member
We will try to
keep the visibility
as minimal as
possible
Sample Classes
Relations
A relation is a template for a connection between two
instances.
Different kinds of relations:
Associations

Multiplicity
Objects on both sides of the Indicates cardinality
default
association can find each 3 exactly 3 object
other * (or n) - unbounded
1..* - 1 to eternity
The relation is consistent in 3..9 3 to 9
time (unless removed)
Association Example
public class Person {
private Dog pet;
}
public class Dog {
private Person owner;
}
public class PetCentre {
public Dog[] availableDogs;
}
Navigation
Folder File
Given a folder, we want to
know the files of each
folder.
However, we do not have a
requirement for knowing the
folder of each file.

If an association is directed, messages can pass


only on that direction
If the association does not have directions, then

By default, all relations should be directed,


unless the requirements dictate a bidirectional
relation
Association Classes
Denoted as a class attached to the association,
and specify properties of the association

An association class According to the


requirements, each
and may include product can appear is
relations, inheritance several orders, and
etc. each order may include
several products
Association Class - Objects

Links should be
verified,
according to the
association
multiplicity
For each link, an
association class
instance should
be declared
Attribute or association?
Attribute
Value is important, not identity: Date, Number,
String
Little behavior and functionality

Association (i.e., a separate class)


Identity is important: Customer, Order, Student,
Book
Complex behavior or functionality
Class Normalization
Classes should be normalized, if:
1. Attributes are selected from large or infinite sets
2. Relations with attributes are in n:n form
3. Groups of attributes are related

1
2
3

Before After
Dependency
Notated by a dotted line
The most general relation between classes
Indicates that an object affects another object

AccountingSystem
creates a Receipt
object
Dependency
Dependencies are the most abstract type of
relations.
Dependencies are always directed
Types:
call
create
MailClient-MailServer Relation
Aggregation
Whole-
Assemble a class from other classes
- assemble a class from a
couple of instances of that class
Aggregation (2)

Parts can exist without the whole


LabClass-Student Relation
Composition
Composition is a stronger form of aggregation
Contained objects that live and die with the
container
Container creates and destroys the contained
objects
Clock-NumberDisplay Relation
Composition vs. Aggregation
Aggregation Composition

Part can be shared by several Part is always a part of a single


wholes whole
0..4 * *
category document Window Frame
Parts can live independently Parts exist only as part of the
(i.e., whole cardinality can be whole. When the wall is
0..*) destroyed, they are destroyed

Whole is not solely responsible Whole is responsible and


for the object should create/destroy the
objects
Role Names
Names may be added at each end of the
association
Provide better understanding of the association
meaning
Especially helpful in self-associated classes

Worker * *..1 *
Person Company
employee employer
1..0
Manages Manager
Four Different Representations
for Class Diagram

Use one most appropriate for context


Major Concepts
Specification of software design and code
through:
Class diagrams
Object diagrams
Sequence diagrams
Collaboration diagrams
Object Diagram
In an Object Diagram, class instances can be
modeled

In runtime

Class Diagram Object Diagram


Class Diagram to Object
Diagram: An Example
Class diagram:
Class Diagram to Object
Diagram: An Example
Object diagrams:
@time=t1
Class Diagram to Object
Diagram: An Example
Object diagrams:
@time=t2
Generics in Object Diagram

Class

Object

Java code:
ListOfThings<BlogEntry> listofBlogEntries;
Association Classes
Denoted as a class attached to the association, and
specify properties of the association

According to the
An association class requirements, each
product can appear is
several orders, and
each order may include
several products
Association Class - Objects

Links should be
verified,
according to the
association
multiplicity
For each link, an
association class
instance should
be declared
Identifying Classes:
True Purpose of Object Diagrams
Motivating Scenario
The Corresponding Object
Diagram
The Corresponding Class
Diagram
Refinement to Class Diagram
Major Concepts
Specification of software design and code
through:
Class diagrams
Object diagrams
Sequence diagrams
Collaboration diagrams
Notations for
Sequence Diagram
Message Signature
Message signature
attribute = signal_or_message_name (arguments) : return_type

Examples:
doSomething()
doSomething(number1 : Number, number2 : Number)
doSomething() : ReturnClass
myVar = doSomething() : ReturnClass
Nested Messages
Message Arrows
Sample Sequence Diagram
Corresponding Java Code
public class MailServer
{
1 public MailItem getNextMailItem(String who)
{
1.1 Iterator<MailItem> it = items.iterator();
loop while(it.hasNext()) {
1.2 MailItem item = it.next();
opt if(item.getTo().equals(who)) {
1.3 it.remove();
1.4 return item;
}
}
1.4 return null; Correspondence with
} numbers in sequence
diagram
Major Concepts
Specification of software design and code
through:
Class diagrams
Object diagrams
Sequence diagrams
Collaboration diagrams
Sample Sequence Diagram
Equivalent Communication
Diagram

Note:
Sample Communication
Diagram

You might also like