GRASP and Core Heuristics
GRASP (General Responsibility Assignment Software Patterns) guides how to assign responsibilities to classes during object design.
Core GRASP Patterns
- Information Expert: Assign responsibility to the class that possesses the necessary information to fulfill it.
- Creator: Assign creation of object
B to class A if A aggregates, contains, or records B.
- Controller: Non-UI class coordinating system operations and workflow events.
- Low Coupling: Organize responsibilities to keep inter-class dependencies minimal.
- High Cohesion: Ensure the responsibilities of each class are focused and closely related.
- Polymorphism: Assign varying behavior to polymorphic classes rather than conditional statements.
- Pure Fabrication: Create an artificial domain class (e.g.,
DatabaseService, Logger) purely to achieve high cohesion and low coupling.
- 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 ↓Hide solution ↑
Solution
The Order class, because it holds direct access to the collection of OrderItem objects with their respective quantities and prices.