Hack Frontend Community

What are GRASP Patterns

GRASP (General Responsibility Assignment Software Patterns) is set of nine principles for distributing responsibilities between classes and objects in object-oriented design.

GRASP Principles

  1. Information Expert
    Assign responsibility to class that has necessary information to perform it.
  2. Creator
    Entrust object instance creation to class that contains or aggregates created objects.
  3. Controller
    Process external events (UI, system) with special controller class, separating logic from presentation.
  4. Low Coupling
    Strive for minimal coupling between classes so changes affect as few entities as possible.
  5. High Cohesion
    Group related responsibilities in same classes so they remain narrowly focused and understandable.
  6. Polymorphism
    Use polymorphism to avoid conditional constructs, delegating variable behavior to subclasses.
  7. Pure Fabrication
    When necessary introduce auxiliary classes ("fabrications") to reduce coupling or increase cohesion.
  8. Indirection
    Introduce intermediaries between objects to weaken direct dependencies and improve variability.
  9. Protected Variations
    Isolate areas that may change behind stable interfaces to minimize impact of changes.

Note:

GRASP doesn't impose specific structure, but provides guidance for reasonable responsibility distribution.

Steps for Applying GRASP in Project

1

1. Define Key Responsibilities

Analyze use cases and identify set of operations and data.

2

2. Assign Experts

For each responsibility choose "Information Expert" class that has needed data.

3

3. Analyze Coupling and Cohesion

Apply Low Coupling and High Cohesion principles, redistribute responsibilities if necessary.

4

4. Introduce Fabrication or Indirection

If classes start getting overloaded or coupling is too strong, add auxiliary class or intermediary.

5

5. Protect Variable Parts

Wrap unstable code with stable interfaces (Protected Variations).

Note:

GRASP is best applied at early design stages to avoid anti-patterns and improve system maintenance.

Read more — in original article by Larman:
GRASP on Wikipedia

Summary

  • GRASP is nine principles of responsibility assignment in object-oriented design.

  • Main ideas: Information Expert, Low Coupling, High Cohesion and Protected Variations.

  • Following GRASP, you'll get more flexible, maintainable and extensible architecture.

  • refactoring.guru - patterns and refactoring

  • patterns.dev - patterns