M2.2.UML ClassDiagrams
M2.2.UML ClassDiagrams
(Class Diagrams)
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 1!
Class Diagrams
• Class diagrams represent the structure of the system
• Used
− during requirements analysis to model application domain
concepts
− during system design to model subsystems
− during object design to specify the detailed behavior and
attributes of classes.
TariffSchedule! Trip!
Table zone2price!
zone:Zone!
Enumeration getZones()!
Price getPrice(Zone)!
*" *" price: Price!
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 2!
Classes
Type" TariffSchedule!
Table zone2price!
Enumeration getZones()!
Name" Price getPrice(Zone)!
TariffSchedule!
zone2price! Attributes" Signature"
getZones()!
getPrice()!
Operations" TariffSchedule!
• A class represents a concept
• A class encapsulates state (attributes) and behavior (operations)
Each attribute has a type
Each operation has a signature
The class name is the only mandatory information
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 3!
Instances
tariff2006:TariffSchedule! :TariffSchedule!
zone2price = {! zone2price = {!
{‘1’, 0.20}, {‘1’, 0.20},
{‘2’, 0.40},! {‘2’, 0.40},!
{‘3’, 0.60}}! {‘3’, 0.60}}!
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 4!
Class vs. Object
• Class
− An abstraction modeling an entity in the application or
solution domain
− The class is part of the system model ( “Passenger”,
“Ticket distributor”, “Server”, “TariffSchedule”)
• Object
− A specific instance of a class (“Joe, the passenger who
is purchasing a ticket from the ticket distributor”).
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 5!
Associations
TarifSchedule! TripLeg!
!
Enumeration getZones()! Price
Price getPrice(Zone)!
*" *" Zone!
!
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 6!
1-to-1 and 1-to-many Associations
Country! 1 ! 1 ! CapitalCity!
name:String" name:String"
1-to-1 association
Point!
Polygon!
*! x: Integer"
y: Integer"
draw()"
1-to-many association
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 7!
Many-to-Many Associations
Company
StockExchange *! *! tickerSymbol
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 8!
Model-Driven Software Development
StockExchange *! *! Company
Lists tickerSymbol
Class Diagram:!
StockExchange *! *! Company
Lists tickerSymbol
Muffler! Tailpipe!
diameter" diameter"
A solid diamond denotes composition: A strong form of aggregation where
the life time of the component instances is controlled by the aggregate.
That is, the parts don’t exist on their own (“the whole controls/destroys
the parts”)
TicketMachine!
3"
ZoneButton!
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 11!
Inheritance
Button!
CancelButton! ZoneButton!
Bank" Customer"
Foo!
Amount!
CustomerId!
Deposit()!
Withdraw()!
GetBalance()!
“Dada”! Foo!
Amount! Amount!
CustomerId! CustomerId!
Deposit()! Deposit()!
Withdraw()! Withdraw()!
GetBalance()! GetBalance()!
Account!
Amount!
CustomerId!
Deposit()!
! Withdraw()!
Is Foo the right name? GetBalance()!
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 15!
Object Modeling in Practice: More classes
Account!
Amount! Customer!
Bank! AccountId!
Name!
Name! Deposit()! CustomerId!
Withdraw()!
GetBalance()!
Account!
?! *! Amount!
*! owns!
Customer!
Bank! has! AccountId!
2! Name!
Name! Deposit()! CustomerId!
Withdraw()!
GetBalance()!
Account!
Bank! Customer!
Name!
*! Amount! *! Has! Name!
AccountId!
CustomerId!
Deposit()!
Withdraw()!
GetBalance()! CustomerId()!
Withdraw()!
Bernd Bruegge & Allen H. Dutoit
! Withdraw()!
! Object-Oriented Software Engineering: Using UML,Withdraw()!
Patterns, and Java 18!
Practice Object Modeling: Simplify, Organize
Account!
Amount!
Show Taxonomies!
AccountId! separately!
Deposit()!
Withdraw()!
GetBalance()!
Withdraw()!
Bernd Bruegge & Allen H. Dutoit
! Withdraw()!
! Object-Oriented Software Engineering: Using UML,Withdraw()!
Patterns, and Java 19!
Exercise: Class diagrams
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 20!
Exercise: Class diagrams
Association"
Class"
Multiplicity" SimpleWatch
1 1 1 1
2 1 1 1
PushButton Display Battery Time
Association"
Class"
Multiplicity"
Watch"
1" 1" 1" 1"
2"
1" 1" 1"
PushButton!
!state! LCDDisplay! Battery! Time!
push() blinkIdx! Load" Now"
release()" blinkSeconds()!
blinkMinutes()!
blinkHours()!
stopBlinking()!
Attribute" ! referesh()" Operations"
Bernd Bruegge & Allen H. Dutoit ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 22!