GRASP Design Principles
I bet you’ve already heard about SOLID Principles. But do you know the GRASP Principles (aka GRASP Patterns)? 🧐
They are a learning aid for object oriented design with responsibilities. There are nine GRASP patterns, which I briefly introduce below as problem — solution (with a thematic picture to help to memorize the principle 🎓).
GRASP = object oriented design with responsibilities
Creator
Who should be responsible for creating a new instance of some class?
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 created. Thus B is an Expert with respect to creating A.
Information Expert
What is a general principle of assigning responsibilities to objects?
Assign a responsibility to the information expert — the class that has the information necessary to fulfill the responsibility.
Low Coupling
How to support low dependency, low change impact, and increased reuse?
Assign a responsibility so that coupling remains low. Use this principle to evaluate alternatives.
Controller
What first object beyond the UI layer receives and coordinates (“controls”) a system operation?
Assign the responsibility to a class representing one of the following choices:
- Represents the overall “system”, a “root object”, a device that the software is running within, or a major subsystem — these are all variations of a facade controller.
- Represents a use case scenario within which the system operation occurs, often named <UseCaseName>[Handler|Coordinator|Session] (use case or session controller).
High Cohesion
How to keep objects focused, understandable, and managable, and as a side effect, support Low Coupling?
Assign a responsibility so that cohesion remains high. Use this to evaluate alternatives.
Polymorphism
How handle alternatives based on type? How to create pluggable software components?
When related alternatives or behaviors vary by type (class), assign responsibility for the behavior — using polymorphic operations — to the types for which the behavior varies.
Indirection
Where to assign a responsibility, to avoid direct coupling between two (or more) things? How to de-couple objects so that low coupling is supported and reuse potential remains higher?
Assign the responsibility to an intermediate object to mediate between other components or services so that they are not directly coupled.
Pure Fabrication
What object should have the responsibility, when you do not want to violate High Cohesion and Low Coupling, or other goals, but solutions offered by Expert are not appropriate?
Assign a highly cohesive set of responsibilities to an artificial or convenience class that does not represent a problem domain concept — something made up, to support high cohesion, low coupling, and reuse.
Protected Variations
How to design objects, subsystems, and systems so that the variations or instability in these elements does not have an undesirable impact on other elements?
Identify points of predicted variation or instability; assign responsibilities to create a stable interface around them.
Applying UML and Patterns
If the subject caught your attention and you’d like to know more, you should visit the Craig Larman’s website and buy his book: Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development 📖
You will deepen your knowledge of the GRASP Principles and learn much more about design and software architecture! 🤓