Reuse at Design Level: Design Patterns – IV
Classification of Patterns (Cont..)
The Decorator: Object Diagram
The Decorator
A problem: Varying Implementations and Abstractions • In a collection hierarchy, on one hand, the abstractions vary – Collection – set, queue, stack • On the other hand, their implementations may also vary – array based, linked list based
A structural pattern: Bridge
Next problem: Some steps in an implementation can vary • In a hierarchy of classes, a behavior is quite common for all classes, but only that some steps are dependent on the nature of these classes • How to avoid redundancy in defining such behavior • Where should such a method be located?
In a Hierarchy: Template Method
The Template Method Pattern
A Problem: Separate collection and iterations over the collection D. K. Yadav • A collection supports members to add and remove elements, to check for membership • Iterations over the collection can be specified separately such that the two abstractions are not intermingled • The iterator hides the internal implementation of the collection • Multiple ways of iteration can be supported • Iterators can be used concurrently
Iterator Pattern
Next problem: An object alters its behavior as it changes its state D. K. Yadav • Example: – A TCP connection object provides methods such as open(), close(), send().. – The connection object changes the behavior of these methods as it changes its state from disconnected to listening to established to closed
TCP States through the State Pattern
The State Pattern
Next Problem: Use different algorithms for different situations in a given problem context: Strategy Pattern • Example: – A document is composed of text. Various line breaking algorithms can be used in formatting the document before printing • Consider the following strategies: – simple compose: determine line breaks, one line at a time – para compose: consider lines in an entire paragraph – array compose: each row has a fixed number of letters
The Solution
The Strategy Pattern

reuse at design level object oriented modelling design patterns

  • 1.
    Reuse at DesignLevel: Design Patterns – IV
  • 2.
  • 3.
  • 4.
  • 5.
    A problem: VaryingImplementations and Abstractions • In a collection hierarchy, on one hand, the abstractions vary – Collection – set, queue, stack • On the other hand, their implementations may also vary – array based, linked list based
  • 6.
  • 7.
    Next problem: Somesteps in an implementation can vary • In a hierarchy of classes, a behavior is quite common for all classes, but only that some steps are dependent on the nature of these classes • How to avoid redundancy in defining such behavior • Where should such a method be located?
  • 8.
    In a Hierarchy:Template Method
  • 9.
  • 10.
    A Problem: Separatecollection and iterations over the collection D. K. Yadav • A collection supports members to add and remove elements, to check for membership • Iterations over the collection can be specified separately such that the two abstractions are not intermingled • The iterator hides the internal implementation of the collection • Multiple ways of iteration can be supported • Iterators can be used concurrently
  • 11.
  • 12.
    Next problem: Anobject alters its behavior as it changes its state D. K. Yadav • Example: – A TCP connection object provides methods such as open(), close(), send().. – The connection object changes the behavior of these methods as it changes its state from disconnected to listening to established to closed
  • 13.
    TCP States throughthe State Pattern
  • 14.
  • 15.
    Next Problem: Usedifferent algorithms for different situations in a given problem context: Strategy Pattern • Example: – A document is composed of text. Various line breaking algorithms can be used in formatting the document before printing • Consider the following strategies: – simple compose: determine line breaks, one line at a time – para compose: consider lines in an entire paragraph – array compose: each row has a fixed number of letters
  • 16.
  • 17.