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
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 />
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 />
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 />
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 />
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 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 />
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 />
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 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 />
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 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 />
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 />
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 />
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 />
References<br />Measuring programming progress by lines of code <br />is like measuring aircraft building progress by weight<br />-Bill Gates<br />
Thank you<br />Intelligence is the ability to avoid doing work,<br /> yet getting the work done. <br />-LinusTorvalds<br />