Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

GRASP Principles


Published on

A presentation about GRASP Principles and Object Oriented Design

  • Be the first to comment

GRASP Principles

  1. 1. GRASP (Object Oriented Design)
  2. 2. GRASP <ul><li>G eneral </li></ul><ul><li>R esponsibility </li></ul><ul><li>A ssignment </li></ul><ul><li>S oftware </li></ul><ul><li>P rinciples / P atterns </li></ul>
  3. 4. GRASP <ul><li>Responsibility </li></ul><ul><li>A contract / obligation that a class / module / component must accomplish </li></ul><ul><ul><li>Knowledge </li></ul></ul><ul><ul><ul><li>Private state </li></ul></ul></ul><ul><ul><ul><li>Computed state </li></ul></ul></ul><ul><li>  </li></ul><ul><li>  </li></ul><ul><ul><li>Behavior </li></ul></ul><ul><ul><ul><li>Send messages itself and modify its private state </li></ul></ul></ul><ul><ul><ul><li>Instantiate another objects </li></ul></ul></ul><ul><ul><ul><li>Send messages to another objects </li></ul></ul></ul>
  4. 5. GRASP <ul><li>GRASP Principles </li></ul><ul><li>  </li></ul><ul><ul><li>Expert  </li></ul></ul><ul><ul><li>Creator </li></ul></ul><ul><ul><li>Low Coupling </li></ul></ul><ul><ul><li>High Cohesion </li></ul></ul><ul><ul><li>Controller </li></ul></ul><ul><ul><li>Polymorphism </li></ul></ul><ul><ul><li>Pure Fabrication </li></ul></ul><ul><ul><li>Indirection </li></ul></ul><ul><ul><li>Controlled Variation </li></ul></ul>
  5. 6. GRASP - Expert <ul><li>Given a resposibility, what class must accomplish it? </li></ul><ul><li>&quot;Assign the resposibility to the class that knows the necessary information&quot; </li></ul><ul><li>Examples: </li></ul><ul><ul><li>    Invoice's total amount </li></ul></ul><ul><ul><li>    Invoice itemss' amount </li></ul></ul>
  6. 7. GRASP - Creator <ul><li>Who must instantiate A objects ? </li></ul><ul><li>Class B must have that resposibility if: </li></ul><ul><ul><li>B is composed by A (composition) </li></ul></ul><ul><ul><li>B knows the necesary information in order to instantiate A objets </li></ul></ul><ul><ul><li>B depends heavily on A </li></ul></ul><ul><li>                                                                                        </li></ul><ul><li>Design Patterns </li></ul><ul><ul><li>Factory </li></ul></ul><ul><ul><li>AbstractFactory </li></ul></ul>
  7. 8. GRASP - Low Coupling <ul><li>How to minimize dependencies between classes ? </li></ul><ul><li>  </li></ul><ul><li>Coupling </li></ul><ul><li>&quot;When a class knows or depends on another class.&quot; </li></ul><ul><li>If a class change the dependant class will be affected. </li></ul><ul><li>Examples </li></ul><ul><ul><li>Inheritance </li></ul></ul><ul><ul><li>Composition / agreggation / association </li></ul></ul><ul><ul><li>A send messages to B </li></ul></ul>
  8. 9. GRASP - Low Coupling <ul><li>How to reduce design change impacts ? </li></ul><ul><li>Assign resposibilitys taking care of not to increasing  coupling. </li></ul><ul><li>Zero coupling is &quot;impossible&quot; </li></ul><ul><li>  </li></ul><ul><li>Cyclic coupling it's the worst case </li></ul>
  9. 10. GRASP - High Cohesion <ul><li>How to design cohesive classes? </li></ul><ul><li>Cohession </li></ul><ul><li>&quot;How strongly related and focused the resposiblites of a class / module / package are.&quot; </li></ul><ul><li>Assing resposibilities related to each other. </li></ul><ul><li>Low Cohesion implies: </li></ul><ul><ul><li>Hard to reuse </li></ul></ul><ul><ul><li>Hard to maintain </li></ul></ul><ul><ul><li>Constantly changing </li></ul></ul>
  10. 11. GRASP - Controller <ul><li>Given an event layer (UI) and a bussines layer, who has the responsibility of receive the requests from the UI layer? </li></ul><ul><ul><li>A class that represents the whole system. </li></ul></ul><ul><ul><ul><li>Facade Controler Pattern </li></ul></ul></ul><ul><ul><li>A  class that represents use case. </li></ul></ul><ul><ul><ul><li>Use Case Controller Pattern </li></ul></ul></ul>
  11. 12. GRASP - Polymorphism <ul><li>How to act different depending on object's class? </li></ul><ul><li>How to design pluggable components ? </li></ul><ul><li>Use polymorphism to assing responsibilities on classes when the behavior changes. </li></ul><ul><li>Don't ask about object's class </li></ul><ul><li>  </li></ul><ul><li>&quot;Different types respond to compatible messages (same method name, same parameter set, same method signature). Each message represents a specific behavior.&quot; </li></ul><ul><li>  </li></ul><ul><li>Inheritance, interfaces, duck typing </li></ul>
  12. 13. GRASP - Pure Fabrication <ul><li>How to assign responsibilities if applying the Expert principle decreases cohesion and increases coupling . </li></ul><ul><li>eg: Invoice printing, CSV export </li></ul><ul><li>Assign the resposibility to an artificial class that doesn't belongs to the domain model. </li></ul><ul><li>Design Patterns: </li></ul><ul><ul><li>Active Record </li></ul></ul><ul><ul><li>EntityManager </li></ul></ul>
  13. 14. GRASP - Indirection <ul><li>How to assign responsibilities in order to remove coupling between two components ? </li></ul><ul><li>  </li></ul><ul><li>Assign the responsibility to a component placed bewteen both sides </li></ul><ul><li>Interface that hides changes produced on an external API </li></ul>
  14. 15. GRASP - Controlled Variation <ul><li>How to design in such a way that changes on classes, modules and systems doesn't affect dependent components ? </li></ul><ul><li>Create stable interfaces upon inestable components. </li></ul><ul><li>Variations: </li></ul><ul><ul><li>data </li></ul></ul><ul><ul><li>behaviour </li></ul></ul><ul><ul><li>external systems </li></ul></ul><ul><ul><li>os </li></ul></ul><ul><ul><li>... </li></ul></ul>
  15. 16. GRASP - Controlled Variation <ul><li>Structure variations </li></ul><ul><li>    E e = x.getA().getB().getC().getD().getE(); </li></ul><ul><li>It's really fragile because a change on a link affects the sturcture </li></ul><ul><li>Inside a method send messages to: </li></ul><ul><ul><li>this, self </li></ul></ul><ul><ul><li>objects received as parameters </li></ul></ul><ul><ul><li>self attributes </li></ul></ul><ul><ul><li>self components </li></ul></ul><ul><ul><li>instantiated objects on the current method call </li></ul></ul>
  16. 17. GRASP - Controlled Variation <ul><li>Avoiding variations </li></ul><ul><ul><li>Encapsulation </li></ul></ul><ul><ul><li>Polymorphism </li></ul></ul><ul><ul><li>Interfaces </li></ul></ul><ul><ul><li>Reflection, introspection, metadata </li></ul></ul><ul><ul><li>Uniform access (access methods) </li></ul></ul><ul><ul><li>Service lookups </li></ul></ul><ul><ul><li>Dependency injection, IoC </li></ul></ul><ul><ul><li>Virtual Machines (jvm, pvm, clr) </li></ul></ul>
  17. 18. EOF