Design 2
Design 2
Aditya P. Mathur
Static structure
diagrams
Interaction Diagrams:
Collaboration diagrams
Sequence diagrams
Design state model
Contracts for
State diagrams
methods and operations
for classes
1: message2()
2: message3()
message1()
:classAinstance :classBinstance
:classAinstance :classBinstance
message1()
message2()
message3()
Focus of control,
activation box, optional May be used for multiple events.
October 2, 2001 Software Design 6
Collaboration diagram: makepayment()
makePayment(cashTendered) 1: makePayment(cashTendered)
:POST :Sale
link line
instance of a class :Payment
Note: Post is Register in the parameter
second edition of Larman.
October 2, 2001 Software Design 7
Making collaboration diagrams
msg1()
addPayment(cashTendered)
:Post :Sale
link line
msg1() 1: message1()
2: message2()
3: message3()
:Post :Sale
msg1()
1: addPayment(amount:Money)
:Post :Sale
1: tot:=total(): int
:Post :Sale
msg1()
:Post
1: clear()
msg1()
msg1()
iteration clause
msg1()
{
for i:= 1 to 10
equal iteration clauses {
myB.msg2();
myC.meg3();
msg1() }
}
1*: [i:= 1..10] msg2()
:A :myB: B
2*: [i:= 1..10] msg3()
:myC: C
October 2, 2001 Software Design 17
Creation
1: create(cashier)
:Post :Sale
<<create>>
1: make(cashier)
create message with optional parameter for
initialization. Note that use of the name
create is a UML convention.
1: msg2()
:classA :classB
1.1: msg3()
2.1: msg5()
2: msg4()
:classC
fifth (nested)
fourth
October 2, 2001 Software Design 20
Conditional message
msg1()
1.1: create()
conditional message with test
:SalesLineItem
1b.1: msg5()
:ClassD :ClassC
sales:Sale
Message sent to a
Multiobject.
msg1()
Message sent to an
element.
msg1()
1: el:=create()
:Sale sl:SalesLineItem
2: addElement(el)
:SalesLineItem
Message sent to a
collection.
October 2, 2001 Software Design 25
Design Guidelines
GRASP:
General Responsibility Assignment Software
Patterns
Question:
How does one assign responsibilities ?
Answer:
Assign a responsibility to an information
expert, i.e. to a class that has the information
needed to fulfill that responsibility.
1: t:=getTotal()
:Sale
Sale
date New method added to
the Sale class. The class itself is
time derived from the domain model.
getTotal
We need:
Quantity of each SalesLineItem and its price.
Quantity is available with SalesLineItem and price
with ProductSpecification.
Sale
*
date
time :SalesLineItem
getTotal() 2.1: p:=getPrice()
SalesLineItem ProductSpecification
:ProductSpecification
quantity description
price
getSubtotal() itemID
getPrice()
October 2, 2001 Software Design 35
Summary: Example [4]
Class Responsibility
Question:
Who should be responsible for creating an
instance of a class ?
Answer:
Assign to B the responsibility to create an
object of class A if the following is true:
:POST :Sale
makeLineItem(quantity)
create(quantity)
:SalesLineItem
makeLineItem(quantity)
:Sale
1: create(quantity)
Sale :SalesLineItem
date
time
makeLineItem()
total() New method added to
the Sale class.
Question:
Who should be responsible for handling
system events ?
Recall that a system event is a high level event, an external
event, generated by a system actor.
Answer:
Assign a responsibility to handle a system
event to a controller.
System
endSale()
enterItem()
makePayment()
enterItem(upc,quantity)
:???
:BuyItemsHandler
October 2, 2001 Software Design 52
Controller: Example [2]
System POST
endSale() …...
enterItem() endSale()
makePayment() enterItem()
makePayment()
System operations
Allocation of system
discovered during
operations during design
analysis
Use the same controller class for events of one use case. This
allows maintenance of the state of a use case.
ReservationAgent MakeAReservationHandler
Scheduler ManageSchdulesHandler
FareAnalyst ManageFaresHandler
October 2, 2001 Software Design 58
Presentation (Interface) Layer [1]
Example:
Assume that POS application has a window that
displays sale information and captures cashier’s
operations. Suppose that a Java applet displays the
window. Let us see how the presentation and domain
layers can be coupled.
October 2, 2001 Software Design 59
Sample GUI for Point of Sale Terminal
Object Store _ x
UPC 1 Quantity 5
Price 2 Description 6
Total 3 Balance 7
Cash 4
Actor System
Actor System
6 .......
Cash Balance
Presses button Enter Item End Sale Make Payment
1.1: makeLineItem(upc,quantity)
Controller
:POST :Sale
Object Store _ x
Cashier
UPC Quantity
Cash
Cash Balance
Presses button Enter Item End Sale Make Payment
onEnterItem()
Business logic embedded in
presentation layer. :SaleJFrame
Presentation layer
1: makeLineItem(upc,quantity)
:Sale
Domain layer
October 2, 2001 Software Design 65
Presentation layer [5]
makePayment()
1: create()
:POST p:Payment
2: addPayment(p)
:Sale
October 2, 2001 Software Design 70
Low coupling: Example [3]
makePayment()
1: makePayment()
:POST :Sale
1.1: create()
Question:
How to keep design complexity manageable.
Answer:
Assign responsibilities while maintaining high
cohesion.
Buy items
• enterItem
• endSale
• makePayment
Start up
• startUp
Choices:
POST or a new class such as RetailSystem:
represents the entire system.
Store: represents the business.
Cashier: represents a real-world entity.
BuyItemsHandler: represents an artificial handler.
We select POST as there are only a few system
operations.
1. Choices:
POST or a new class such as RetailSystem:
represents the entire system.
enterItem()
:POST
endSale()
We select the POST
:POST class to handle system
events.
makePayment()
:POST
startUp
:POST
October 2, 2001 Software Design 80
Interaction diagrams and contracts
Example:
The enterItem() system operation requires a Sale to be
created in its contract.
enterItem(upc,quantity)
Name: enterItem
(upc: number, quantity: int)
Responsibilities: Enter sale of an item and
add it to the sale. Display the
item description and price.
Type: System
Cross reference: Use cases: Buy items
Exceptions: If the UPC is not valid, indicate
that it was an error.
Post-conditions:
enterItem(upc,quantity)
:POST
1.1: create()
by controller
by creator
:SalesLineItem
2: spec:=specification(upc)
1.1: create()
3.1 create(spec,qty)
:Product
by expert 3.2: add(s))
Catalog
2.1: spec:=find(upc) :
:SalesLineItem
:
:SalesLineItem
sl:SalesLineItem
Name: endSale()
endSale() 1: saleComplete()
:POST :Sale
by controller
by expert
by expert
tot:=total() 2: st:=subtotal()
:Sale :sli:SalesLineItem
Sale--total() :
{ :SalesLineItem
total:=0;
for each SalesLineItem.sli
tot:=tot+sli.subtotal(); prodSpec: Product
return tot; specification
}
October 2, 2001 Software Design 102
Collaboration diagram: makePayment (1)
Name: makePayment(amount:float)
Type: System
Cross reference: Use cases: Buy items
Exceptions: If a sale is not complete,
indicate an error.
Pre-condition: None.
Post-conditions:
A Payment was created (instance creation)
Payment.tendered is set to amount (attribute modification).
The Payment was associated with the Sale (relationship formed).
The Sale was associated with the Store to add it to the historical
log of completed sales (relationship formed).
by controller 1.1create(cashTendered)
:Payment
bal:=balance() 1: amt:=amount()
:Sale :Payment
makePayment(cashTendered)
1: makePayment(cashTendered)
:POST s:Sale
2:addSale(s)
by expert 1.1create(cashTendered)
:Store
:Payment
2.1:add(s)
:
completedSales:Sale
October 2, 2001 Software Design 111
Collaboration diagram: startUp() (1)
Name: startUp()
Type: System
Post-conditions:
Store, POST, ProductCatalog, and ProductSpecifications
created (instance creation).
ProductCatalog associated with ProductSpecifications
Post-conditions:
Store, POST, ProductCatalog, and
ProductSpecifications created (instance creation).
ProductCatalog associated with ProductSpecifications
Store associated with POST (association formed).
POST associated with ProductCatalog (association
formed).
create() 2: create(pc)
:Store :POST
1.1: create()
1:create()
by creator 1.2.2*: add(ps)
ProductSpecification
ProductSpecification
pc:ProductCatalog
1.2.1*: create(upc,price,description)
1.1: loadProdSpec ps:productSpecification
October 2, 2001 Software Design 116
Connecting presentation and domain layers(1)
1: create()
2: p=getPOST():POST
create()
:POSTApplet store: Store
Object Store _ x
Cashier
UPC Quantity
Cash
Cash Balance
Presses button Enter Item End Sale Make Payment
onEnterItem()
Presentation layer
:PostApplet
1:enterItem(upc,qty)
Domain layer
:POST
Object Store _ x
Cashier
UPC Quantity
Cash
Cash Balance
Presses button Enter Item End Sale Make Payment
onEnterItem()
Presentation layer 3: t:=total(): float
:PostApplet
1:enterItem(upc,qty)
2: [no sale] sale:=getSale():Sale
Domain layer
post:POST sale:Sale
A class