GRASP stands for General Responsibility Assignment Software Patterns. It guides in assigning responsibilities to collaborating objects.
9 GRASP patterns
- Information Expert
- Low Coupling
- High Cohesion
- Protected Variations
- Pure Fabrication
How to Attache Responsiblity to a Object
- Responsibility can be accomplished by a single object or a group of object collaboratively accomplish a responsibility.
- GRASP helps us in deciding which responsibility should be assigned to which object/class.
- Identify the objects and responsibilities from the problem domain, and also identify how objects interact with each other.
- Define blue print for those objects e.g. class with methods implementing those responsibilities.
The following are mail design principle
- Who creates an Object? Or who should create a new instance of some class?
- “Container” object creates “contained” objects.
- Decide who can be creator based on the objects association and their interaction.
- provided an object obj, which responsibilities can be assigned to obj?
- Expert principle says that assign those responsibilities to obj for which obj has the information to fulfill that responsibility.
- They have all the information needed to perform operations, or in some cases they collaborate with others to fulfill their responsibilities.
3. Low Coupling
- How strongly the objects are connected to each other?
- Coupling – object depending on other object.
- When depended upon element changes, it affects the dependant also.
- Low Coupling – How can we reduce the impact of change in depended upon elements on dependant elements.
- Prefer low coupling – assign responsibilities so that coupling remain low.
- Minimizes the dependency hence making system maintainable, efficient and code reusable
- Two elements can be coupled, by following if
- One element has aggregation/composition or association with another element.
- – One element implements/extends other element.
4. High Cohesion
- How are the operations of any element are functionally related?
- Related responsibilities in to one manageable unit.
- Prefer high cohesion
- Clearly defines the purpose of the element
– Easily understandable and maintainable.
– Code reuse
– Low coupling
- Deals with how to delegate the request from the UI layer objects to domain layer objects.
- when a request comes from UI layer object, Controller pattern helps us in determining what is that first object that receive the message from the UI layer objects.
- This object is called controller object which receives request from UI layer object and then controls/coordinates with other object of the domain layer to fulfill the request.
- It delegates the work to other class and coordinates the overall activity.
- We can make an object as Controller, if
- Object represents the overall system (facade controller)
- Object represent a use case, handling a sequence of operations
Benefits of Controller:
– can reuse this controller class.
– Can use to maintain the state of the use case.
– Can control the sequence of the activities
is Bloated Controllers Good Or Bad
- Controller class is called bloated, if the class is overloaded with too many responsibilities.
- follwoing are the solution
- Add more controllers
- Controller class also performing many tasks instead of delegating to other class.
- controller class has to delegate things to others.
- How to handle related but varying elements based on element type?
- Polymorphism guides us in deciding which object is responsible for handling those varying elements.
- Benefits: handling new variations will become 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.
- How can we avoid a direct coupling between two or more elements.
- Indirection introduces an intermediate unit to communicate between the other units, so that the other units are not directly coupled.
- Benefits: low coupling, e.g Facade, Adapter, Obserever.
- Class Employee provides a level of indirection to other units of the system.
9. Protected Variation
- How to avoid impact of variations of some elements on the other elements.
- It provides a well defined interface so that the there will be no affect on other units.
- Provides flexibility and protection from variations.
- Provides more structured design. Example: polymorphism, data encapsulation, interfaces
- Applying UML and Patterns, Third Edition, Craig Larman