19
GRASP Pattern GRASP Pattern Zhen Jiang West Chester University [email protected]

GRASP Pattern Zhen Jiang West Chester University [email protected]

Embed Size (px)

Citation preview

Page 1: GRASP Pattern Zhen Jiang West Chester University zjiang@wcupa.edu

GRASP PatternGRASP Pattern

Zhen   Jiang

West Chester University

[email protected]  

Page 2: GRASP Pattern Zhen Jiang West Chester University zjiang@wcupa.edu

OutlineOutline

IntroductionInformation ExpertCreatorLow CouplingHigh CohesionController

Page 3: GRASP Pattern Zhen Jiang West Chester University zjiang@wcupa.edu

IntroductionIntroduction

Design processWhat’s GRASP patternWhat’s responsibilityResponsibilities and methodsResponsibilities and Interaction Diagrams

Page 4: GRASP Pattern Zhen Jiang West Chester University zjiang@wcupa.edu

Design ProcessDesign Process

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.

Page 5: GRASP Pattern Zhen Jiang West Chester University zjiang@wcupa.edu

What’s GRASP patternWhat’s GRASP pattern

This approach to understanding and using design principles is based on patterns of assigning responsibilities.

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

Page 6: GRASP Pattern Zhen Jiang West Chester University zjiang@wcupa.edu

What’s responsibilityWhat’s responsibility

Doing:– 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:– Knowing about private encapsulated data– Knowing about related objects– Knowing about things it can derive or calculate

Page 7: GRASP Pattern Zhen Jiang West Chester University zjiang@wcupa.edu

Responsibilities and methodsResponsibilities and methods

:Sale

:Payment

makePaymentcreate

makePayment implies Sale object has a responsibility to create a Payment object

Page 8: GRASP Pattern Zhen Jiang West Chester University zjiang@wcupa.edu

Responsibilities and Responsibilities and Interaction DiagramsInteraction Diagrams

Interaction diagrams show choices in assigning responsibilities to objects.

GRASP patterns guide choices in where to assign responsibilities.

GRASP patterns are a codification of widely used basic principles.

Page 9: GRASP Pattern Zhen Jiang West Chester University zjiang@wcupa.edu

Information ExpertInformation Expert

Assign a responsibility to the information expert – the class that has the information necessary to fulfill the responsibility.

What is a general principle of assigning responsibilities to objects?– Who should be responsible for knowing/doing …?– Domain model (domain expert, domain analysis) to

design model (software classes).– Any existing to any representative.

Page 10: GRASP Pattern Zhen Jiang West Chester University zjiang@wcupa.edu

Example: What information is Example: What information is needed to determine the grand total?needed to determine the grand total?

Sale

date

Product

descriptionpriceitemID

Page 11: GRASP Pattern Zhen Jiang West Chester University zjiang@wcupa.edu

Sale

date

Product

descriptionpriceitemID

SalesLineItem

quantity

Who is information expert?

Page 12: GRASP Pattern Zhen Jiang West Chester University zjiang@wcupa.edu

Sale

date

Product

descriptionpriceitemID

SalesLineItem

quantity

getTotal

getSubTotal

getPrice

Page 13: GRASP Pattern Zhen Jiang West Chester University zjiang@wcupa.edu

Time sequence?

Page 14: GRASP Pattern Zhen Jiang West Chester University zjiang@wcupa.edu

CreatorCreator Who should be responsible for creating a new instance

of some class? Creational Design Pattern Assign class B the responsibility to create an instance of

class A if one or more of the following is true:– B aggregates A– B contains A– B records instance of A objects– B closely use A objects– B passes initializing data to A

Page 15: GRASP Pattern Zhen Jiang West Chester University zjiang@wcupa.edu

Low CouplingLow Coupling

Coupling is a measure of how strongly one element is connected to, has knowledge of, or relies on other elements.

Payment, register, and sale.– Time sequence?– Classes?

Page 16: GRASP Pattern Zhen Jiang West Chester University zjiang@wcupa.edu

:Sale

:Payment

:Register

create

addPayment

makePayment

:Payment

:Sale

:Register

makePayment

create

makePayment

Page 17: GRASP Pattern Zhen Jiang West Chester University zjiang@wcupa.edu

High CohesionHigh Cohesion How to keep complexity manageable? Time sequence of the above example. More tasks? Key role, key object, key class, key relations Modular Design (indirect control)

Page 18: GRASP Pattern Zhen Jiang West Chester University zjiang@wcupa.edu

Yin and YangYin and Yang

Yin and Yang – The light color area which indicates more

sunlight is called Yang (Sun). The dark color area has less sunlight (more moonlight) and is called Yin (Moon). Yang is like man. Yin is like woman. Yang wouldn't grow without Yin. Yin couldn't give birth without Yang.

Interdependent influence Trade-off

Page 19: GRASP Pattern Zhen Jiang West Chester University zjiang@wcupa.edu

ControllerController Assign the responsibility for receiving or handling a

system event message to a class representing one of the following choices:– Represents the overall system, device, or subsystem– Represents a use case within which the system event occurs.

Who should be the controller for system events such as enterItem and endSale?

Which class should be responsible for receiving system event message?

Interface layer does not handle system events. The controller receives the service request from the UI

layer and coordinates their fulfillment, ie., Command, Façade, and etc.