Grasp principles

32
General Responsibility Assignment Software Patterns author: Yuriy Shapovalov ([email protected]) GRASP

description

Overview of General Responsibility Assignments Patterns.

Transcript of Grasp principles

Page 1: Grasp principles

General Responsibility Assignment Software Patterns

author: Yuriy Shapovalov ([email protected])

GRASP

Page 2: Grasp principles

• GRASP like a term and principles firstly was described at Craig Larman’s book “Applying UML and Patterns”

GRASP stands for:• General• Responsibility• Assignment• Software• Patterns (Principles)

Where it came from?

Page 3: Grasp principles

• Creator• Informational Expert• Controller• Low Coupling• High Cohesion• Polymorphism• Pure Fabrication• Indirection• Protected Variation

Nine Patterns

Page 4: Grasp principles

• Patterns of assigning responsibilities (RDD)

• GRASP declares more common principles responsibility separation, when GoF describe more specific and complex design patterns.

• SOLID – more common OOP principles less oriented on responsibility segregation

• GRASP somewhere between GoF patterns and SOLID principles.

Differences from GoF and SOLID

Page 5: Grasp principles

Confidential 5

Representation in Sequence Diagram (UML)

:Sale

:Payment

makePayment(cashTendered)create(cashTendered)

• Relationship between methods and responsibilities.

• Assuming that Sale objects are responsible to create Payment objects

Page 6: Grasp principles

• “doing” responsibilities– Doing something itself, such as creation an object or doing a

calculation.– Initiating action in other objects– Controlling and coordinating activities in other objects.

• “knowing” responsibilities– Knowing about encapsulated data.– Knowing about related objects.– Knowing about things it can derive or calculate.

Responsibility-Driven Design

Page 7: Grasp principles

Problem: Which class possesses information about object A?

More common question: What is a general principle of assigning responsibilities to objects?

Assign the responsibility to the class that knows the necessary information for performing required action and fulfill the responsibility.

Informational Expert

Page 8: Grasp principles

Tell, Don’t Ask.

“Procedural code gets information then, makes decisions. Object-oriented code tells object to do things” © Alec Sharp

Informational Expert

Page 9: Grasp principles

Informational Expert

A B C D

DO

GetDataX()

GetDataY()

GetDataZ()

return Z

return Y

return X

Page 10: Grasp principles

Informational Expert

A B C D

DO

DoAction()

DoAction(X)

DoAction(Y)

Page 11: Grasp principles

Problem: Who should be responsible for creating object A?

In general, a class B should be responsible for creating instances of class A if one, or preferably more, of the following apply:• Instances of B contain or compositely aggregate instances of A• Instances of B record instances of A• Instances of B closely use instances of A• Instances of B have the initializing information for instances of A

and pass it on creation.

Creator

Page 12: Grasp principles

• The goal is to define creator-object, which will be related to all created objects.

Creator

Board Square64

Square

Boardcreate

create

Page 13: Grasp principles

In case of complex creation login, based on some external cases, make sense to use Factory pattern, and delegate creation responsibility to auxiliary class.

• Related patterns: Concrete Factory and Abstract Factory

Creator

Page 14: Grasp principles

• Problem: How to reduce design change impact, support low dependency and increase reuse?

Coupling is the degree, defines how tightly one component linked to other components, or how much information it knows about other components.

• A change in one module usually forces a ripple effect of changes in other modules.

• Assembly of modules might require more effort and/or time due to the increased inter-module dependency.

• A particular module might be harder to reuse and/or test because dependent modules must be included.

Low Coupling

Page 15: Grasp principles

Confidential 15

Tightly Coupled Application

Page 16: Grasp principles

• Assign a responsibility so that coupling remains low.

Low Coupling is an evaluative pattern, which dictates how to assign responsibilities to support:• lower dependency between the classes,• change in one class having lower impact on other classes,• higher reuse potential.

http://en.wikipedia.org/wiki/Coupling_(computer_science)#Module_coupling

Low Coupling

Page 17: Grasp principles

• Problem: How to keep objects focused, understandable, manageable, and support low coupling?

Cohesion is a measure of how strongly related or focused the responsibilities of a single module are.

High Cohesion is an evaluative pattern that attempts to keep objects appropriately focused, manageable and understandable.

Alternatively, low cohesion is a situation in which a given element has too many unrelated responsibilities (“God Object”)

High Cohesion

Page 18: Grasp principles

Cohesion is decreased if:• The functionalities embedded in a class, accessed through its methods, have

little in common.

• Methods carry out many varied activities, often using coarsely grained or unrelated sets of data.

Disadvantages of low cohesion (or “weak cohesion”) are:• Increased difficulty in understanding modules.

• Increased difficulty in maintaining a system, because logical changes in the domain affect multiple modules, and because changes in one module require changes in related modules.

• Increased difficulty in reusing a module because most applications won’t need the random set of operations provided by a module.

High Cohesion

Page 19: Grasp principles

High Cohesion

A A ?? ??

DoA()

DoB()

DoC()

DoA()

DoB()

DoC()

Page 20: Grasp principles

• Problem: Who should be responsible for handling events and messages from external actors (UI, …)?

• Assign the responsibility to a class, such as:– A class that represents the overall system, device, or subsystem.

• Façade Controller Pattern

– A class that represent a use case, whereby performs handling particular system operation.

• Use Case Controller Pattern

• Generally does not perform operation by itself, but delegate responsibility to component objects.

Controller

Page 21: Grasp principles

• Problem: How to act different depending in object’s class, or how to design pluggable components?

• In case of class behavior might changes, responsibilities segregates to different behavior specific classes, using polymorphic operations for this class.

• Advise: Do not use type checking, but conditional logic for implementation different variations based on object type.

Polymorphism

Page 22: Grasp principles

Polymorphism

PlayerSquare

landedOn

RegularSquare

landedOn

GoSquare

landedOn

OtherSquare

landedOn

• There are (roughly) 3 types a polymorphism:

– Ad hoc Polymorphism

– Parametric Polymorphism

– Subtype Polymorphism

Page 23: Grasp principles

• Problem: How to assign responsibilities if applying the Informational Expert principle decreases cohesion and increases coupling?

• Assign the responsibility to an artificial class that does not belongs to the domain model.

• Pure Fabrication is a class that does not reflect any business domain object, but required only for increase cohesion and decrease coupling.

Pure Fabrication

Page 24: Grasp principles

• Problem: How to assign responsibilities in order to avoid direct coupling between two components, and keep ability for reuse.

• Assign responsibility to intermediate class for providing linking between objects not linking directly.

• Related design patterns: Adapter, Bridge, Mediator.

Indirection

Page 25: Grasp principles

• Problem: How to design system and subsystems, that changes in these components does not affects on other components.

• Identify points of possible variations and instability; create stable interfaces upon instable components.

• Open-Closed Principle almost equivalent to PV pattern.

Protected Variations

Page 26: Grasp principles

• There are 2 types of points:– Variation point – branching point on existing system or in

requirements. For example we need to support several types of interfaces for tax payment system

– Evolution point – supposed branching point, which might occur in future, but does not declared by existing requirements.

• Protected variation pattern applying for both variation and evolution points.

Protected Variations

Page 27: Grasp principles

Specific case of loose coupling.

• Each unit should have only limited knowledge about other units• Each unit should only talk to its friend (“don’t talk to strangers”)

Law of Demeter (not part of GRASP)

A B C

Page 28: Grasp principles

More formally, the Law of Demeter for functions requires that a method m of an object O may only invoke the methods of the following kinds of objects:• O itself• m's parameters• Any objects created/instantiated within m• O's direct component objects• A global variable, accessible by O, in the scope of m

“Don’t talk to strangers”

Law of Demeter (not part of GRASP)

Page 29: Grasp principles

• Therefore, sending a message to the result of a previous message send isn't allowed.

RDD conflicts with Law of Demeter

However, "returned values are part of the client/server contract. There need be no correlation between the structure of an object and the object returned by the message.“

Wirfs-Brock, Rebecca; McKean, Alan (November 2002). Object Design: Roles, Responsibilities, and Collaborations.

Page 30: Grasp principles

Overview

Informational Expert Assign a responsibility to the class that has the information needed to fulfill it.

Creator Assign class B the responsibility to create an instance of class A if one of these is true (the more the better):• B "contains" or compositely aggregates A. • B records A. • B closely uses A. • B has the initializing data for A that will be passed to A

when it is crated. Thus B is an Expert with respect to creating A.

Controller Assign the responsibility to a class representing one of the following choices:• Major subsystem classes• A use case scenario classes within which the system

event occursLow Coupling Assign a responsibility so that coupling remains low.

High Cohesion Assign a responsibility so that cohesion remains high.

Page 31: Grasp principles

Overview

Polymorphism The same name operations (methods) in the difference classes is defined. And assign a responsibility to the class the class that the behavior is changed.

Pure Fabrication Define a class for convenience' sake that doesn't express the concept of the problem area at all.

Indirection Assign the responsibility to an intermediate object to mediate between other components or services, so that they are not directly coupled.

Protected Variations Assign responsibility to create a stable interface around an unstable or predictably variable subsystem or component.

Page 32: Grasp principles

Contacts:Yuriy Shapovalov ( [email protected] )

email: [email protected]: shapovalov.yuriytwitter: @YuriyShapovalov

Questions?