principles of software construction: objects, design and...

29
toad © 2012-14 C Kästner, C Garrod, J Aldrich, and W Scherlis School of Computer Science Principles of Software Construction: Objects, Design and Concurrency Object-Oriented Design: Assigning Responsibilities Christian Kästner Charlie Garrod 15-214 With slides from Klaus Ostermann

Upload: dangthuy

Post on 19-Mar-2018

216 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad

© 2012-14 C Kästner, C Garrod, J Aldrich, and W Scherlis

School of Computer Science

Principles of Software Construction:

Objects, Design and Concurrency

Object-Oriented Design:

Assigning Responsibilities

Christian Kästner Charlie Garrod

15-214

With slides from Klaus Ostermann

Page 2: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 3 15-214 Kästner

Object-Oriented Design

•“After identifying your requirements and creating a domain model, then add methods to the software classes, and define the messaging between the objects to fulfill the requirements.”

•But how? How should concepts be implemented by classes? What method belongs where? How should the objects interact? This is a critical, important, and non-trivial task

Page 3: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 4 15-214 Kästner

Responsibilities

• Responsibilities are related to the obligations of an object in terms of its behavior.

• Two types of responsibilities: knowing doing

• Doing responsibilities of an object include: doing something itself, such as creating an object or doing a calculation

initiating action in other objects controlling and coordinating activities in other objects

• Knowing responsibilities of an object include: knowing about private encapsulated data knowing about related objects knowing about things it can derive or calculate

Page 4: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 5 15-214 Kästner

Design Goals - Summary

•5 design goals Design for division of labor Design for understandability and maintenance Design for change Design for reuse Design for robustness

•5 design strategies (for now) Explicit interfaces (clear boundaries) Information hiding(hide likely changes) Low coupling (reduce dependencies) High cohesion (one purpose per class) Low repr. gap (align requirements and impl.)

Page 5: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 6 15-214 Kästner

GRASP Patterns

• GRASP = General Responsibility Assignment Software Patterns (introduced by Craig Larman)

• Patterns of assigning responsibilities reason about design trade-offs when assigning methods and fields to classes

• The GRASP patterns are a learning aid to help one understand essential object design apply design reasoning in a methodical, rational, explainable way

lower level and more local reasoning than most design patterns

Page 6: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 7 15-214 Kästner

Patterns/Principles aid Communication

Fred: "Where do you think we should place the responsibility for creating a SalesLineltem? I think a Factory." Wilma: "By Creator, I think Sale will be suitable." Fred: "Oh, right - I agree."

Page 7: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 8 15-214 Kästner

Nine GRASP Pattern:

• Creator

• Information Expert

• Low Coupling

• High Cohesion

• Controller

• Polymorphism

• Indirection

• Pure Fabrication

• Protected Variations

Page 8: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 9 15-214 Kästner

Information Expert (GRASP Pattern 1)

• Who should be responsible for knowing the grand total of a sale?

???

getTotal(…)

Page 9: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 10 15-214 Kästner

Information Expert (GRASP Pattern 1)

• Who should be responsible for knowing the grand total of a sale?

Sale

time

SalesLineItem

quantity

ProductDescription

descriptionpriceitemID

Described-by*

Contains

1..*

1

1

Register

id

Captured-on

Customer

name

Paid by

Page 10: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 11 15-214 Kästner

Information Expert

• Problem: What is a general principle of assigning responsibilities to objects?

• Solution: Assign a responsibility to the class that has the information necessary to fulfill the responsibility

• Start assigning responsibilities by clearly stating responsibilities!

• Typically follows common intuition

• Design Classes (Software Classes) instead of Conceptual Classes If Design Classes do not yet exist, look in Domain Model for fitting abstractions (-> low representational gap)

Page 11: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 12 15-214 Kästner

Information Expert

• What information is needed to determine the grand total? Line items and the sum of their subtotals

• Sale is the information expert for this responsibility.

getTotal()

Page 12: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 13 15-214 Kästner

Information Expert

• To fulfill the responsibility of knowing and answering the sale's total, three responsibilities were assigned to three design classes of objects

Page 13: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 14 15-214 Kästner

Information Expert

S a le

t im e

. . .

g e tT o ta l ( )

S a le s L in e I te m

q u a n t i t y

g e tS u b to ta l ( )

P r o d u c t

D e s c r ip t io n

d e s c r ip t io n

p r ic e

i te m ID

g e tP r ic e ( )N e w m e th o d

:P r o d u c t

D e s c r ip t io n

1 .1 : p := g e tP r ic e ( )

1 * : s t = g e tS u b to ta l: S a le

t = g e tT o ta l l in e I te m s [ i ] :

S a le s L in e I te m

Page 14: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 15 15-214 Kästner

Information Expert -> "Do It Myself Strategy"

• Expert usually leads to designs where a software object does those operations that are normally done to the inanimate real-world thing it represents a sale does not tell you its total; it is an inanimate thing

• In OO design, all software objects are "alive" or "animated," and they can take on responsibilities and do things.

• They do things related to the information they know.

Page 15: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 16 15-214 Kästner

Information Expert: Discussion of Design Goals/Strategies

• Explicit and small interfaces, information hiding Does not expose how sale is computed

• Low coupling Client does not need to know about LineItem and ProductDescription

• Low representational gap Assign responsibilities similar to responsibilities of real-world abstractions

• May conflict with cohesion Example: Who is responsible for saving a sale in the database?

Adding this responsibility to Sale would distribute database logic over many classes low cohesion

• -> Design for Reuse, Understanding, Change, …

Page 16: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 17 15-214 Kästner

Creator (GRASP Pattern 2)

• Who is responsible for creating SalesLineItem objects?

Sale

time

SalesLineItem

quantity

ProductDescription

descriptionpriceitemID

Described-by*

Contains

1..*

1

1

Register

id

Captured-on

Customer

name

Paid by

Page 17: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 18 15-214 Kästner

Creator Pattern (GRASP Pattern 2)

• Problem: Assigning responsibilities for creating objects Who creates Nodes in a Graph? Who creates instances of SalesItem? Who creates Rabbit-Actors in a Game? Who creates children if rabbits breed? Who creates Tiles in a Monopoly game? AI? Player? Main class? Board? Meeple (Dog)?

Page 18: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 19 15-214 Kästner

Creator Pattern

• Problem: Who creates an A?

• Solution: Assign class responsibility of creating instance of class A to B if B aggregates A objects B contains A objects B records instances of A objects B closely uses A objects B has the initializing data for creating A objects

• the more the better; where there is a choice, prefer B aggregates or contains A objects

• Key idea: Creator needs to keep reference anyway and will frequently use the created object

Page 19: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 20 15-214 Kästner

Creator : Example

• Who is responsible for creating SalesLineItem objects? Creator pattern suggests Sale.

• Interaction diagram:

Page 20: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 21 15-214 Kästner

Creator: Discussion of Design Goals/Strategy

• Promotes low coupling, design for reuse class responsible for creating objects it needs to reference creating the objects themselves avoids depending on another class to create the object

• Information hiding, design for change Object creation is hidden, can be replaced locally

• But creation may require significant complexity using recycled instances for performance reasons conditionally creating an instance from one of a family of similar classes based upon some external property value

Sometimes desired to outsource object wiring (“dependency injection”)

• -> Several more complex creation strategies; many design patterns

Page 21: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 22 15-214 Kästner

Controller (GRASP Pattern 3)

22

• What first object receives and coordinates a system operation (events)?

???

endSale(…)

???

enterItem(…)

Page 22: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 23 15-214 Kästner

Controller (GRASP Pattern 3)

23

• What first object receives and coordinates a system operation (events)? a user clicking on a button a network request arriving a database connection dropped

???

endSale(…)

???

nextRound(…)

Page 23: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 24 15-214 Kästner

Controller (GRASP Pattern 3)

24

• Problem: What first object receives and coordinates a system operation (events)?

• Solution: Assign the responsibility to an object representing the overall system, device, or subsystem (facade controller) or a use case scenario within which the system event occurs (use case controller)

Page 24: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 25 15-214 Kästner

Controller: Example

• By the Controller pattern, here are some choices:

• Register, POSSystem: represents the overall "system," device, or subsystem

• ProcessSaleSession, ProcessSaleHandler: represents a receiver or handler of all system events of a use case scenario

Page 25: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 26 15-214 Kästner

Controller: Discussion

• Controller delegates to other objects; coordinates or controls the activity; does not much work itself

• Facade controllers suitable when not "too many" system events -> one overall controller for the system

• Use case controller suitable when façade controller "bloated" with excessive responsibilities (low cohesion, high coupling) -> several smaller controllers for specific tasks

• Closely related to Façade design pattern

Page 26: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 27 15-214 Kästner

Controller: Discussion of Design Goals/Strategies

• Design for Reuse Reuse entire subsystem through small explicit interface Information hiding for entire subsystem, e.g. hide that operations must be performed in specific sequence

Separation of application logic from GUI

• Design for Change Application logic changeable without changing GUI GUI changeable without changing application logic

• Design for Understandability Dedicated place to understand interaction with environment events

• But, bloated controllers increase coupling and decrease cohesion; split if applicable

Page 27: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 28 15-214 Kästner

Other GRASP Patterns

• Low Coupling Decide between two designs for the one with lower coupling

• High Cohesion Decide between two designs for the one with higher cohesion

• Polymorphism Support alternatives with dynamic dispatch (multiple implementations of an interface) instead of case analysis

See also Strategy Design Pattern

• Pure Fabrication If domain model provides no reasonable concept to assign responsibility without violating cohesion/coupling -> create a new abstraction (e.g., PersistantStorage)

Page 28: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 29 15-214 Kästner

Summary

• Assigning Responsibilities to Classes

• GRASP Patterns for first design considerations Information Expert Creator Controller

• Reason with Design Goals

• Patterns facilitate communication

Page 29: Principles of Software Construction: Objects, Design and ...charlie/courses/15-214/2014-spring/slides/11c... · Creator Pattern (GRASP Pattern 2) ... system events -> one overall

toad 30 15-214 Kästner

Literature

• Craig Larman, Applying UML and Patterns, Prentice Hall, 2004 Chapter 16+17+22 introduce GRASP