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