Your SlideShare is downloading. ×
0
Design Patterns Sisimon E S
Concepts <ul><li>Initiated in construction industry </li></ul><ul><li>“ A Pattern Language: Towns, Buildings, Construction...
Gang Of Four (GoF) <ul><li>“ Design Patterns: Elements of Reusable Object-Oriented Software” by Erich Gamma, Richard Helm,...
Definition by Christopher Alexander <ul><li>“ Each pattern describes a problem that occur over and over again in our envir...
How Design Patterns help? <ul><li>Set of solutions for a set of known problems. You just match your problem with one in th...
 
Categories of Design Patterns <ul><li>Creational Patters – Concerned about how objects are created. </li></ul><ul><li>Stru...
Some patterns <ul><li>Most of these solutions/patterns, you may feel, Oh I already know this solution. Yes that’s true, bu...
Composite Pattern <ul><li>The Document editor support tables which is created using  lines and texts. </li></ul><ul><li>Th...
Composite Pattern Example <ul><li>(a+b) – Object 1 </li></ul><ul><li>(a+b)*c – Object 2 = Ob1 * c </li></ul><ul><li>(a+b)*...
Composite Pattern Example 2 Graphic Draw() Add(Graphic) Remove(Graphic) Table Draw() Add(Graphic) Remove(Graphic) Line Dra...
Graphic Draw() Add(Graphic) Remove(Graphic) Picture Draw() Add(Graphic) Remove(Graphic) Graph Draw() 0..* Table Draw()
Strategy <ul><li>Need to support multiple formatting algorithms in the document editor. </li></ul><ul><li>Client cannot im...
Strategy(2) Format DoFormat()=0; FormatAlgo2 DoFormat() Editor FormatAlgo1 DoFormat()
Decorator <ul><li>Some of the windows need to have border. </li></ul><ul><li>Some of the windows need scroll bar. </li></u...
Decorator(2) <ul><li>A Decorator, also known as a Wrapper </li></ul><ul><li>A Decorator is an object that has an interface...
Decorator(3) <ul><li>class BorderWindows: public Window </li></ul><ul><li>{ </li></ul><ul><li>public: </li></ul><ul><li>Bo...
Abstract Factory <ul><li>Factories create product objects. </li></ul><ul><li>Provide interface for creating  families  of ...
Abstract Factory(2) <ul><li>Note:- Here client got a pointer to GUIFactory interface which can be Mac or Windows. Client d...
Bridge <ul><li>Take the above toolkit example. We have multiple existing window standard. But unfortunately  these librari...
Switch Example..
Bridge(2) MacWindowImp DeviceDrawText() DeviceDrawRect() WindowsImp DeviceDrawText()=0; DeviceDrawRect()=0; XWindowsImp De...
<ul><li>Sorry I don’t have code. Refer GoF’s book for a nice example, </li></ul>
Singleton <ul><li>Only one object instance, provide global access to it. </li></ul><ul><li>Many printers in a system, but ...
Singleton(2) <ul><li>See code Singleton.cpp </li></ul>
Upcoming SlideShare
Loading in...5
×

Design Patterns By Sisimon Soman

324

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
324
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Design Patterns By Sisimon Soman"

  1. 1. Design Patterns Sisimon E S
  2. 2. Concepts <ul><li>Initiated in construction industry </li></ul><ul><li>“ A Pattern Language: Towns, Buildings, Construction ” by Christopher Alexander. </li></ul><ul><li>Patten concepts came to Software Industry from this book. </li></ul><ul><li>Not specific to a programming language, but most software design patterns based on OOP. </li></ul>
  3. 3. Gang Of Four (GoF) <ul><li>“ Design Patterns: Elements of Reusable Object-Oriented Software” by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. </li></ul><ul><li>First book about Design Patterns in Software Engineering. </li></ul><ul><li>All diagrams and samples in coming slides taken from this book. </li></ul><ul><li>The Design Pattern concepts started in Software Engineering from this book. </li></ul>
  4. 4. Definition by Christopher Alexander <ul><li>“ Each pattern describes a problem that occur over and over again in our environment, </li></ul><ul><li>and describes the core solution to that problem, </li></ul><ul><li>in such a way we can use it millions of times, </li></ul><ul><li>with out doing the same way twice.” </li></ul>
  5. 5. How Design Patterns help? <ul><li>Set of solutions for a set of known problems. You just match your problem with one in the set. </li></ul><ul><li>Patterns help to decompose the module into small objects which is the toughest part in object oriented design. </li></ul><ul><li>Easy to maintain. Read from a Slashdot discussion about the advantage of Design Patterns – “A mechanic who knows very well about how cars work can repair all cars, even TATA’s NANO also  ” </li></ul>
  6. 7. Categories of Design Patterns <ul><li>Creational Patters – Concerned about how objects are created. </li></ul><ul><li>Structural Patterns – How objects are composed or structured. </li></ul><ul><li>Behavioral Patters – How objects communicate each other to implement a logic or algorithm. </li></ul>
  7. 8. Some patterns <ul><li>Most of these solutions/patterns, you may feel, Oh I already know this solution. Yes that’s true, but this is a documented/standard solution or pattern and has this name. </li></ul><ul><li>Composite (Structural) </li></ul><ul><li>Strategy (Behavioral) </li></ul><ul><li>Decorator (Structural) </li></ul><ul><li>Abstract Factory (Creational) </li></ul><ul><li>Bridge (Structural) </li></ul><ul><li>Singleton (Creational) </li></ul>
  8. 9. Composite Pattern <ul><li>The Document editor support tables which is created using lines and texts. </li></ul><ul><li>Then the editor support graphical diagrams that contains tables, graphs. (Again more complex pictures can form using these graphic diagrams) </li></ul><ul><li>It is too complex to consider each primitive graphic elements (like line, circle, text,..) individually. </li></ul><ul><li>Here the developer should consider diagrams as a unit rather than as a collection of individual graphic primitives. User should consider table as a whole instead of unstructured mass of text and lines. </li></ul>
  9. 10. Composite Pattern Example <ul><li>(a+b) – Object 1 </li></ul><ul><li>(a+b)*c – Object 2 = Ob1 * c </li></ul><ul><li>(a+b)*c/d – Object 2 / d </li></ul><ul><li>(a+b) * x = Object 1 * x </li></ul><ul><li>Option 1 – Calculate each expression independently, which is very complex and tough to maintain. </li></ul><ul><li>Option 2 - Group multiple objects and consider groups and individual objects uniformly. Simple and easy to maintain. </li></ul>
  10. 11. Composite Pattern Example 2 Graphic Draw() Add(Graphic) Remove(Graphic) Table Draw() Add(Graphic) Remove(Graphic) Line Draw() 0..* Rect Draw()
  11. 12. Graphic Draw() Add(Graphic) Remove(Graphic) Picture Draw() Add(Graphic) Remove(Graphic) Graph Draw() 0..* Table Draw()
  12. 13. Strategy <ul><li>Need to support multiple formatting algorithms in the document editor. </li></ul><ul><li>Client cannot implement all algorithms. </li></ul><ul><li>Algorithm implement in an object. The object accepts data/context object as parameter. </li></ul><ul><li>Make algorithms interchangeable. </li></ul><ul><li>Makes algorithms independent of the clients that use it. </li></ul><ul><li>Choice of algorithm implementation at run-time. </li></ul>
  13. 14. Strategy(2) Format DoFormat()=0; FormatAlgo2 DoFormat() Editor FormatAlgo1 DoFormat()
  14. 15. Decorator <ul><li>Some of the windows need to have border. </li></ul><ul><li>Some of the windows need scroll bar. </li></ul><ul><li>Can have ScrollableWindow, BorderedWindow, BordedWithScrollableWindow classes. </li></ul><ul><li>Having separate class for each specialized windows is more complex. </li></ul>
  15. 16. Decorator(2) <ul><li>A Decorator, also known as a Wrapper </li></ul><ul><li>A Decorator is an object that has an interface identical to an object that it contains. </li></ul><ul><li>Any calls that the decorator gets, it forward to the object that it contains, and adds its own functionality along the way, either before or after the call. </li></ul><ul><li>Attach additional responsibilities to a object (not class) dynamically. </li></ul><ul><li>Decorator provide flexible alternative to subclassing for extending functionality. </li></ul>
  16. 17. Decorator(3) <ul><li>class BorderWindows: public Window </li></ul><ul><li>{ </li></ul><ul><li>public: </li></ul><ul><li>BorderDecorator(Window *p): </li></ul><ul><li>virtual void Draw() </li></ul><ul><li>{ </li></ul><ul><ul><li>m_pWindow>Draw(); //Call the original implementation </li></ul></ul><ul><li>DrawBorder();// add extra functionality </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>TextWindow *pTextWnd = new TextWindow;// TextWindow derived from Window </li></ul><ul><li>BorderWindow *pBorderedWindow = new BorderWindow(pTextWnd); </li></ul><ul><li>pBorderedWindow->Draw();// Draw the text windows first, then draw the border above it. </li></ul><ul><li>ButtonWindow *pBtnWnd = new ButtonWindow;// ButtonWindow derived from Window </li></ul><ul><li>BorderWindows *pBorderedWindow2 = new BorderWindows(pBtnWnd); </li></ul><ul><li>pBorderedWindow->Draw();// Draw the button windows first, then draw the border above it. </li></ul>
  17. 18. Abstract Factory <ul><li>Factories create product objects. </li></ul><ul><li>Provide interface for creating families of related objects. </li></ul><ul><li>A UI toolkit support Windows and Mac look-and-feel and planning to add Linux type windows in future. </li></ul><ul><li>The toolkit should be independent of UI look-and-feel. </li></ul>
  18. 19. Abstract Factory(2) <ul><li>Note:- Here client got a pointer to GUIFactory interface which can be Mac or Windows. Client don’t know which type Window and where the GUIFactory pointer came from. </li></ul>MacWinFactory CreateScrollBar() CreateWindows() GUIFactory CreateScrollBar() = 0; CreateWindows()=0; WindowsWinFactory CreateScrollBar() CreateWindows() Client
  19. 20. Bridge <ul><li>Take the above toolkit example. We have multiple existing window standard. But unfortunately these libraries interfaces are not compatible each other (like one library support API to draw rectangle, but other library support API to draw line only, not rectangle). Then we cannot use Factory Pattern here. </li></ul><ul><li>Decouple the interfaces from its implementation. </li></ul><ul><li>No need to change client code to incorporate changes in implementation. </li></ul>
  20. 21. Switch Example..
  21. 22. Bridge(2) MacWindowImp DeviceDrawText() DeviceDrawRect() WindowsImp DeviceDrawText()=0; DeviceDrawRect()=0; XWindowsImp DeviceDrawText() DeviceDrawRect() Window DrawText() DrawRect() DialogBox DrawBorder() FrameWnd … .. Window::DrawRect() m_pWndImpl->DevDrawRect() MACWin->Line() MacWin->Line() MacWin->Line() MacWin->Line() XWin->Rect()
  22. 23. <ul><li>Sorry I don’t have code. Refer GoF’s book for a nice example, </li></ul>
  23. 24. Singleton <ul><li>Only one object instance, provide global access to it. </li></ul><ul><li>Many printers in a system, but only one printer spooler. </li></ul>
  24. 25. Singleton(2) <ul><li>See code Singleton.cpp </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×