GRASP (General Responsibility Assignment Software Patterns) is a design pattern in object-oriented software development used to assign responsibilities for different modules of code. GRASP is a set of “nine fundamental principles in object design and responsibility assignment”, first published by Craig Larman in 1997 in a book “Applying UML and Patterns”.
GRASP is occasionally coupled with other design patterns such as SOLID. Design patterns such as these help keep code simpler, more organized, more comprehensible, analyzable and reusable.
In object-oriented design, a pattern is a named description of a problem and solution that can be applied in new contexts; ideally, a pattern advises us on how to apply its solution in varying circumstances and considers the forces and trade-offs. Many patterns, given a specific category of problem, guide the assignment of responsibilities to objects.
This is the list of 9 GRASP patterns.
- Information Expert
- Low Coupling
- High Cohesion
- Protected Variations
- Pure Fabrication
Takes responsibility for creating certain other objects. It has information on how to create objects. It can be used to enforce the logical design of a system.
The information expert pattern states that we need to assign responsibilities to the right expert.
One of the major GRASP principles is Low Coupling. Coupling is a measure of how strongly one object is connected to, has knowledge of, or depends upon other objects.
Controller is responsible for handling the actions of user-interfaces. The controller is the first object to receive a command from the UI.
High cohesion is an evaluative pattern that attempts to keep objects appropriately focused, manageable and understandable. High cohesion is generally used in support of low coupling.
The indirection pattern supports low coupling and reuses potential between two elements by assigning the responsibility of mediation between them to an intermediate object.
According to the polymorphism principle, responsibility for defining the variation of behaviors based on type is assigned to the type for which this variation happens. This is achieved using polymorphic operations. The user of the type should use polymorphic operations instead of explicit branching based on type.
A pattern that protects elements from the variations on other elements by wrapping the focus with an interface and using polymorphism to create various implementations.
A class that does not represent a concept in the problem domain. This kind of class is called a “service” in domain-driven design.