AmirdhaGopalRajendran<br />X-Conf <br />Thoughtworks, Chennai<br />Patterns of assigning Responsibilities<br />The critica...
Setting the Context<br />Pattern<br />Responsibility<br />Domain Model<br />Patterns<br />Creator<br />Information Expert<...
Pattern is an abstractsolution to a recurring problem, in a particular context.<br />Larman: A good pattern is a named and...
Responsibilities are obligations of an object in terms of its behaviour.<br />Doing:<br />Doing something itself, such as ...
Domain Model<br />It’s all very well in practice, but it will never work in theory.<br />-Management Maxim<br />
Who is responsible for creating SalesLineItem?<br />
Who should be responsible for creating a new instance of some class?<br />Assign class B the responsibility to create an i...
How to determine the grandTotal?<br />
What is the general principle of assigning responsibilities to objects?<br />Assign a responsibility to the information ex...
How to Save the Sale to Database?<br />SaleRepository<br />Sale Load(intsaleId)<br />void Save(Sale sale)<br />…<br />
What object should have the responsibility, when not to violate High Cohesion and Low Coupling?<br />Assign a highly cohes...
How to avoid direct coupling between Sale and External Tax Systems? <br />
Where to assign a responsibility, to avoid direct coupling between two things? <br />Assign the responsibility to an inter...
How to handle multiple Payment Types?<br />
How handle alternatives based on type? <br />When alternate behaviours are selected based on the type of an object, use po...
How to design objects, subsystems, and systems so that the variations or instability in these elements does not have an un...
Controller<br />High Cohesion<br />Low Coupling<br />Law of Demeter<br />Principle of Least Knowledge<br />Only talk to yo...
References<br />Measuring programming progress by lines of code <br />is like measuring aircraft building progress by weig...
Thank you<br />Intelligence is the ability to avoid doing work,<br /> yet getting the work done. <br />-LinusTorvalds<br />
Upcoming SlideShare
Loading in...5
×

Patterns of Assigning Responsibilities

1,330

Published on

Presentation prepared for xConf Conference at Thoughtworks, Chennai.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,330
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • Common Relationships - Composite aggregates Part - Container contains Content - Recorder records RecordedA composite object is an excellent candidate to make its parts.When creation requires significant complexity, delegate creation to a helper class called Concrete Factory or Abstract Factory.Benefits: Low coupling is supported
  • Frequently used in the assignment of responsibilities.Undesirable in some situations, because of problems in coupling and cohesion.Eg: Who should be responsible for saving a sale in a database?By Expert, it should be in Sale class, which leads to cohesion, coupling and duplication problem.Benefits: - Information encapsulation is maintained, which supports low coupling. - Distribution of behaviour across the classes, thus encouraging more cohesive “lightweight” class definitions.
  • What object should have the responsibility, when you do not wantto violate High Cohesion and Low Coupling, or other goals, but solutions offered by Expert (for example) are not appropriate?Pure Fabrication – Idiom – implies making something up.The responsibilities are shifted from the Expert class to the Pure Fabrication.Design of Objects:Representational Decomposition – Represents things in DomainBehavioral Decomposition - AlgorithmsPreserves low coupling and high cohesion.Virtually, all design patterns are Pure Fabrications.
  • Where to assign a responsibility, to avoid direct coupling between two things? How to de-couple objects so that low coupling is supported and reuse potential remains higher?Pure Fabrications are generated because of Indirection.
  • Very important, fundamental principle of software design.Eg:Data Driven DesignsService LookupUniform AccessStandard LanguagesTwo points of change: Variation Point Evolution PointInformation Hiding: Hide Information about the design from other modules, at the points of difficulty or likely change.Open Closed Principle: Modules should be both open (for extension; adaptable) and closed (the module is closed to modification in ways that affect clients.)ContraIndications and Benefits are same as Polymorphism.
  • Controller:What first object beyond the UI layer receives and coordinates (“Controls”) a system operation?Assign the responsibility to an object representing one of these choicesRepresents the overall “system”, a “root object” (façade controller)Represents a use case scenario (session controller)Law of DemeterWithin a method, messages should only be sent to the following objectsThe this object (or self)A parameter of the methodAn attribute of thisAn element of a collection which is an attribute of this.An object created within the method.
  • Patterns of Assigning Responsibilities

    1. 1. AmirdhaGopalRajendran<br />X-Conf <br />Thoughtworks, Chennai<br />Patterns of assigning Responsibilities<br />The critical design tool for software development is <br />a MIND well educated in DESIGN PRINCIPLES. <br />– Craig Larman<br />
    2. 2. Setting the Context<br />Pattern<br />Responsibility<br />Domain Model<br />Patterns<br />Creator<br />Information Expert<br />Pure Fabrication<br />Indirection<br />Polymorphism<br />Protected Variation<br />Related Patterns<br />References<br />Introduction<br />Life can only be understood backwards, but it must be lived forwards.<br />– Soren Kierkegaard<br />
    3. 3. Pattern is an abstractsolution to a recurring problem, in a particular context.<br />Larman: A good pattern is a named and well-knownproblem/solution pair that can be applied in new contexts.<br />Design Pattern: <br />Notto express new design ideas. <br />Codify Existing tried-and-true knowledge, idioms and principles<br />GRASP - General Responsibility Assignment Software Patterns or Principles. <br />Pattern<br />One person’s pattern is another person’s primitive building block.<br />– GoF<br />
    4. 4. Responsibilities are obligations of an object in terms of its behaviour.<br />Doing:<br />Doing something itself, such as creating an object or doing a calculation<br />Initiating action in other objects<br />Controlling and coordinating activities in other objects.<br />Knowing:<br />Knowing about private encapsulated data<br />Knowing about related objects<br />Knowing about things it can derive or calculate<br />Responsibility<br />Understanding responsibilities is key to good object-oriented design. <br />-Martin Fowler<br />
    5. 5. Domain Model<br />It’s all very well in practice, but it will never work in theory.<br />-Management Maxim<br />
    6. 6. Who is responsible for creating SalesLineItem?<br />
    7. 7. Who should be responsible for creating a new instance of some class?<br />Assign class B the responsibility to create an instance of class A if one or more of the following is true:<br />B aggregates A<br />B contains A<br />B records instance of A objects<br />B closely use A objects<br />B passes initializing data to A<br />Delegate complex creation logic to Concrete Factory or Abstract Factory.<br />Benefits<br />Low Coupling<br />Creator<br />
    8. 8. How to determine the grandTotal?<br />
    9. 9. What is the general principle of assigning responsibilities to objects?<br />Assign a responsibility to the information expert – the class that has the information necessary to fulfill the responsibility.<br />ContraIndication<br />Undesirable in some situations, because of coupling and cohesion<br />Benefits<br />Information Encapsulation<br />Distribution of behaviour – more cohesive “lightweight” class definitions<br />Information Expert<br />
    10. 10. How to Save the Sale to Database?<br />SaleRepository<br />Sale Load(intsaleId)<br />void Save(Sale sale)<br />…<br />
    11. 11. What object should have the responsibility, when not to violate High Cohesion and Low Coupling?<br />Assign a highly cohesive set of responsibilities to an artificial class, that does not represent a problem domain, in order to support high cohesion, low coupling, and reuse.<br />Design of Objects<br />Representational Decomposition – Domain Things<br />Behavioral Decomposition – Algorithms<br />ContraIndications<br />Overuse – too many behavior objects.<br />Benefits<br />High Cohesion<br />Reuse potential because of fine-grained Pure Fabrication.<br />Virtually, all design patterns are Pure Fabrications<br />Pure Fabrication<br />
    12. 12. How to avoid direct coupling between Sale and External Tax Systems? <br />
    13. 13. Where to assign a responsibility, to avoid direct coupling between two things? <br />Assign the responsibility to an intermediate object to mediate between other components or services so that they are not directly coupled.<br />Benefits<br />Low Coupling<br />Many pure fabrications are generated because of Indirection<br />Many Existing design patterns are specialization of Indirection.<br />Indirection<br />Most problems in computer science can be solved by <br />another level of indirection.<br />Most problems in performance can be solved by <br />removing another level of indirection .<br />
    14. 14. How to handle multiple Payment Types?<br />
    15. 15. How handle alternatives based on type? <br />When alternate behaviours are selected based on the type of an object, use polymorphic method call to select the behaviour, rather than using if statement to test the type.<br />“Giving the same name to services in different objects”<br />ContraIndications<br />Speculative “future-proofing” – unnecessary effort<br />Benefits:<br />New variations are easy to add.<br />Clients are not affected.<br />Polymorphism<br />
    16. 16. How to design objects, subsystems, and systems so that the variations or instability in these elements does not have an undesirable impact on other elements?<br />Identify points of predicted variation or instability; assign responsibilities to create a stable interface around them.<br />Very important, fundamental principle of software design.<br />Points of Change (Hot spots) <br /> Variation Point, Evolution Point<br />Mechanisms motivated by PV<br />Service Lookups, Virtual Machines, Standard Languages, etc…<br />Same as Information Hiding and Open-Closed Priciple.<br />Protected Variation<br />
    17. 17. Controller<br />High Cohesion<br />Low Coupling<br />Law of Demeter<br />Principle of Least Knowledge<br />Only talk to your immediate friends<br />Related Patterns<br />Not everything that can be counted counts, <br />and not everything that counts can be counted.<br />-Albert Einstein<br />
    18. 18. References<br />Measuring programming progress by lines of code <br />is like measuring aircraft building progress by weight<br />-Bill Gates<br />
    19. 19. Thank you<br />Intelligence is the ability to avoid doing work,<br /> yet getting the work done. <br />-LinusTorvalds<br />

    ×