GRASP and Core Heuristics

GRASP and Core Heuristics

GRASP (General Responsibility Assignment Software Patterns) guides how to assign responsibilities to classes during object design.

Core GRASP Patterns

  1. Information Expert: Assign responsibility to the class that possesses the necessary information to fulfill it.
  2. Creator: Assign creation of object B to class A if A aggregates, contains, or records B.
  3. Controller: Non-UI class coordinating system operations and workflow events.
  4. Low Coupling: Organize responsibilities to keep inter-class dependencies minimal.
  5. High Cohesion: Ensure the responsibilities of each class are focused and closely related.
  6. Polymorphism: Assign varying behavior to polymorphic classes rather than conditional statements.
  7. Pure Fabrication: Create an artificial domain class (e.g., DatabaseService, Logger) purely to achieve high cohesion and low coupling.
  8. Protected Variations: Shield points of anticipated change with stable interfaces.

Pragmatic Design Heuristics

  • DRY (Don't Repeat Yourself): Every piece of knowledge must have a single, unambiguous representation in a system.
  • YAGNI (You Aren't Gonna Need It): Do not add speculative functionality until strictly needed.
  • KISS (Keep It Simple, Stupid): Favor the simplest design that completely satisfies requirements.
  • Hollywood Principle: "Don't call us, we'll call you"—framework inversion of control.
Exercise 1
Question

According to the Information Expert pattern, which class should calculate total order price: the Order class or the Customer class?

Show solution ↓
Solution

The Order class, because it holds direct access to the collection of OrderItem objects with their respective quantities and prices.