Design Patterns


Published on

This is a part of my series of presentations on C++ and Design Pattern. This is still being developed.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Cultural-Revolution (1966-76) excesses exposed on Mao's death (1976)
  • Design Patterns

    1. 1. February 28, 2009 Design Pattern Dr. Partha Pratim Das Interra Systems (India) Pvt. Ltd. Abstractions in OOD Current Practices in Object Oriented Software Development Meghnad Saha Institute of Technology
    2. 2. Agenda <ul><li>Design Patterns </li></ul><ul><ul><li>Patterns Everywhere </li></ul></ul><ul><ul><li>Fundamentals </li></ul></ul><ul><ul><li>Sample Pattern Overview </li></ul></ul>
    3. 3. Pattern’s Everywhere Quest in Human Endeavor
    4. 4. Pattern’s Everywhere in Life <ul><ul><ul><li>Summer, Winter, Autumn, Spring: </li></ul></ul></ul><ul><ul><li>Climatic Pattern </li></ul></ul><ul><ul><ul><li>New Moon, Full Moon: </li></ul></ul></ul><ul><ul><li>Lunar Pattern </li></ul></ul><ul><ul><ul><li>Iron Filings in a Magnetic Fields: </li></ul></ul></ul><ul><ul><li>Magnetic Pattern </li></ul></ul><ul><li>Patterns in Nature </li></ul>
    5. 5. Pattern’s Everywhere in Life <ul><ul><ul><li>Bhairavi, Malkosh, Hansadhwani: </li></ul></ul></ul><ul><ul><li>Musical Pattern </li></ul></ul><ul><ul><ul><li>Sonnet: </li></ul></ul></ul><ul><ul><li>Poetic Pattern </li></ul></ul><ul><ul><ul><li>Bibi #1, Hero #1, Jodi #1, Premi #1: </li></ul></ul></ul><ul><ul><li>Bollywood Pattern </li></ul></ul><ul><ul><ul><li>Hesh toric Win, The Damned, Grave end to Mutiny: </li></ul></ul></ul><ul><ul><li>Media Pattern </li></ul></ul><ul><ul><ul><li>Simple, Complex, Compound Sentences: </li></ul></ul></ul><ul><ul><li>Grammatical Pattern </li></ul></ul><ul><li>Patterns in Art / Humanities </li></ul>
    6. 6. Pattern’s Everywhere in Life <ul><ul><ul><li>4-3-3, 5-2-3, 10: </li></ul></ul></ul><ul><ul><li>Soccer Pattern </li></ul></ul><ul><ul><ul><li>Castling: </li></ul></ul></ul><ul><ul><li>Chess Pattern </li></ul></ul><ul><li>Patterns in Sport </li></ul>
    7. 7. Pattern’s Everywhere in Life <ul><ul><ul><li>BTK, Stone Man: </li></ul></ul></ul><ul><ul><li>Serial Killer Pattern </li></ul></ul><ul><ul><ul><li>9/11, 7/11, 26/11: </li></ul></ul></ul><ul><ul><li>Terror Pattern </li></ul></ul><ul><ul><ul><li>Mughal, Gandhi: </li></ul></ul></ul><ul><ul><li>Dynastic Pattern </li></ul></ul><ul><li>Patterns in Crime / Politics </li></ul>
    8. 8. Pattern’s Everywhere in Life <ul><ul><ul><li>Baluchari, Benarasi, Pachhampalli: </li></ul></ul></ul><ul><ul><li>Silk Pattern </li></ul></ul><ul><ul><ul><li>Palace, House, Apartment, Condo: </li></ul></ul></ul><ul><ul><li>Housing Pattern </li></ul></ul><ul><ul><ul><li>Recipe: </li></ul></ul></ul><ul><ul><li>Cooking Pattern </li></ul></ul><ul><ul><ul><li>Times New Roman, Arial, Verdana, Tahoma: </li></ul></ul></ul><ul><ul><li>Typesetting Pattern </li></ul></ul><ul><li>Patterns in Construction </li></ul>
    9. 9. Pattern’s Everywhere in Life <ul><ul><ul><li>Jiang Qing, Zhang Chunqiao, Yao Wenyuan, Wang Hongwen </li></ul></ul></ul><ul><ul><ul><ul><li>Cultural-Revolution excesses exposed on Mao's death </li></ul></ul></ul></ul><ul><ul><ul><li>Wang Dongxing, Wu De, Ji Dengkui, Chen Xilian </li></ul></ul></ul><ul><ul><ul><ul><li>Hua Guofeng's clique; Crushed in 1980 by Deng Xiaoping </li></ul></ul></ul></ul><ul><ul><li>Corrupt Collusion Pattern </li></ul></ul><ul><ul><ul><li>Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides </li></ul></ul></ul><ul><ul><ul><ul><li>GoF Book on Pattern </li></ul></ul></ul></ul><ul><ul><li>Creative Consortium Pattern </li></ul></ul><ul><li>Patterns in Collaboration </li></ul>
    10. 10. Pattern’s Everywhere in Life <ul><ul><ul><li>Semi-Group, Monoid, Group, Abelian Group: </li></ul></ul></ul><ul><ul><li>Algebraic Pattern </li></ul></ul><ul><ul><ul><li>Unconditional Branch, Conditional Branch, Definite Loop, Indefinite Loop, Recursion: </li></ul></ul></ul><ul><ul><li>Programming Pattern </li></ul></ul><ul><ul><ul><li>Divide-and-Conquer, Greedy, Dynamic Programming, Branch-and-Bound: </li></ul></ul></ul><ul><ul><li>Algorithmic Pattern </li></ul></ul><ul><ul><ul><li>Factory, Iterator, Visitor, Singleton: </li></ul></ul></ul><ul><ul><li>Design Pattern </li></ul></ul><ul><li>Patterns in Computation </li></ul>
    11. 11. Pattern Pioneers Kurt Godel (1906-1978) Austrian-American Logician Who is he? This sentence is false
    12. 12. Pattern Pioneers Who is he? Champion of Graphic Patterns
    13. 13. Pattern Pioneers Maurits Cornelis Escher (1898-1972) Dutch Graphic Artist
    14. 14. Pattern Pioneers Johann Sebastian Bach (1685-1750) German Composer & Organist Who is he?
    15. 15. Pattern Pioneers Robert James Fisher (1943-2008) US Chess World Champ Who is he?
    16. 16. Pattern Pioneers Who is he? Donald Ervin Knuth (1938-) Father of Analysis of Algorithms
    17. 17. Pattern Pioneers There is one timeless way of building. It is a thousand years old, and the same today as it has ever been. The great traditional buildings of the past, the villages and tents and temples in which man feels at home, have always been made by people who were very close to the center of this way. It is not possible to make great buildings, or great towns, beautiful places, places where you feel yourself, places where you feel alive, except by following this way. And, as you will see, this way will lead anyone who looks for it to buildings which are themselves as ancient in their form, as the trees and hills, and as our faces are. Who is he? Christopher Alexander (1936-) Architect – The Pattern Language
    18. 18. Patterns in Life: Take Back <ul><li>Patterns Pervade </li></ul><ul><li>Patterns Drive Research </li></ul><ul><li>Patterns are the greatest exploits for Design </li></ul>
    19. 19. Design Patterns Search of Similarities in OOD
    20. 20. What is a Design Pattern? <ul><li>A Design Pattern </li></ul><ul><ul><li>describes a problem </li></ul></ul><ul><ul><ul><li>Occurring over and over again (in software engineering) </li></ul></ul></ul><ul><ul><li>describes the solution </li></ul></ul><ul><ul><ul><li>Sufficiently generic </li></ul></ul></ul><ul><ul><ul><li>Applicable in a wide variety of contexts. </li></ul></ul></ul>Recurring Solution to a Recurring Problem
    21. 21. Essential Elements of a Design Pattern <ul><li>Pattern Name: Descriptor for a design problem, its solutions, and their consequences. </li></ul><ul><li>Problem: Where to apply the pattern and its associated context. </li></ul><ul><li>Solution: Elements that make up the design, their relationships, responsibilities, and collaborations. </li></ul><ul><li>Consequences: Results and trade-offs in applying the pattern. </li></ul>
    22. 22. Example Pattern <ul><li>Pattern Name </li></ul><ul><ul><li>Iterator </li></ul></ul><ul><li>Problem </li></ul><ul><ul><li>How to serve Patients at a Doctor’s Clinic </li></ul></ul><ul><li>Solution </li></ul><ul><ul><li>Front-desk manages the order for patients to be called </li></ul></ul><ul><ul><ul><li>By Appointment </li></ul></ul></ul><ul><ul><ul><li>By Order of Arrival </li></ul></ul></ul><ul><ul><ul><li>By Extending Gratitude </li></ul></ul></ul><ul><ul><ul><li>By Exception </li></ul></ul></ul><ul><ul><ul><li>… </li></ul></ul></ul><ul><li>Consequences </li></ul><ul><ul><li>Patient Satisfaction </li></ul></ul><ul><ul><li>Clinic’s Efficiency </li></ul></ul><ul><ul><li>Doctor’s Productivity </li></ul></ul>
    23. 23. Describing a Design Pattern <ul><li>Pattern Name and Classification </li></ul><ul><ul><li>The pattern's name conveys the essence of the pattern succinctly. </li></ul></ul><ul><li>Intent </li></ul><ul><ul><li>A short statement on: </li></ul></ul><ul><ul><ul><li>What does the design pattern do? </li></ul></ul></ul><ul><ul><ul><li>What is its rationale and intent? </li></ul></ul></ul><ul><ul><ul><li>What particular design issue or problem does it address? </li></ul></ul></ul><ul><li>Also Known As </li></ul><ul><ul><li>Other well-known names for the pattern. </li></ul></ul>
    24. 24. Describing a Design Pattern <ul><li>Motivation </li></ul><ul><ul><li>A scenario that illustrates a design problem and how the class and object structures in the pattern solve the problem. </li></ul></ul><ul><li>Applicability </li></ul><ul><ul><li>What are the situations in which the design pattern can be applied? </li></ul></ul><ul><ul><li>What are examples of poor designs that the pattern can address? </li></ul></ul><ul><ul><li>How can you recognize these situations? </li></ul></ul>
    25. 25. Describing a Design Pattern <ul><li>Structure </li></ul><ul><ul><li>A graphical representation of the classes in the pattern using a notation based on the Object Modeling Technique (OMT) – specifically UML. </li></ul></ul><ul><li>Participants </li></ul><ul><ul><li>The classes and/or objects participating in the design pattern and their responsibilities. </li></ul></ul><ul><li>Collaborations </li></ul><ul><ul><li>How the participants collaborate to carry out their responsibilities? </li></ul></ul>
    26. 26. Describing a Design Pattern <ul><li>Consequences </li></ul><ul><ul><li>How does the pattern support its objectives? </li></ul></ul><ul><ul><li>What are the trade-offs and results of using the pattern? </li></ul></ul><ul><ul><li>What aspect of system structure does can be varied independently? </li></ul></ul><ul><li>Implementation </li></ul><ul><ul><li>What pitfalls, hints, or techniques should you be aware of when implementing the pattern? </li></ul></ul><ul><ul><li>Are there language-specific issues? </li></ul></ul>
    27. 27. Describing a Design Pattern <ul><li>Sample Code </li></ul><ul><ul><li>Code fragments to implement the pattern in specific language (C++ or C# or Java). </li></ul></ul><ul><li>Known Uses </li></ul><ul><ul><li>Examples of the pattern found in real systems. </li></ul></ul><ul><li>Related Patterns </li></ul><ul><ul><li>What design patterns are closely related to this one? </li></ul></ul><ul><ul><li>What are the important differences? </li></ul></ul><ul><ul><li>With which other patterns should this one be used? </li></ul></ul>
    28. 28. Iterator Pattern <ul><li>Pattern Name and Classification: </li></ul><ul><ul><li>Iterator </li></ul></ul><ul><ul><li>Behavioral </li></ul></ul><ul><li>Intent </li></ul><ul><ul><li>Provide a way to access </li></ul></ul><ul><ul><li>the elements </li></ul></ul><ul><ul><li>of an aggregate object (container) </li></ul></ul><ul><ul><li>sequentially </li></ul></ul><ul><ul><li>without exposing its underlying representation. </li></ul></ul><ul><li>CONTAINERS </li></ul><ul><li>Array </li></ul><ul><li>Vector </li></ul><ul><li>List </li></ul><ul><li>Stack </li></ul><ul><li>Queue </li></ul><ul><li>Tree </li></ul><ul><li>SEQUENTIAL </li></ul><ul><li>Forward </li></ul><ul><li>Backward </li></ul><ul><li>Bidirectional </li></ul><ul><li>Random </li></ul><ul><li>ACCESS </li></ul><ul><li>Read </li></ul><ul><li>Write </li></ul><ul><li>Read-Write </li></ul>
    29. 29. Iterator Pattern <ul><li>Also Known As </li></ul><ul><ul><li>Cursor </li></ul></ul><ul><li>Motivation </li></ul><ul><ul><li>An aggregate object (list) should have a way to access its elements without exposing its internal structure. </li></ul></ul><ul><ul><li>There is a need to traverse the list in different ways, depending on a specific task. </li></ul></ul><ul><ul><li>Multiple traversals may be pending on the same list. </li></ul></ul><ul><ul><li>The key idea in this pattern is to take the responsibility for access and traversal out of the list object and put it into an iterator object. </li></ul></ul>
    30. 30. Iterator Pattern <ul><li>Motivation </li></ul>
    31. 31. Iterator Pattern <ul><li>Motivation </li></ul>
    32. 32. Iterator Pattern <ul><li>Applicability </li></ul><ul><ul><li>to access an aggregate object's contents without exposing its internal representation. </li></ul></ul><ul><ul><li>to support multiple traversals of aggregate objects. </li></ul></ul><ul><ul><li>to provide a uniform interface for traversing different aggregate structures (that is, to support polymorphic iteration). </li></ul></ul>
    33. 33. Iterator Pattern <ul><li>Structure </li></ul>
    34. 34. Iterator Pattern <ul><li>Participants </li></ul><ul><ul><li>Iterator </li></ul></ul><ul><ul><ul><li>defines an interface for accessing and traversing elements. </li></ul></ul></ul><ul><ul><li>ConcreteIterator </li></ul></ul><ul><ul><ul><li>implements the Iterator interface. </li></ul></ul></ul><ul><ul><ul><li>keeps track of the current position in the traversal of the aggregate. </li></ul></ul></ul><ul><ul><li>Aggregate </li></ul></ul><ul><ul><ul><li>defines an interface for creating an Iterator object. </li></ul></ul></ul><ul><ul><li>ConcreteAggregate </li></ul></ul><ul><ul><ul><li>implements the Iterator creation interface to return an instance of the proper ConcreteIterator. </li></ul></ul></ul>
    35. 35. Iterator Pattern <ul><li>Collaborations </li></ul><ul><ul><li>A ConcreteIterator keeps track of the current object in the aggregate and can compute the succeeding object in the traversal. </li></ul></ul><ul><li>Consequences </li></ul><ul><ul><li>It supports variations in the traversal of an aggregate. </li></ul></ul><ul><ul><li>Iterators simplify the Aggregate interface. </li></ul></ul><ul><ul><li>More than one traversal can be pending on an aggregate. </li></ul></ul>
    36. 36. Iterator Pattern <ul><li>Implementation </li></ul><ul><ul><li>Who controls the iteration? </li></ul></ul><ul><ul><ul><li>External Iteration </li></ul></ul></ul><ul><ul><ul><li>Internal Iteration </li></ul></ul></ul><ul><ul><li>Who defines the traversal algorithm? </li></ul></ul><ul><ul><li>How robust is the iterator? </li></ul></ul><ul><ul><ul><li>What happens if items are inserted / deleted? </li></ul></ul></ul><ul><ul><li>Additional Iterator operations </li></ul></ul><ul><ul><li>Using polymorphic iterators in C++. </li></ul></ul><ul><ul><ul><li>allocated dynamically by a factory method </li></ul></ul></ul><ul><ul><ul><li>the client is responsible for deleting them </li></ul></ul></ul><ul><ul><li>Iterators may have privileged access </li></ul></ul><ul><ul><ul><li>friend class </li></ul></ul></ul><ul><ul><li>Iterators for composites </li></ul></ul><ul><ul><li>Null iterators </li></ul></ul>
    37. 37. Iterator Pattern <ul><li>Sample Code – Follows in the next few slides. </li></ul>
    38. 38. Iterator Pattern <ul><li>template <class Item> </li></ul><ul><li>class List { </li></ul><ul><li>public: </li></ul><ul><li>List(long size = DEFAULT_LIST_CAPACITY); </li></ul><ul><li>long Count() const; </li></ul><ul><li>Item& Get(long index) const; </li></ul><ul><li>// ... </li></ul><ul><li>}; </li></ul>
    39. 39. Iterator Pattern <ul><li>template <class Item> </li></ul><ul><li>class Iterator { </li></ul><ul><li>public: </li></ul><ul><li>virtual void First() = 0; </li></ul><ul><li>virtual void Next() = 0; </li></ul><ul><li>virtual bool IsDone() const = 0; </li></ul><ul><li>virtual Item CurrentItem() const = 0; </li></ul><ul><li>protected: </li></ul><ul><li>Iterator(); </li></ul><ul><li>}; </li></ul>
    40. 40. Iterator Pattern <ul><li>template <class Item> </li></ul><ul><li>class ListIterator : public Iterator<Item> { </li></ul><ul><li>public: </li></ul><ul><li>ListIterator(const List<Item>* aList); </li></ul><ul><li>virtual void First(); </li></ul><ul><li>virtual void Next(); </li></ul><ul><li>virtual bool IsDone() const; </li></ul><ul><li>virtual Item CurrentItem() const; </li></ul><ul><li>private: </li></ul><ul><li>const List<Item>* _list; </li></ul><ul><li>long _current; </li></ul><ul><li>}; </li></ul>
    41. 41. Iterator Pattern <ul><li>template <class Item> </li></ul><ul><li>ListIterator<Item>::ListIterator ( </li></ul><ul><li>const List<Item>* aList </li></ul><ul><li>) : _list(aList), _current(0) { </li></ul><ul><li>} </li></ul><ul><li>template <class Item> </li></ul><ul><li>void ListIterator<Item>::First () { </li></ul><ul><li>current = 0; </li></ul><ul><li>} </li></ul>
    42. 42. Iterator Pattern <ul><li>template <class Item> </li></ul><ul><li>void ListIterator<Item>::Next () { </li></ul><ul><li>current++; </li></ul><ul><li>} </li></ul><ul><li>template <class Item> </li></ul><ul><li>bool ListIterator<Item>::IsDone () const { </li></ul><ul><li>return _current >= _list->Count(); </li></ul><ul><li>} </li></ul>
    43. 43. Iterator Pattern <ul><li>template <class Item> </li></ul><ul><li>Item ListIterator<Item>::CurrentItem () const { </li></ul><ul><li>if (IsDone()) { </li></ul><ul><li>throw IteratorOutOfBounds; </li></ul><ul><li>} </li></ul><ul><li>return _list->Get(_current); </li></ul><ul><li>} </li></ul>
    44. 44. Iterator Pattern <ul><li>void PrintEmployees (Iterator<Employee*>& i) { </li></ul><ul><li>for (i.First(); !i.IsDone(); i.Next()) { </li></ul><ul><li>i.CurrentItem()->Print(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>List<Employee*>* employees; </li></ul><ul><li>// ... </li></ul><ul><li>ListIterator<Employee*> forward(employees); </li></ul><ul><li>ReverseListIterator<Employee*> backward(employees); </li></ul><ul><li>PrintEmployees(forward); </li></ul><ul><li>PrintEmployees(backward); </li></ul>
    45. 45. Iterator Pattern <ul><li>Known Uses </li></ul><ul><ul><li>Iterators are common in object-oriented systems </li></ul></ul><ul><li>Related Patterns </li></ul><ul><ul><li>Composite: </li></ul></ul><ul><ul><ul><li>Iterators are often applied to recursive structures such as Composites. </li></ul></ul></ul><ul><ul><li>Factory Method: </li></ul></ul><ul><ul><ul><li>Polymorphic iterators rely on factory methods to instantiate the appropriate Iterator subclass. </li></ul></ul></ul><ul><ul><li>Memento: </li></ul></ul><ul><ul><ul><li>Often used in conjunction with the Iterator pattern. An iterator can use a memento to capture the state of iteration. The iterator stores the memento internally. </li></ul></ul></ul>
    46. 46. Catalogue of Design Patterns (GoF)
    47. 48. Visitor Pattern <ul><li>Problem </li></ul><ul><ul><li>Consultant in an Office </li></ul></ul><ul><li>Solution </li></ul><ul><li>Consequences </li></ul>
    48. 49. Factory Pattern <ul><li>Problem </li></ul><ul><ul><li>Bread Maker </li></ul></ul><ul><li>Solution </li></ul><ul><li>Consequences </li></ul>
    49. 50. Façade Pattern <ul><li>Problem </li></ul><ul><ul><li>Façade Website </li></ul></ul><ul><li>Solution </li></ul><ul><li>Consequences </li></ul>
    50. 51. Singleton Pattern <ul><li>Problem: </li></ul><ul><ul><li>Managing entities with one-instance-at-a-time </li></ul></ul><ul><ul><li>Office of the Principal of MSIT </li></ul></ul><ul><ul><li>Heart in a Human Body </li></ul></ul><ul><ul><li>cout, cin, cerr, clog … </li></ul></ul><ul><li>Solution: </li></ul><ul><li>Consequences: </li></ul><ul><ul><li>Synchronization needed for multi-threaded use </li></ul></ul><ul><li>Note: </li></ul><ul><ul><li>Closely related to Monostate Pattern </li></ul></ul>
    51. 52. Thank You
    52. 53. Pattern’s Everywhere in Life <ul><li>Pattern in Nature </li></ul><ul><ul><li>Summer, Winter, Autumn: Climatic Pattern </li></ul></ul><ul><ul><li>New Moon, Full Moon: Lunar Pattern </li></ul></ul><ul><ul><li>Iron Filings in a Magnetic Fields: Magnetic Pattern </li></ul></ul><ul><li>Patterns in Art/ Humanities </li></ul><ul><ul><li>Bhairavi, Malkosh, Hansadhwani: Musical Pattern </li></ul></ul><ul><ul><li>Sonnet: Poetic Pattern </li></ul></ul><ul><ul><li>Bibi #1, Hero #1, Jodi #1, Premi #1: Bollywood Pattern </li></ul></ul><ul><ul><li>Hesh toric Win, The Damned: Media Pattern </li></ul></ul><ul><ul><li>Simple, Complex, Compound Sentences: Grammatical Pattern </li></ul></ul><ul><li>Patterns in Sport </li></ul><ul><ul><li>4-3-3, 5-2-3, 10: Soccer Pattern </li></ul></ul><ul><ul><li>Castling: Chess Pattern </li></ul></ul><ul><li>Patterns in Crime / Politics </li></ul><ul><ul><li>BTK, Stone Man: Serial Killer Pattern </li></ul></ul><ul><ul><li>9/11, 7/11, 26/11: Terror Pattern </li></ul></ul><ul><ul><li>Mughal, Gandhi: Dynastic Pattern </li></ul></ul><ul><li>Patterns in Construction </li></ul><ul><ul><li>Baluchari, Benarasi, Pachhampalli: Silk Pattern </li></ul></ul><ul><ul><li>Palace, House, Apartment, Condo: Housing Pattern </li></ul></ul><ul><ul><li>Recipe: Cooking Pattern </li></ul></ul><ul><ul><li>Times New Roman, Arial, Verdana, Tahoma: Typesetting Pattern </li></ul></ul><ul><li>Patterns in Computation </li></ul><ul><ul><li>Semi-Group, Monoid, Group, Abelian Group: Algebraic Pattern </li></ul></ul><ul><ul><li>Unconditional Branch, Conditional Branch, Definite Loop, Indefinite Loop, Recursion: Programming Pattern </li></ul></ul><ul><ul><li>Divide-and-Conquer, Greedy, Dynamic Programming, Branch-and-Bound: Algorithmic Patterny </li></ul></ul><ul><ul><li>Factory, Iterator, Visitor, Singleton: Design Pattern </li></ul></ul>