02 UML Classes
02 UML Classes
One-to-many
Inventory has many items, items know 1 inventory
Aggregation and Composition
A special kind of association
Models whole-part relationship between things
Whole is usually referred to as composite
Composite aggregation
Also referred to as composition
Composite solely owns the part and they are in
a tree structure parts hierarchy
Most common form of aggregation
In UML, represented by filled diamond
Hand Finger
1 0..7
Shared Aggregation
Part may be in many composite instances
In UML, represented as hollow diamond
Network
Node Arc
How to identify aggregation
Lifetime of part is bound within lifetime of
composite
There is a create-delete dependency
There is an obvious whole-part physical or
logical assembly
Some properties of composite propagate to
parts (e.g., location)
Operations applied to composite propagate to
parts (e.g., destruction, movement, recording)
Why show aggregation
Clarifies domain constraints regarding part-
whole relationship
Assists in identification of a creator
Operations applied to whole should usually
propagate to parts
Identifying whole wrt a part supports
encapsulation
Dependency
Denotes dependence between classes
Always directed (Class A depends on B)
Represented by dotted line with arrowhead
A B
A depends on B
Dependency
Caused by class methods
Method in Class A temporarily “uses a” object
of type Class B
Change in Class B may affect class A
A B
Class A { Class B {
B Foo(B x) { …
B y = new B(); …
return y; …
} } }
Dependency Example
A «B»
A implements interface B
UML Examples
Read UML class diagram
Try to understand relationships
Examples
Pets & owners
Computer disk organization
Banking system
Home heating system
Printing system
UML Example – Veterinary System
Try to read & understand UML diagram
UML Example – Veterinary System
Try to read & understand UML diagram
Java
UML Java : Veterinary System
UML
Java
class Pet {
PetOwner myOwner; // 1 owner for each pet
}
class PetOwner {
Pet [ ] myPets; // multiple pets for each owner
}
Java UML : Veterinary System
Java
class Pet {
PetOwner myOwner; // 1 owner for each pet
}
class PetOwner {
Pet [ ] myPets; // multiple pets for each owner
}
UML
Java UML : Veterinary System
Java
class Pet {
PetOwner myOwner; // 1 owner for each pet
}
class PetOwner {
Pet [ ] myPets; // multiple pets for each owner
}
UML
UML Class Diagrams Java
UML
Java
class Pet {
PetOwner myOwner; // 1 owner for each pet
}
class PetOwner {
Pet [ ] myPets; // multiple pets for each owner
}
UML Java : Computer System
UML
Java
UML Java : Computer System
UML
Java
class Controller {
}
class SCSIController extends Controller {
}
UML Java : Computer System
UML
Java
Design code using all available information in UML…
UML Java : Computer System
Java
class CPU {
Controller [ ] myCtlrs;
}
class Controller {
CPU myCPU;
}
class SCSIController extends Controller {
DiskDrive [ ] myDrives = new DiskDrive[4];
}
Class DiskDrive {
SCSIController mySCSI;
}
Java UML : Printing System
Java
class Registry {
PrintQueue findQueue();
}
class PrintQueue {
List printJobs;
Printer myPrinter;
Registry myRegistry;
void newJob();
int length();
Resources getResource();
}
Java UML : Printing System
Java
Class Printer {
Resources myResources;
Job curJob;
void print();
boolean busy();
boolean on();
}
class Job {
Job(Registry r) {
…
}
}
Java UML : Printing System
UML Summary
Graphics modeling language
Visually represents design of software system
Focus: Class diagrams
Contents of a class
Relationship between classes
You should be able to
Draw UML class diagram given code
Write code given UML class diagram