Title: UML Class Associations Sipping from the Fire Hose
1UML Class AssociationsSipping from the Fire Hose
2Exercise From Last Time Possible Solution
Recipe
Cookbook
- name - ingredients - instructions -
nutritionInformation
- categories - recipes - name
setName(name String) add(ingredient
Ingredient) remove (ingredient
Ingredient) display() print()
setName(name String) add(recipe Recipe,
category String) remove(recipe Recipe,
category String) display() print()
Ingredient
- name - amount - nutritionInformation
setName(name String) setAmount(amount
float) setUnit(unit String)
3Session Goals
- At the end of this session, you will be able to
define and apply the following class
relationships - Dependency
- Association
- Shared Aggregation
- Composite Aggregation
- Generalization/Specialization
4Relationships
- Classes can relate to other classes in several
ways - UML relationship types in increasing strength
- Dependency
- Association
- Aggregation
- Composition
- Generalization
5Dependency
- The weakest relationship
- Represented by a dashed arrow
- Only implies a change the target may affect the
source - Further investigation is required
Position
BilliardBall
6Source Code Example
The dependency may be relatively weak. Here the
dependency is only in the fact that setPosition()
uses the type for a parameter.
public class BilliardBall private int
color private int number private int
xPixel private int yPixel public void
setPosition (Position p) public void hitBy
(BilliardBall b) public void draw()
public class Position private double x
private double y private double z public
void moveTo (double x, double y, double z)
public void tranform (Matrix matrix)
7Source Code Example
The dependency may be relatively strong. Here the
dependency exists because the class has an
attribute of the type.
public class BilliardBall private int
color private int number private
Position position public void setPosition
(Position p) public void hitBy
(BilliardBall b) public void draw()
public class Position private double x
private double y private double z public
void moveTo (double x, double y, double z)
public void tranform (Matrix matrix)
8Dependencies and Other Relationships
- Dependencies can exist in many forms
- In fact, all other relationships are a form of
dependency - UML provides other notations to assert common
forms - Advanced - For those UML does not supply,
stereotypes can be used
9Association
- Directed relationship
- Represented by a regular line with an arrow
- May be bi-directional
- Represented by a line (no arrow heads)
- Implies that an instance of the source is aware
of an instance of the target
10Association
Unidirectional Association
Road
Car
Bidirectional Association
University
Professor
11Associations and Class Structure
- There are different ways to code the association
relationship - IST 240 Method!
- Associations require an attribute
- Associations will have set operations
- May be set like (e.g., add() in the case of
multiplicity) - May be encapsulated in the constructor
- Associations may have get operations
- These access state data
- May be get like (e.g., find())
12Source Code Example - Unidirectional
Notice that Car has an attribute of type Road but
Road does not reference Car.
public class Car private Road currentRoad
private int speedInMPH public void
setCurrentRoad(Road r) public Road
getCurrentRoad() public void
setSpeedInMPH (int newSpeedInMPH) public
void setSpeedInKPH (int newSpeedInMPH)
public int getSpeedInMPH() public int
getSpeedInKPH()
public class Road private String name
private int speedLimitInMPH public void
setName(String name) public void
setLimitInMPH (int limitInMPH) public void
setLimitInKPH (int limitInKPH) pubic
String getName() public int
getLimitInMPH() public int
getLimitInKPH()
13Source Code Example - Bidirectional
Here Professor has an attribute of type
University. University has an array of type
Professor.
class Professor private String name
private CV cv private University
currentEmployer public void setUniversity
(University u) public void displayCV()
class University private Professor
professors100 private int
currentNumberOfProfessors public void
hireProfessor (Professor p) public void
fireProfessor (Professor p)
14Roles Names and Multiplicity
- Associations may also have role names
- An instance of the source refers to an instance
of the target via the role name - Associations may also indicate multiplicity
- Multiplicity describes the number (or range) of
instances of a class that can take part in the
association - Multiplicity can be placed at either the source
or target in association
Multiplicity Options 0 lt n lt m
n exactly n n .. at least n Unspecified
.. m at most m n .. m between n and m
15Associations
Unidirectional Association
0..1
Road
Car
currentRoad
Bidirectional Association
1
University
Person
employer
employee
16Associations
An instance of road cannot be aware of an
instance of car, hence there is no role name.
Unidirectional Association
0..1
Road
Car
currentRoad
Bidirectional Association
1
University
Person
employer
employee
17For Car Is there a difference?
0..1
Road
Car
currentRoad
Car
Road
currentRoad Road
setRoad(road Road)
getRoad() Road
18For Car Is there a difference?
0..1
Road
Car
currentRoad
Car
Road
currentRoad Road
setRoad(road Road)
getRoad() Road
No. Both diagrams indicate that class Car has an
attribute, currentRoad, of type Road and at any
point in time currentRoad may or may not
reference a Road object
19For Car Is there a difference?
1
Road
Car
currentRoad
Car
Road
currentRoad Road
setRoad(road Road)
getRoad() Road
20For Car Is there a difference?
1
Road
Car
currentRoad
Car
Road
currentRoad Road
setRoad(road Road)
getRoad() Road
Structurally, no. Formally, the multiplicity of
1 indicates that currentRoad must reference an
instance of Road at all times.
21Aggregation
- Aggregation captures a part of relationship
- Represented as a line between two classes with a
diamond at the source (the larger object) - There are two flavors
- Shared aggregation
- A hollow diamond
- Composite Aggregation
- A filled in diamond
22Shared Aggregation
- Shared Aggregation, for all intents in purposes,
is association - The association has a slightly stronger feel
- Part of versus Knows of relationship
- Shared Aggregation is often called aggregation
- Aggregation can have role names and multiplicities
0..1
1
Car
Driver
currentDriver
23Shared Aggregation and Class Structure
- There are different ways to code the shared
aggregation relationship - IST 240 Method!
- Aggregations require an attribute
- Aggregations may/may not have set operations
for the attribute - Aggregations may/may not have get operations
for the attribute
24Source Code Example
public class Car private Road
currentRoad private int speedInMPH
private Driver currentDriver public void
setCurrentRoad(Road r) public Road
getCurrentRoad() public void
setDriver(Road r) public Road getDriver()
public void setSpeedInMPH (int
newSpeedInMPH) public void setSpeedInKPH
(int newSpeedInMPH) public int
getSpeedInMPH() public int
getSpeedInKPH()
Shared aggregation behaves like association. The
relationship gives rise to an attribute.
public class Driver private String name
private boolean isDriving public void
drive() public void stopDriving()
25Shared Aggregation
- Aggregation relationships can be bi-directional
- This is in the sense of awareness
- In the class diagram below
- Car is aware of at most one Driver
- Driver is aware of at most one Car
0..1
0..1
Car
Driver
currentDriver
26Source Code Example
public class Car private Road
currentRoad private int speedInMPH
private Driver currentDriver public void
setCurrentRoad(Road r) public Road
getCurrentRoad() public void
setDriver(Road r) public Driver getDriver()
public void setSpeedInMPH (int
newSpeedInMPH) public void setSpeedInKPH
(int newSpeedInMPH) public int
getSpeedInMPH() public int
getSpeedInKPH()
Bidirectional shared aggregation gives rise to
atttributes in both classes.
public class Driver private String name
private boolean isDriving private Car
currentCarl public void drive() public
void stopDriving() public void setCar(Car
car) public Car getCar()
27Composite Aggregation
- Composite Aggregation is stronger than Shared
Aggregation - Generally the composer exists longer than the
composee - This is not the case for Shared Aggregation
- Composite Aggregation is often called
composition - Compositions can have role names and
multiplicities
4
Car
Wheel
myWheels
28Composition and Class Structure
- There are different ways to code the composition
relationship - Must always ensure the target instance lives no
longer than the source - IST 240 Method
- Compositions require an attribute
- Compositions do not have set operations for the
attribute - Why?
- Compositions may have get operations for the
attribute - Need to do this carefully!
29Source Code Example
public class Car private Road
currentRoad private int speedInMPH
private Driver currentDriver private Wheel
wheels public Car() wheels new
Wheel4 for (int w0 wltwheels.length
w) wheelsw new Wheel()
public void setCurrentRoad(Road r)
public Road getCurrentRoad()
public void setDriver(Road r) public
Driver getDriver() public void
setSpeedInMPH (int newSpeedInMPH) public
void setSpeedInKPH (int newSpeedInMPH)
public int getSpeedInMPH() public int
getSpeedInKPH()
- Note that myWheels represents a reference to an
array object. - When an instance of Car is deleted, the myWheels
array should also deleted.
30Putting It Together
4
0..1
Wheel
Road
Car
0..1
Driver
31Remember the IST 240 Method
- Associations
- Source class will have an attribute, set
operation and perhaps a get operation - Composition
- Source class will have an attribute, no set
operation and perhaps a get operation
32Composition vs. Aggregation
- Composers cannot share instances of their
composees - Aggregators can share
0..1
Car
Owner
currentOwner
4
Car
Wheel
33Example
- Lets assume that a shirt has a collar, a single
pocket, and two sleeves - There are 12 buttons on the shirt
- 7 down the front,
- 2 for the collar
- 1 for each cuff
- 1 for the pocket
34Example
- Whats wrong with this diagram?
Shirt
12
2
1
1
2
Collar
Sleeve
Pocket
Button
1
1
35Example
- What can be said about this diagram?
Shirt
9
2
1
1
2
Collar
Sleeve
Pocket
Button
1
1
36IST 240 Method! Dont Use Shared Aggregation
- Shared aggregation can be modeled using an
association - Since it leads to confusion, it is typically not
used in modeling - We discuss it because it is important to
understand as it does appear
37Subclassing
- Classes can be subclassed
- The representation is an arrow with an open,
triangular head at the superclass - This is the mechanism in UML which supports
inheritance - Often referred to as generalization or
specialization
Vehicle
LandVehicle
AquaticVehicle
Sub
Boat
Tank
Auto
38Source Code Example
Vehicle
public class Vehicle . . . public
class AquaticVehicle extends Vehicle . .
. public class Sub extends AquaticVehicle
. . .
AquaticVehicle
Sub
39Specialization
- When specializing a class
- All public and protected attributes are visible
by the subclass - All public and protected operations can be
overridden - Using the leaf tag, operations can be indicated
that they are not to be overridden
BillardBall
init(in atPosition Position) void
leaf hitBy (in ball BillardBall)
void currentPosition() Position
leaf
40Source Code Example
BillardBall
init(in atPosition Position) void
leaf hitBy (in ball BillardBall)
void currentPosition() Position
leaf
public class BilliardBall private int
color private int number private Position
p public final void init(Position p)
public void hitBy(BilliardBall b) public
final Position getCurrentPosition()
41Example Alarm Clock
42Example Digital Alarm Clock