Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Modeling with UML

(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}}!

• An instance represents a phenomenon


• The attributes are represented with their values
• The name of an instance is underlined
• The name can contain only the class name of the instance
(anonymous instance)

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!
!

Associations denote relationships between classes.


The multiplicity of an association end denotes how many
objects the instance of a class can legitimately reference.

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

Reality: A stock exchange lists many companies. Each


company is identified by a ticker symbol

Analysis results in analysis object model (UML Class Diagram):

StockExchange *! *! Company
Lists tickerSymbol

Implementation results in source code (Java):


public class StockExchange {
public Vector m_Company = new Vector();
};
public class Company {
public int m_tickerSymbol;
public Vector m_StockExchange = new Vector();
};
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 9!
From Problem Statement to Code
Problem Statement : A stock exchange lists many companies.
Each company is identified by a ticker symbol

Class Diagram:!
StockExchange *! *! Company
Lists tickerSymbol

Java Code! public class StockExchange


{ Associations!
private Vector m_Company = new Vector(); are mapped to !
};
Attributes!!
public class Company
{
public int m_tickerSymbol;
private Vector m_StockExchange = new Vector();
};
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 10!
Aggregation
• An aggregation is a special case of association denoting a
“consists-of” hierarchy Exhaust system!

• The aggregate is the parent class,


the components are the children classes 1" 0..2"

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!

• Inheritance is another special case of an association


denoting a “kind-of” hierarchy
• Inheritance simplifies the analysis model by introducing a
taxonomy
• The children classes inherit the attributes and operations of
the parent class.
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 12!
Packages
• Packages help you to organize UML models to increase their
readability
• We can use the UML package mechanism to organize classes
into subsystems
Account"

Bank" Customer"

• Any complex system can be decomposed into subsystems, where


each subsystem is modeled as a package.
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 13!
Object Modeling in Practice

Foo!

Amount!
CustomerId!

Deposit()!
Withdraw()!
GetBalance()!

Class Identification: Name of Class, Attributes and Methods

Is Foo the right name?


Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 14!
Object Modeling in Practice: Brainstorming

“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()!

1) Find New Classes!

2) Review Names, Attributes and Methods!


Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 16!
Object Modeling in Practice: Associations

Account!

?! *! Amount!
*! owns!
Customer!
Bank! has! AccountId!
2! Name!
Name! Deposit()! CustomerId!
Withdraw()!
GetBalance()!

1) Find New Classes!


2) Review Names, Attributes and Methods!
3) Find Associations between Classes!
4) Label the generic associations!
5) Determine the multiplicity of the associations!
Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 17!
6) Review associations!
Practice Object Modeling: Find Taxonomies

Account!
Bank! Customer!

Name!
*! Amount! *! Has! Name!
AccountId!
CustomerId!
Deposit()!
Withdraw()!
GetBalance()! CustomerId()!

Savings! Checking! Mortgage!


Account! Account! Account!

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()!

Savings! Checking! Mortgage!


Account! Account! Account!

Withdraw()!
Bernd Bruegge & Allen H. Dutoit
! Withdraw()!
! Object-Oriented Software Engineering: Using UML,Withdraw()!
Patterns, and Java 19!
Exercise: Class diagrams

• Draw a class diagram for a SimpleWatch that has 2


push buttons to set the time, a LCD display to view
the time, has a battery, and shows current time.

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

Class diagrams represent the structure of the system"


Bernd Bruegge & Allen H. Dutoit ! ! Object-Oriented Software Engineering: Using UML, Patterns, and Java 21!
Exercise: Class diagrams
Class diagrams represent the structure of the system"

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!

You might also like