75
1 Design with GRASP Week 6 April 18, 2007

Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

1

Design with GRASP

Week 6April 18, 2007

Page 2: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

2

Agenda

Object design basicsMonopoly game design and GRASP principles introductionUse Case Realization using GRASPFrom Design to codeMonopoly Game Use Case Realization (self study material)

Page 3: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

3

Object Design

“Identify requirements, create a domain model, add methods to the software classes, define messages to meet requirements…”Too Simple!

What methods belong where?How do we assign responsibilities to classes?

The critical design tool for software development is a mind well educated in design principles and patterns.

Page 4: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

4

Object Design: inputPOS project inputs

Use case text of Process SaleDefining the behavior

System sequence diagramIdentifying the system operation messages

The operation contractStating events to design for, and detailed post-condition to satisfy

Supplementary specification Defines non-functional goals

GlossaryData format, data related with UI and database

Domain modelinitial attempt of software object in the domain layer of software architecture

Page 5: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

5

Object Design: activities and output

Design activitiesCoding immediately

Ideally using test-driven developmentShort UML modeling

Apply OO Design principles and design patternsDesign output

UML diagrams for difficult parts of the systemUI prototypes and database models

Page 6: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

6

Responsibility Driven DesignResponsibility is a contract or obligation of a classWhat must a class “know”? [knowing responsibility]

Private encapsulated dataRelated objectsThings it can derive or calculate

What must a class “do”? [doing responsibility] Take action (create an object, do a calculation)Initiate action in other objectsControl/coordinate actions in other objects

Responsibilities are assigned to classes of objects during object design

Page 7: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

7

Examples of responsibilities

“A Sale is responsible for creating SalesLineItems” (doing)“A Sale is responsible for knowing its total”(knowing)Knowing responsibilities are related to attributes, associations in the domain modelDoing responsibilities can be expressed at different granularities.Doing responsibilities are implemented by means of methods.

Page 8: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

8

Domain model and responsibilities

Domain model illustrates attributes and associations => inspires the “knowing”responsibilitiesResponsibilities are defined in conceptual levelMethods fulfill responsibilities

AloneThrough collaboration with other objects and methods

Page 9: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

9

Responsibilities and UML

: Sale

makePayment(cashTendered): Paymentcreate(cashTendered)

abstract, implies Sale objects have a responsibility to create Payments

Page 10: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

10

GRASP principles

General Responsibility Assignment Software PatternsThe five basic principles in Monopoly game design:

Creator Information ExpertHigh CohesionLow CouplingController

Page 11: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

11

Creator: problemProblem

Who creates the Square objectStart point: domain model

Page 12: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

12

Creator: principle

ProblemWho creates an A object

SolutionAssign class B the responsibility to create an instance of class A if one of these is true

B “contains” AB records AB closely uses AB has the Initializing data for A

Page 13: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

13

Creator: Solution

Page 14: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

14

Information ExpertProblem

Who knows about a Square object, given a key?Alternatively, who should be the receiver of message getSquare(name)

Board knows about all squaresBoard has the information necessary to fulfill this responsibility

Page 15: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

15

Information Expert: principle

ProblemWhat is a general principle of assigning responsibilities to objects

SolutionAssign a responsibility to the class that has the information needed to fulfill it

Page 16: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

16

Low coupling

ProblemHow to reduce the impact of change, to support low dependency, and increase reuse?

SolutionAssign a responsibility so that coupling remains low

Page 17: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

17

Coupling

How strongly one element is connected to, has knowledge of, or depends on other elementsIllustrated as dependency relationship in UML class diagram

A

E

BC

DF

Page 18: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

18

Example of poor design

Page 19: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

19

High cohesion

ProblemHow to keep objects focused, understandable, and manageable, and as a side effect, support Low Coupling?

SolutionAssign responsibilities so that cohesion remains high

Page 20: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

20

Cohesion

How strongly related and focused the responsibilities of an element areFormal definition (calculation) of cohesion

Cohesion of the two methods is defined as the intersection of the sets of instance variables that are used by the methodsIf an object has different methods performing different operations on the same set of instance variables, the class is cohesive

Page 21: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

21

Example

Page 22: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

22

Controller

ProblemWhat first object beyond the UI layer receives and coordinates (“controls”) a system operation

SolutionAssign the responsibility to an object representing one of these choices

Represents the overall system, root object, device or subsystemRepresents a use case scenario within which the system operations occurs

Page 23: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

23

Example

Page 24: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

24

Example

Page 25: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

25

Example

Page 26: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

26

Use-Case Realization

“…describes how a particular use case is realized within the design model, in terms of collaborating objects” [RUP]Individual scenarios are realized

Use case -> System events -> System sequence diagram ->System operation contracts -> Interaction diagrams -> Design classes

Page 27: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

27

System operation

The system operations in the SSD are used as the start messages into the domain layerIf communication diagrams are used, one per system operationSame for sequence diagram

:RegisterenterItem

:RegisterendSale

:RegistermakePayment

1: ???

1: ???

1: ???

:RegistermakeNewSale 1: ???

makeNewSale, etc., are the system operations from the SSD

each major interaction diagram starts with a system operation going into a domain layer controller object, such as Register

DOMAIN LAYERUI LAYER

Window objects or

GUI widget objectsor

Web control objects

. . .

Page 28: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

28

One diagram per system operation

: Register

: Sale

makeNewSalecreate

: Register

enterItem(...)

: ProductCatalog

desc = getProductDesc( itemID )

. . .

UI LAYER

Window objects or

GUI widget objectsor

Web control objects

. . .

DOMAIN LAYER

Page 29: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

29

Design makeNewSale

Choosing the controller classA façade controller is satisfactory if there are only a few system operationsUse Register here.

Creating a new SaleRegister create SaleSale create a collection to store SalesLineItems

Page 30: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

30

Design makeNewSale

:Register

makeNewSale

:Salecreate

Register creates a Sale by Creator

create lineItems :List<SalesLineItem>

by Creator, Sale creates an empty collection (such as a List) which will eventually hold SalesLineItem instances

by Creator and Controller

this execution specification is implied to be within the constructor of the Sale instance

Page 31: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

31

Design: enterItem

[Larman 2002]

Page 32: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

32

Design enterItem

Choosing controller classDisplay item description and price (ignore at this stage)Create a new SalesLineItemFinding a ProductDescriptionVisibility to ProductCatalogTemporary and persistent storage

We can defer database design and use temporary memory object instead

Page 33: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

33

Design enterItem

2: makeLineItem(desc, qty)enterItem(id, qty)

1: desc = getProductDesc(id) 2.1: create(desc, qty)

1.1: desc = get(id)

:Register :Sale

:ProductCatalog

sl: SalesLineItem

lineItems : List<SalesLineItem>: Map<ProductDescription>

2.2: add(sl)

by Expert

by Controllerby Creator

add the newly created SalesLineItem instance to the List

Page 34: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

34

Partial Design Class Diagram

SalesLineItem

quantity : Integer

...

ProductCatalog

...

getProductDesc(...)

ProductDescription

description : Textprice : MoneyitemID: ItemID

...

1..*

1..*

Register

...

enterItem(...)...

Sale

isComplete : Booleantime : DateTime

makeLineItem(...)...1

1

1

catalog

currentSale

descriptions{Map}

lineItems{ordered}

description

Page 35: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

35

Design endSale

Who should be responsible for knowing the grand total of a saleWhat information do we need to compute the grand total

SalesLineItem

Sale

time

SalesLineItem

quantity

ProductDescription

descriptionpriceitemID

Described-by*

Contains

1..*

1

1

Page 36: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

36

Create diagrams in parallel

Sale

time...

getTotal()

:Salet = getTotal

New method

1. The new responsibility is conveyed by an operation in aninteraction diagram. A new method is created in the designclass to express this.

Page 37: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

37

Create diagram in parallel

Sale

time...

getTotal()

SalesLineItem

quantity

getSubtotal()New method

1 *: st = getSubtotal: Salet = getTotal lineItems[ i ] : SalesLineItem

this notation will imply we are iterating over all elements of a collection

2. Responsibility for each subtotal is assigned to theSalesLineItem

Page 38: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

38

Create diagrams in parallelSale

time...

getTotal()

SalesLineItem

quantity

getSubtotal()

ProductDescription

descriptionpriceitemID

getPrice()New method

:ProductDescription

1.1: p := getPrice()

1 *: st = getSubtotal: Salet = getTotal lineItems[ i ] :SalesLineItem

3. Responsibility for each item price is assignedto the ProductSpecification

Page 39: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

39

Design makePayment

[Larman 2002]

Page 40: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

40

Design makePaymentCreating Payment instanceHow should we assignment responsibilities of creating Payment instance and associate it with the Sale.

Page 41: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

41

Options

: Register p : Payment

:Sale

makePayment() 1: create()

2: addPayment(p)

: Register :Sale

:Payment

makePayment() 1: makePayment()

1.1. create()

Page 42: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

42

Final Solution

Creating payment

1: makePayment(cashTendered)

1.1: create(cashTendered)

:Register :Sale

:Payment

makePayment(cashTendered)

by Controller by Creator and Low Coupling

Page 43: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

43

Design makePayment

Logging a sale

Store

...

addSale(s : Sale)...

SalesLedger

...

addSale(s : Sale)...

Store is responsible for knowing and adding completed Sales.

Acceptable in early development cycles if the Store has few responsibilities.

SalesLedger is responsible for knowing and adding completed Sales.

Suitable when the design grows and the Store becomes uncohesive.

Sale

...

...

Sale

...

...

Logs-completed5 Logs-completed5* *

1 1

Page 44: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

44

Design makePayment

1: makePayment(cashTendered)

1.1: create(cashTendered)

:Register s :Sale

:Payment

makePayment(cashTendered)

:Store

2: addSale(s)

completedSales: List<Sale>

2.1: add(s)

by Expert

note that the Sale instance is named's' so that it can be referenced as a parameter in messages 2 and 2.1

Page 45: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

45

Design makePayment

Calculating the balanceWho is responsible for knowing the balance?

To calculate, sale total and payment tendered are requiredSale and Payment are partial Experts

Consider coupling…Sale already has visibility into PaymentPayment does not have visibility into SaleGiving Sale primary responsibility doesn’t increase overall coupling

Page 46: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

46

getBalance design

s :Sale pmt: Payment1: amt = getAmountbal = getBalance

2: t = getTotal

{ bal = pmt.amount - s.total }

Page 47: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

47

From UI to Domain Layer

Which class of object should be responsible for receiving this system event message?

It is sometimes called the controller or coordinator. It does not normally do the work, but delegates it to other objects.

The controller is a kind of "facade" onto the domain layer from the interface layer.

actionPerformed( actionEvent )

: ???

: Cashier

:SaleJFrame

presses button

enterItem(itemID, qty)

UI Layer

Domain Layer

system operation message

Page 48: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

48

Controller Choices

Represents the overall “system”, “root object”, device or subsystem

RegisterRepresents a receiver or handler of all system events of a user case scenario

:RegisterenterItem(id, quantity)

:ProcessSaleHandlerenterItem(id, quantity)

Page 49: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

49

Allocate system operationsRegister

...

endSale()enterItem()makeNewSale()makePayment()

makeNewReturn()enterReturnItem(). . .

System

endSale()enterItem()makeNewSale()makePayment()

makeNewReturn()enterReturnItem(). . .

system operations discovered during system behavior analysis

allocation of system operations during design, using one facade controller

ProcessSaleHandler

...

endSale()enterItem()makeNewSale()makePayment()

System

endSale()enterItem()makeNewSale()makePayment()

enterReturnItem()makeNewReturn(). . .

allocation of system operations during design, using several use case controllers

HandleReturnsHandler

...

enterReturnItem()makeNewReturn(). . .

Page 50: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

50

Choosing a Controller

Façade controllerPRO: all events handled in one placeCON: can become too bloated

Use-case controllerPRO: manage many different events across different processesCON: adds additional classes; events can’t be managed (e.g. logged) in one place

Page 51: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

51

Desirable coupling of UI layer to domain layer

actionPerformed( actionEvent )

:Register

: Cashier

:SaleJFrame

presses button

1: enterItem(itemID, qty)

:Sale1.1: makeLineItem(itemID, qty)

UI Layer

Domain Layer

system operation message

controller

SalesJFrame needsto have a reference to Register object and will send message to it.

Page 52: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

52

Less desirable coupling

Cashier

:SaleJFrame

actionPerformed( actionEvent )

:Sale1: makeLineItem(itemID, qty)

UI Layer

Domain Layer

It is undesirable for an interfacelayer object such as a window to get involved in deciding how to handle domain processes.

Business logic is embedded in the presentation layer, which is not useful.

SaleJFrame should not send this message.

presses button

Page 53: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

53

Object Design: startUpInitial system operationDelay until all other system operations have been considered

What objects need to be there through out the use caseWhat associations need to be there through out the use case

Create a set of domain objects that need to be there to support the use case executionFind a suitable initial object and request that object to create a set of other objectsDo the initialization design last

Page 54: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

54

Create Initial Domain Object

[Larman 2003]

Page 55: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

55

Designing Store.create()

Create: Store, Register, ProductCatalog,ProductDescriptionAssociate: ProductCatalog with ProductDescription; Store with ProductCatalog; Store with Register; Register with ProductCatalog

Page 56: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

56

Designing Store.create()

:Store :Register

pc:ProductCatalog

create 2: create(pc)

1: create

1.2: loadProdSpecs()

descriptions:Map<ProductDescription>

1.1: create

1.2.2*: put(id, pd)

1.2.1*: create(id, price, description)

pd:ProductDescriptionthe * in sequence number indicates the

message occurs in a repeating section

pass a reference to the ProductCatalog to the Register, so that it has permanent visibility to it

by Creator create an empty collection object

Page 57: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

57

Mapping Designs to Code

Class and interface definitionMethod definitions

Page 58: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

58

Creating Class Definition from DCD

public class SalesLineItem{private int quantity;

private ProductDescription description;

public SalesLineItem(ProductDescription desc, int qty) { ... }

public Money getSubtotal() { ... }

}

SalesLineItem

quantity : Integer

getSubtotal() : Money

ProductDescription

description : Textprice : MoneyitemID : ItemID

...

1

description

Page 59: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

59

Creating methods from interaction diagram

2: makeLineItem(desc, qty)enterItem(id, qty)

1: desc = getProductDesc(id) 2.1: create(desc, qty)

1.1: desc = get(id)

:Register :Sale

:ProductCatalog

sl: SalesLineItem

lineItems : List<SalesLineItem>: Map<ProductDescription>

2.2: add(sl)

Page 60: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

60

The Register class

ProductCatalog

...

getProductDesc(...)

Sale

isComplete : Booleantime : DateTime

becomeComplete()makeLineItem(...)makePayment(...)getTotal()

Register

...

endSale()enterItem(id: ItemID, qty : Integer)makeNewSale()makePayment(cashTendered : Money)

public class Register{private ProductCatalog catalog;private Sale currentSale;

public Register(ProductCatalog pc) {...}

public void endSale() {...}public void enterItem(ItemID id, int qty) {...}public void makeNewSale() {...}public void makePayment(Money cashTendered) {...}}

1

1

catalog

currentSale

Page 61: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

61

The enterItem method

2: makeLineItem(desc, qty)enterItem(id, qty)

1: desc := getProductDescription(id)

:Register :Sale

:ProductCatalog

{ ProductDescription desc = catalog.ProductDescription(id); currentSale.makeLineItem(desc, qty);}

Page 62: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

62

Collection classes in code

SalesLineItem

quantity : Integer

getSubtotal()1..*

Sale

isComplete : Booleantime : DateTime

becomeComplete()makeLineItem()makePayment()getTtotal()

public class Sale{...

private List lineItems = new ArrayList();}

A collection class is necessary to maintain attribute visibility to all the SalesLineItems.

lineItems

Page 63: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

63

Defining the Sale.makeLineItemMethod

{ lineItems.add( new SalesLineItem(desc, qty) );}

2: makeLineItem(desc, qty)enterItem(id, qty)

2.1: create(desc, qty)

:Register :Sale

sl: SalesLineItemlineItems :List<SalesLineItem>

2.2: add(sl)

Page 64: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

64

Order of implementation

SalesLineItem

quantity : Integer

getSubtotal()

ProductCatalog

...

getProductDesc(...)

ProductDescription

description : Textprice : MoneyitemID : ItemID

...

Store

address : Addressname : Text

addSale(...)

Payment

amount : Money

...

1..*

1..*

Register

...

endSale()enterItem(...)makeNewSale()makePayment(...)

Sale

isComplete : Booleantime : DateTime

becomeComplete()makeLineItem(...)makePayment(...)getTotal()...

1

1

1

1

1

1

* 1

23

4

56

7

From least-coupled to most-coupled

Page 65: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

65

Monopoly game project

Two system operations in the first iteration

InitializeIgnore at this stage

Play gameMain focus

Page 66: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

66

Design playGame

Choosing the Controller Class

Represents the overall “system”, “root object”

MonopolyGame? *Represents a receiver or handler of all system events of a use case scenario

PlayGameHandler?

Page 67: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

67

Game-Loop algorithm

Terminologyturn – a player rolling the dice and moving the pieceround – all the players taking one turn

Game loopfor N rounds

for each player pp takes a turn

Page 68: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

68

Who is responsible for controlling the game loop

No object has it yet, assigning this to the MonopolyGame object is justifiable

The current round count

MonopolyGame objectAll players

Who has this informationInformation needed

Page 69: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

69

Who takes a turn?When there are multiple partial information experts to choose from

Place the responsibility in the dominant information expertIf the first guideline does not apply, consider the coupling and cohesion impact of each and choose the bestConsider possible future evolution

Taking a turn might involve buying property, deposit money in bank.. Etc. A player should have all information regarding those activities

BoardAll the squares

MonopolyGameTwo die objects

PlayerCurrent location of the player

Who has the informationInformation Needed

Page 70: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

70

Partial design diagrams

Page 71: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

71

Taking a turn refine

Taking a turn involvesCalculating the face value of two dieCalculating the new square locationMoving the player’s piece from an old location to a new location

Who coordinate all thisPlayer

Visibility problemPlayer must have references to all those objects

Page 72: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

72

Taking a turn

Page 73: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

73

Command-Query Separation Principle

Style 1#public void roll(){

faceValue = //random num generation}public int getFaceValue(){

return faceValue;}

Style 2#public int roll(){

faceValue = //random num generationreturn faceValue;

}

Every method should either be:

A command method that performs an action often has side effects such as changing the state of objects and is void. A query that returns data to the caller and has no side effects – it should not permanently change the state of any objects.

But a method should not be both

Page 74: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

74

Static design for playGame

Page 75: Design with GRASP - LUMSsuraj.lums.edu.pk/~cs463s06/lectures/W7_GRASP.pdf · Use-Case Realization “…describes how a particular use case is realized within the design model, in

COMP5028 Object-Oriented Analysis and Design (S1 2007)© Dr. Ying Zhou, School of IT, The University of Sydney

75

Initialization and the start up use case