Lecture 14 
GRASP
Agenda 
 Architectural Principles 
 GRASP
Reading 
 Barbin 2, 4
ARCHITECTURAL PRINCIPLES
Code Reuse 
 Use of existing software to build new software 
 Examples: 
– Libraries, previous version, inheritance, composition, 
cut-paste programming, code generators, and 
frameworks 
 Opposite 
– Reinventing the Wheel or Reinventing the Square 
Wheel
DRY – Don’t Repeat Yourself 
 Aimed at reducing duplicate information, 
process and behavior 
– Can apply to code, databases, documentations, 
builds, tests 
 Achieved by 
– Automating repetitive tasks, data transformation, code 
generators, scripting
NIH – Not Invented Here 
 Syndrome where people don’t want to use 
existing code but prefer to writing their own in-house 
code 
– Fear of unknown, distrust, turf war, nationalism, 
protectionism 
 Opposite 
– Proudly found elsewhere (PFE) where use of other 
code, open source, is encouraged
Cathedral and the Bazaar (CatB) 
 Software models 
– In the Cathedral model, the source code is protected 
and seen by only exclusive limited group of devs 
– In the Bazaar model, the source is open to everyone
Cathedral and the Bazaar (CatB) 
 Software models 
– The Cathedral model creates proprietary assets that 
can be monetized 
– The Bazaar model finds more bugs faster and is 
central to the open source community 
– Monetization of Bazaar code must be done with 
services or something else that selling code
POGE – Principles of Good 
Enough 
 Quick and simple solution is favored over 
complex and elaborate 
– The software is evolved and driven by end user 
requirements 
– Central to the Agile development
Worse is Better 
 Less (worse) functionality is preferable (better) 
 Quick and simple solution is favored over 
complex and elaborate 
– The software is evolved and driven by end user 
requirements 
– Central to the Agile development 
 Examples 
– Unix, C
Worse is Better 
 Simplicity 
– The design must be simple, both in implementation 
and interface 
 Correctness 
– The design must be correct in all observable aspects 
 Consistency 
– The design must not be overly inconsistent. 
 Completeness 
– The design must cover as many important situations 
as is practical
GRASP
GRASP 
 General Responsibility Assignment Software 
Patterns 
– Note: pattern is not referring to Design Pattern 
 Set of principles that will guide you on how to 
assign responsibility to classes 
– Responsibility is data (knowing) and process (doing) 
 Answers: “Where to put the Code?” 
 Who takes care of what?
GRASP 
1. Creator 
2. Controller 
3. Information Expert 
4. Low Coupling 
5. High Cohesion 
6. Indirection 
7. Pure Fabrication 
8. Polymorphism 
9. Protected Variations
Creator 
 Creator has the responsibility of creating an 
object 
– Who should create objects? 
– Creating object creates dependency 
A should create B if A has intimate knowledge of B 
A contains B and knows how to initialize B 
Factory classes know how to create
Controller 
 The overall coordinator of system operations 
Receiving client requests and delegating them to other 
objects to fulfill the request 
Controllers are the classes that handle exceptions 
Model-View-Controller, Process
Information Expert 
 The class that has the information necessary to 
fulfill responsibility 
Example: Employee class that knows how to get and 
set its own information
Low Coupling 
 Assign responsibility to classes so that coupling 
is low 
– They have less dependency on each other 
– Dependency injections 
– Coupling between 
different modules
High Cohesion 
 Assign related responsibility to classes so that 
they form a highly-cohesive and functional unit 
– Cohesion within a module
Indirection 
 Assign responsibility to an intermediate object 
to facilitate communication between two objects 
to avoid direct coupling 
Example: mapper, adaptors, data exchange XML – Json
Polymorphism 
 Assign responsibility of defining variations of 
behaviors to different classes 
Example: class Payment with subclasses 
PaymentByCreditCard, PaymentByDebitCard etc
Protected Variations (PV) 
 Identify variations and evolution points, then 
assign their responsibilities to stable interfaces 
– The implementation can then be written and modified 
without affecting existing clients 
Open-Closed Principle 
Program to Interfaces not implementation 
Separate what varies
Pure Fabrication 
 Assign responsibility to artificial or convenience 
objects to abstract operation 
Example: loggers
Summary 
 Architectural Principles 
– DRY, NIH, CatB, POGE, Worse is Better 
 General Responsibility Assignment Software 
Patterns

L12 GRASP

  • 1.
  • 2.
    Agenda  ArchitecturalPrinciples  GRASP
  • 3.
  • 4.
  • 5.
    Code Reuse Use of existing software to build new software  Examples: – Libraries, previous version, inheritance, composition, cut-paste programming, code generators, and frameworks  Opposite – Reinventing the Wheel or Reinventing the Square Wheel
  • 6.
    DRY – Don’tRepeat Yourself  Aimed at reducing duplicate information, process and behavior – Can apply to code, databases, documentations, builds, tests  Achieved by – Automating repetitive tasks, data transformation, code generators, scripting
  • 7.
    NIH – NotInvented Here  Syndrome where people don’t want to use existing code but prefer to writing their own in-house code – Fear of unknown, distrust, turf war, nationalism, protectionism  Opposite – Proudly found elsewhere (PFE) where use of other code, open source, is encouraged
  • 8.
    Cathedral and theBazaar (CatB)  Software models – In the Cathedral model, the source code is protected and seen by only exclusive limited group of devs – In the Bazaar model, the source is open to everyone
  • 9.
    Cathedral and theBazaar (CatB)  Software models – The Cathedral model creates proprietary assets that can be monetized – The Bazaar model finds more bugs faster and is central to the open source community – Monetization of Bazaar code must be done with services or something else that selling code
  • 10.
    POGE – Principlesof Good Enough  Quick and simple solution is favored over complex and elaborate – The software is evolved and driven by end user requirements – Central to the Agile development
  • 11.
    Worse is Better  Less (worse) functionality is preferable (better)  Quick and simple solution is favored over complex and elaborate – The software is evolved and driven by end user requirements – Central to the Agile development  Examples – Unix, C
  • 12.
    Worse is Better  Simplicity – The design must be simple, both in implementation and interface  Correctness – The design must be correct in all observable aspects  Consistency – The design must not be overly inconsistent.  Completeness – The design must cover as many important situations as is practical
  • 13.
  • 14.
    GRASP  GeneralResponsibility Assignment Software Patterns – Note: pattern is not referring to Design Pattern  Set of principles that will guide you on how to assign responsibility to classes – Responsibility is data (knowing) and process (doing)  Answers: “Where to put the Code?”  Who takes care of what?
  • 15.
    GRASP 1. Creator 2. Controller 3. Information Expert 4. Low Coupling 5. High Cohesion 6. Indirection 7. Pure Fabrication 8. Polymorphism 9. Protected Variations
  • 16.
    Creator  Creatorhas the responsibility of creating an object – Who should create objects? – Creating object creates dependency A should create B if A has intimate knowledge of B A contains B and knows how to initialize B Factory classes know how to create
  • 17.
    Controller  Theoverall coordinator of system operations Receiving client requests and delegating them to other objects to fulfill the request Controllers are the classes that handle exceptions Model-View-Controller, Process
  • 18.
    Information Expert The class that has the information necessary to fulfill responsibility Example: Employee class that knows how to get and set its own information
  • 19.
    Low Coupling Assign responsibility to classes so that coupling is low – They have less dependency on each other – Dependency injections – Coupling between different modules
  • 20.
    High Cohesion Assign related responsibility to classes so that they form a highly-cohesive and functional unit – Cohesion within a module
  • 21.
    Indirection  Assignresponsibility to an intermediate object to facilitate communication between two objects to avoid direct coupling Example: mapper, adaptors, data exchange XML – Json
  • 22.
    Polymorphism  Assignresponsibility of defining variations of behaviors to different classes Example: class Payment with subclasses PaymentByCreditCard, PaymentByDebitCard etc
  • 23.
    Protected Variations (PV)  Identify variations and evolution points, then assign their responsibilities to stable interfaces – The implementation can then be written and modified without affecting existing clients Open-Closed Principle Program to Interfaces not implementation Separate what varies
  • 24.
    Pure Fabrication Assign responsibility to artificial or convenience objects to abstract operation Example: loggers
  • 25.
    Summary  ArchitecturalPrinciples – DRY, NIH, CatB, POGE, Worse is Better  General Responsibility Assignment Software Patterns