Class Diagram
Class Name
Example: Bike
If abstract class use italics: Vehicle
If interface, mentioned at the top: <<interface>>
Fields
public (+)
private (-)
protected (#)
Package Local (~)
Relationships
Association A ━━━━━━ B
A stores B as a field and vice versa
Aggregation: Whole ◇━━━━━━ Part
Logical grouping, Parts have independent lifecycle
It is confused with Association
Composition: Owner ◆━━━━━━ Part
Part cannot exist independently
Lifecycle is controlled by owner
Inheritance: Child ━━━extends━━━▷ Parent
Realization: Cat -------implements------▷ Animal
Dependency: A --------⇒ B
A uses B temporarily
Generally not shown in the diagrams
Relationship Strength
Dependency < Association < Aggregation < Composition
Practical Uses
Interface (Realization): -------implements------▷
Inheritance: ━━━extends━━━▷
Aggregation (Logical grouping, no ownership) ?????
Composition (Strong ownership of lifecycle): ◆━━━━━━
Association (if none of the above matches): ━━━━━━
Association
solid single line: A ━━━━━━ B
Bidirectional: (without arrow A ━━━━━━ B)
A can call B and B can call A
Bus and BusStop
Unidirectional: (with arrow A ━━━━━━> B)
A can call B but B cannot call A
User and Desktop
generally an attribute of the dependent class is an instance of the independent class.
Objects know each other, but lifecycles are independent
Bidirectional: Bus and BusStop
class Bus {
private List< BusStop > busStops;
void addStop (BusStop busStop ) {
busStops. add (busStop)
}
}
class BusStop {
private List< Bus > buses;
void addBus (Bus bus ) {
buses. add (bus);
}
}
Unidirectional: User and Desktop
class User {
private Desktop desktop;
}
HAS-A relationship
Aggregation
empty diamond: A ◇━━━━━━ B (unfilled diamond + solid line)
weak ownership/grouping (empty/weak diamond)
It is special type of association and confusing
It does not have defined semantics, hence need to understand the context
https://stackoverflow.com/questions/9640885/uml-aggregation-vs-association
Example:
Module ◇━━━━━━ Components
If Module does not exist Component can still exist
Components can be reused in other Modules
Composition
aka “Part of” relationship
filled diamond: A ◆━━━━━━ B (filled diamond + solid line)
strong ownership (full/strong diamond)
A owns B and controls its lifecycle
It is special type of Aggregation
Example:
HttpRequest ◆━━━━━━ Header
If HttpRequest instance is destroyed, Headers are also destroyed
Inheritance
aka “IS-A” relationship
denotes generalization/specialization relationship
“extends” in java
A ━━━━━━▷ B (sold line + triangle)
A is subclass of B and B is superclass of A
Realization
aka interface
“implements” in java
A -------------▷ B (dashed line + triangle)
mention <<interface>> at the top of the table name
A implements B, B is interface or abstract class
Dependency
A --------⇒ B (dashed line with arrow)
A depends on B, if there are changes in B, A will be affected
Dependency is weaker than association
generally the independent class is used in the dependent class as a
parameter variable of method
local variable inside the method
It is not stored as a field, hence the class is temporarily used but any change in contract may cause problems
https://stackoverflow.com/questions/1152335/does-an-association-imply-a-dependency-in-uml
I feel the confusing thing is that the term “dependency” has different meaning in general software engineering context compared to UML context
//@assoc The Player(A) has? some Dice(B)
class Player {
Dice myDice;
}
//@dep The Player(C) uses some Dice(D) when playing a game
class Player {
rollYahtzee (Dice someDice );
}
Multiplicity
How many instances of each class
Exact: a
Range: a…b
Anything: Asterisk (*) (0 to anything)
Examples:
one-to-one: A ━(1)━━━━━━(1)━ B
one-to-ranged: A ━(1…3)━━━━━━━(1)━ B
one-to-many: A ━(1)━━━━━━━(*)━ B
one-to-many with minimum 1: A ━(1)━━━━━━━(1…*)━ B
Role
What is the role/relationship in association
Example:
Guest ━━eats━━ Food
Cook ━━cooks━━ Food
Generally uses is used:
Examples
Bank Account
Notice: generally (but not necessarily) we have List<> to show aggregation or composition
If it is not “HAS-A” then probably it has association (“USES” ??) relationship
Java Collections
Notice: inheritance (extends) vs implementation (implements)