Thursday, December 28, 2017

GRASP


  • Acronym for General Responsibility Assignment Software Pattern
  • Appropriate assignment of responsibility to classes is the key of success in a project.
  • There are fundamental principle in assigning responsibilities they are summarized in GRASP.
  • There are nine principle that object oriented designer apply.
9 GRASP patterns

1. Creator
2. Information Expert
3. Low Coupling
4. Controller
5. High Cohesion
6. Indirection
7. Polymorphism
8. Protected Variations
9. Pure Fabrication

1. Creator : Who creates an object. Container object creates contained object. Decide who can be creator based on interaction and association of object.
Example : Video Store is container and video is contained object. Video Store has an Aggregation relationship with Video. so we can instantiate video object inside Video Store class.

2. Expert : 

What responsibility should assign to object. Expert Principle says give the responsibility to object for which it has the information to full fill the responsibility. Or in some cases they colloborate with other to get the information.

Example :
Assume we need to get all videos of a video store. Since Video store has all the information required for this task so we can assign this responsibility to video store.

class VideoStore
{
getAllVideos();

}


3. Low Coupling

Object should be loosely coupled.

Two elements are coupled if
1. One element has aggregation/composition relationship with another element.
2. One element implements another.

Example of Poor Coupling




4. Controller

Deals with how to delegate the request from UI layer to domain layer object
Controller decide who will process this request.
This object is called controller which receive the request from UI.


We can make an object controller if

1. Object represent the overall system (facade controller)
2. Object represent a use case, handling a sequence of operation (session controller)


Benefit
1. Can reuse this controller class
2. Can control the sequence of operations.


Blotted Controller

Controller class is called blotted if it is overloaded with too many responsibilities.
Solution : Add more controller

Controller class also performing many task other than delegation.
Solution : Controller class has to delegates things to others


5. High Cohesion

  • How are the operations of an element are functionally related?
  • Related responsibilities should be be in one manageable unit
  • Prefer High Cohesion
  • Clearly define the purpose of an element.
  • Benefits
    • Easily understandable and maintainable
    • Code reuse
    • Low coupling 





6. Polymorphism

  • How to handle related and varying elements based on element type?
  • Polymorphism guides us in deciding which object is responsible for handling those varying elements.
  • Benefit : Handling new variations becomes easy.

7. Pure Fabrication


Fabricated class/ artificial class – assign set of related responsibilities that doesn't represent
any domain object.

● Provides a highly cohesive set of activities.
● Behavioral decomposed – implements some algorithm.
● Examples: Adapter, Strategy
● Benefits: High cohesion, low coupling and can reuse this class.


Example If we want to store shape data in database and write this functionality inside shape class then there will be need to write some database related operation inside shape class which will make it incohesive So solution is that create a separate DBStore class for storing.

Similarly Logging interface which write log it is an good example of Pure Fabrication

8. Indirection

  • How can we avoid direct coupling between two or more elements?
  • Indirection introduce an intermediate unit to facilitate their communication so that they will not be directly coupled.
  • Benefit : Low Couplinng
  • Example : Facade, Observer, Adaptor

9. Protected Variation/ Don't talk to Strangers

  • How to avoid impact of variation of some element on other elements?
  • It defines a well defined interface so that there will be no affect on other elements.
  • Provides flexibility and protection from variations.
  • Provide more structured design
  • Example : Polymorphism,Data Encapsulation and interfaces

No comments:

Followers

Link