Escolar Documentos
Profissional Documentos
Cultura Documentos
K. Meena SymIndia Training & Consultancy Pvt Ltd Bangalore www.symindia.com meena@symindia.com
SymIndia
Design Patterns/1
Coverage
Need
Features
SymIndia
Problem Solving
Repairing your bike Making Coffee Fixing a leak in a pipe Can we afford to? Should we re-use the learning in future?
Does this mean that our creativity is curbed?
Do we get accustomed to common terms? When do we say that the solution is elegant?
Design Patterns - Intro/3
SymIndia
Why Patterns ?
Solve "real world" problems Capture domain expertise Document design decisions and rationale Reuse wisdom and experience of master practitioners Explain "Best-fits" for the given set of concerns/trade-offs Form a shared vocabulary for problemsolving discussion
Design Patterns - Intro/4
SymIndia
Need contd
Forces (trade-off alternatives, misfits, goals+constraints) Resolution (how and why the solution balances the forces)
SymIndia
Conclusion
Why re-invent the wheel? Avoid re-designing, at least minimize Not every problem needs to be solved from first principles!
SymIndia
SymIndia
James Coplien, Advanced C++ Idioms book, 19891991 Gamma, Helm, Johnson, Vlissides ("Gang of Four GoF)
Definitions
a fully realized form, original, or model accepted or proposed for imitation[dictionary] the abstraction from a concrete form which keeps recurring in specific non-arbitrary contexts [Riehle] both a thing and the instructions for making the thing [Coplien] ...a literary format for capturing the wisdom and
SymIndia
SymIndia
Overview of Patterns
Present solutions to common software problems arising within a certain context Help resolve key software design forces Flexibility Extensibility Dependability Predictability Scalability Efficiency
Capture recurring structures & dynamics among software participants to facilitate reuse of successful designs
AbstractService
service Client
Generally codify expert knowledge of design strategies, constraints & best practices
Proxy
Service
1 1
service
service
Data Structures and Algorithms are used in the implementation of Patterns They solve more fine-grained computational problems
Sorting, Searching Focus on efficiency and optimization tradeoffs and compromises, communicating commonality
SymIndia
FrameWork
A framework is a set of cooperating classes that make up a reusable design for a specific class of software The framework captures the design decisions that are common to its application domain A developer customizes a framework to a particular application by sub-classing and composing instances of framework classes A single framework typically encompasses several design patterns
SymIndia
Contd
Frameworks are the physical realization of one or more software pattern solutions
SymIndia
Patterns do
SymIndia
Solutions that have worked only once Providing exact solutions Abstract Principles or Heuristics Universally applicable in all contexts Untested Ideas/theories Panacea Restricted to Software/OO design
Design Patterns - Intro/17
SymIndia
Just because something is not a pattern does not mean it is not any good! If something is a pattern, then (hopefully) it is good!
SymIndia
Ingredients
Pattern Context a design situation giving rise to a design problem Problem a set of forces occuring in that context Solution a form or rule that can be applied to resolve these forces
forces
solution
in every room, make at least one window into a window place SymIndia
SymIndia
GoF format
Pattern name and classification Intent what does pattern do / when the solution works Also known as other known names of pattern (if any) Motivation the design problem / how class and object structures solve the problem
SymIndia
Design Patterns - Intro/21
GOF Format
Applicability situations where pattern can be applied Structure a graphical representation of classes in the pattern Participants the classes/objects participating and their responsibilities Collaborations of the participants to carry out responsibilities
SymIndia
GoF format
Consequences trade-offs, concerns Implementation hints, techniques Sample code code fragment showing possible implementation
SymIndia
Design Patterns - Intro/23
GOF Format
Known uses patterns found in real systems Related patterns closely related patterns
SymIndia
DP catalog - GoF
Purpose
Creational
Class
Behavioral Interpreter Chain of Responsibility Command Iterator Mediator Memento Observer State Strategy Visitor Template method
Scope Object
SymIndia
Requirements
Estimate amount of money that employee can expect to see in his/her next paycheck Complex calculations based on varying country and local taxes + retirement contributions Operates in London/NY and may open up other offices over the next few years Predicted that the Personnel Manager had plans for the introduction of a second retirement plan
SymIndia
UKLondonNoRetirement +GetTakeHome()
Approach 2
<<Interface>> TakeHomePay +GetTakeHome()
CommonTakeHomePay +GetTakeHome()
Decorator +GetTakeHome()
LondonTax
NYCTax +GetTakeHome()
UKTax +GetTakeHome()
USATax +GetTakeHome()
RetirementPlan +GetTakeHome()
SymIndia
+GetTakeHome()
Approach #2
<<Interface>> TakeHomePay +GetTakeHome() *
1
UKLondonTaxNoRetirement +GetTakeHome() UKLondonTaxRetirementA +GetTakeHome() UKLondonTaxRetirementB +GetTakeHome() USNYCTaxNoRetirement +GetTakeHome() USNYCTaxRetirementA +GetTakeHome() USNYCTaxRetirementB +GetTakeHome() USMiamiTaxNoRetirement
Employee
+GetTakeHome() USMiamiTaxRetirementA +GetTakeHome() USMiamiTaxRetirementB +GetTakeHome()
Decorator +GetTakeHome()
+GetTakeHome()
RetirementPlanA +GetTakeHome()
RetirementPlanB +GetTakeHome()
+GetTakeHome()
SymIndia
Some Terms
Class
Defines how an object is implemented Describes internal state and implementation of its operations Refers only to its interface Implementation Inheritance Share code and representation
Type/Interface
Class/Implementation Inheritance
Interface Inheritance
SymIndia
Implementation Inheritance
Merits
Inherit both WHAT and HOW If base class functionality is fine, Do nothing in the derived class Fragile Base Class problem
Demerits
Tight Coupling
A class, once published, cannot be altered New methods, Change in existing methods signature/body If a class is substituted with another , user code has to be recompiled
SymIndia
Interface Inheritance
Merits
Delink WHAT from HOW Skill sets to define are different from skill sets to implement Loose coupling
Demerits
User code works only with Interface Change in class does not impact user
Workaround in C++
SymIndia
Fundamental to Re-use
SymIndia
Benefits
Clients remain unaware of the specific types of objects they use. Clients remain unaware of the classes that implement the objects. Clients only know about abstract class(es) defining the interfaces Do not declare variables to be instances of particular concrete classes Use creational patterns to create actual objects.
Design Patterns - Intro/36
SymIndia
Re-use Mechanisms
Inheritance
Re-use by subclassing White-box Approach Assembling/composing objects to get complex functionality Black-box re-use
Composition
SymIndia
Inheritance
Merits
Easy to use Easy to override/enhance Cannot change implementations at run-time If parent class changes, sub classes are also to be changed
Demerits
SymIndia
Composition
Merits
Defined dynamically at run time Smaller class hierarchies More objects Mere assembling may not suffice
Demerits
SymIndia
Fundamental to Re-use
SymIndia
Delegation
Composition as powerful for reuse as inheritance
Two objects involved in handling requests Explicit object references Easy to compose behaviors at runtime
Window rectangle Area() Area() width height Rectangle
return rectangle->Area()
SymIndia
Typical Scenarios
While creating an object, we dont want to commit to a specific implementation A request is not always handled in just one way
SymIndia
Typical Scenarios
Client should not know anything about an objects representation, location or storage No tight coupling among objects Avoid too much of subclassing How to add new functionality to classes? How to add new responsibilities to specific objects?
Design Patterns - Intro/43
SymIndia
Commits to one way of satisfying a request Compile-time and runtime modifications to request handling can be simplified by avoiding hard-coded requests Patterns: Chain of Responsibility, Command
SymIndia
Clients dont know how an object is represented, stored, located, or implemented Hide information from clients to avoid cascading changes Patterns: Abstract factory, Bridge, Memento, Proxy
SymIndia
Tight coupling
Leads to monolithic systems Tightly coupled classes are hard to reuse in isolation Patterns: Abstract Factory, Bridge, Chain of Responsibility, Command, Facade, Mediator, Observer
SymIndia
Sources not available Change might require modifying lots of existing classes Patterns: Adapter, Decorator, Visitor
SymIndia
Relationships
Memento Proxy Builder saving state of iteration Iterator Enumerating children Composite Decorator adding respnsibilities to objects sharing composites defining grammar Interpreter Chain of Responsibility composed using Command Adapter Bridge
Flyweight
Visitor
Strategy
Mediator
Observer
Prototype
Template Method
Factory Method
Singleton
SymIndia
Questions?
SymIndia