12+UML+Class++Diagram+and+Packages Updated+
12+UML+Class++Diagram+and+Packages Updated+
One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated tha
C.A.R. Hoare
Designing a system
Class
Name Window Window Window
size: Size size: Size
Attributes visibility: boolean visibility: boolean No inference can be
drawn about the
display() presence or absence
Operations hide() of elements in a
missing
compartment
Attributes
• Classes have attributes that describe the
characteristics of their objects.
• Attributes are atomic entities with no
responsibilities.
• Attribute syntax (partial):
▫ [visibility] name [ : type ] [ = defaultValue ]
• Class scope attributes are underlined
Attributes (cont.)
• Example (Java implementation):
Note
+ author: String = “unknown”
+ text : String
- total : long = 0
2..* 1..*
Car Door House
Whole Part
Aggregation (cont.)
• Aggregation tests:
▫ Is the phrase “part of” used to describe the
relationship?
A door is “part of” a car
▫ Are some operations on the whole automatically
applied to its parts?
Move the car, move the door.
▫ Are some attribute values propagated from the
whole to all or some of its parts?
The car is blue, therefore the door is blue.
▫ Is there an intrinsic asymmetry to the relationship
where one class is subordinate to the other?
A door is part of a car. A car is not part of a door.
Aggregation (cont.)
• Java implementation:
public class Car {
private Vector doors = new Vector();
public void addDoor(Door door) { ... }
...
}
public static void main(String[] args)
{
Door door = new Door();
House house = new House(door);
Car car = new Car();
car.addDoor(door);
...
}
Composition
• A strong form of aggregation
▫ The whole is the sole owner of its part.
The part object may belong to only one whole
▫ Multiplicity on the whole side must be zero or one.
▫ The life time of the part is dependent upon the
whole.
The composite must manage the creation and destruction
of its parts.
1
Circle Point Circle
Point
Composition
Window
Window
1
1 1
Shape
Shape
Shared Target Style
...
Polygon Ellipse Spline
Dependency
• A dependency is a relation between two classes in
which a change in one may force changes in the
other although there is no explicit association
between them.
• A stereotype may be used to denote the type of the
dependency.
• Example - a class calls a class scope operation of another class.
<<friend>>
Iterator Vector
Realization
• A realization relationship indicates that one class
implements a behavior specified by another class
(an interface or protocol).
• An interface can be realized by many classes.
• A class may realize many interfaces.
<<interface>>
LinkedList List
Realization (cont.)
• Java implementation:
public interface List
{
boolean add(Object o);
...
}
public class LinkedList implements List
{
public boolean add(Object o) { ... }
...
}
Constraint Rules and Notes
• Constraints and notes annotate among other
things associations, attributes, operations and
classes.
• Constraints are semantic restrictions noted as
expressions.
▫ UML offers many pre-defined constraints.
Customer
1 * may be
Order
{ total < $50 } canceled
id: long { value > 0 }
Constraint Note
UML Packages
• A package is a general purpose grouping
mechanism.
▫ Can be used to group any UML element (e.g. use case,
actors, classes, components and other packages.
• Commonly used for specifying the logical
distribution of classes.
Name
Packages and Class Diagrams
b
Packages and Class Diagrams (cont.)
b a
a.A
b.C
a.B
Packages and Class Diagrams (cont.)
E D B
C
Example UML Class Diagram
Reference: www.smartdraw.com
Tips