design patterns - inspirations from nature

39
Design Patterns inspirations from nature Prakash Sathiyamoorthy

Upload: prakash-sathiyamoorthy

Post on 17-Nov-2014

1.772 views

Category:

Education


0 download

DESCRIPTION

This presentation uses examples from day-to-day life to explain 22 of the 24 design patterns mentioned in GoF patterns.

TRANSCRIPT

  • 1. Prakash Sathiyamoorthy

2. Presentation Overview Introduction to Patterns Patterns are not only for C.Sc. Earliest Patterns. Patterns in C.Sc. To real life. 3. Patterns - What are they? Natural human thought process Helps to solve problems effectively Easy to recognize Has been in use for thousands of years We come across them everyday 4. Patterns - Not a C.Sc Subject Computer Science has adopted a version of DesignPatterns. Design patterns are for everyone. 5. Earliest Patterns - 1 Astronomical constellations 6. Earliest Patterns - 2 Rangolis 7. If only C.Sc used Patterns Shortest pathconnecting the dots ! 8. Earliest Patterns - 3 Periodic table of elements 9. Patterns in Nature - OOPS OOPS Separation of responsibilities. Nothing much 10. Example - OOPSImagine if you are a single person : building your own house, manufacturing your own car, cooking your own food, farming your own crops, stitching your own dresses, doing laundry, running your own transport, making your own televisions, running your own channels, etc. etc. etc. 11. OOPS !!! (literally) missedmaking your own software ! 12. Patterns in Nature - Singleton Problem: Controlling traffic in a road junction. Imagine having a police for every direction. Solution: Place one at the center. Example: Traffic police in junctions.Pic follows 13. Patterns in Nature - Factory Problem: Imagine visiting every brands showroom separately i.e. Reebok, Peter England, etc. Solution: Keep every brand under one roof so it is easy to find everything in the same place. Example: Brand factory 14. Patterns in Nature - Builder Problem: A guy thats wants to own a house most of the times cannot build himself/herself. Solution: Handover the responsibility of building the house to a builder who specializes in that. Example: Construction builder 15. Patterns in Nature - Prototype Problem: Imagine writing lecture notes laboriously during the lecture in college. Solution (Easy) : Take photocopies from the front row guys Example: Xerox copies 16. Patterns in Nature - Pool Problem: Everyone wants to pick up the same book thesubject lecturer announces. You cannot have infinite copiesof a title. Even if you do, not all would be required. Solution: Have finite set of copies and keep circulatingthat. Examples: Lending books from library Cabs servicing customers 17. Patterns in Nature - Flyweight Problem: Making ice for every color is not only costlybut also unnecessary. Solution: Have ice separately so it is easy to mix theessence which differs among orders. Example: ice gola (Pic follows) 18. Patterns in Nature - Composite Some patterns are hard to explain and this is one of them, so lets skip 19. Patterns in Nature - Adapter Problem: You reach US and try to connect your Laptop you purchased in India and end up in trouble. Solution: You get a India-US plug adapter that can connect your plug to the wall socket. 20. Patterns in Nature - Decorator Problem: Well, there are no problems here actually. Solution: A solution is not required when there is no problem So a decorator just adds up to the beauty of the original. May be having undecorated could be scary Example: suspense here, next slide 21. And he is theDecorator 22. Patterns in Nature - Proxy Problem: Original may be presented later untilabsolutely required. Solution: Place a substitute for time being. Example: Proxy attendance, reserving bus seat with kerchief (could be any piece of cloth actually )Bus seat reservation with kerchief 23. Patterns in Nature - Bridge Remember, some patterns cannot be explained so easily? This is the second one, so well skip. 24. Patterns in Nature - Facade Problem: Hide the complexity and expose a much simpler and a easy to understand look. Solution: Cover the complex portion with a thing that looks simpler and beautiful. Example: A modern buildings faadePic follows 25. Patterns in Nature - COR Problem: One person/entity is burdened with all theresponsibilities. Solution: Delegate the individual responsibilities so thatone person/entity is not burdened with all the work. Examples: A typical hotel scenario biller -> Server -> Cook A government office extended COR 26. Patterns in Nature - Template Problem: Making commonly used patterns is time consuming Solution: Avoid commonly used but time consuming work byhaving a ready made shape that can be used as a reference tomake the intended one easily Examples: Stencils in school Painting templates Do you realize how early you started applying design patterns? 27. Patterns in Nature - Interpreter Problem: Need a dedicated way of handling a small domain related work. Solution: Define a domain specific language, the grammar, rules for interpretation, etc. Example: Language used in avatar (Navi) : Define a domains own language and the interpretation rules. 28. Patterns in Nature - Command Problem: Need a command in between that will shorten things up. Imagine every customer of hotel talking to the cook directly ! Solution: Have a person/thing in between who can take the command and delegate the request and handle it back. Example: A server in hotel One who takes the request is not the one that does the actual work. 29. Patterns in Nature - Iterator Problem: Need a easy way to visit every member of a group. Solution: Have a representation of the collection so it is easy to callout each person. Example: An attendance register. 30. Patterns in Nature - Mediator Problem: Need to mediate things so that the individual entities can have easy and effective communication. Solution: Place a mediator who will mediate between the things. In our example, quikrr or ebay mediates between buyers and sellers. Example: Quikrrrrrrrrrrr, ebay 31. Patterns in Nature - Memento Problem: Need to keep a memory for every occasion so it is easy to recollect later. Solution: Have a memento for every occasion. Example: from Ramayana, next slide. 32. Aaranya Kand1. Sita asks Rama to fetch the deer for her2. Maricha (the daemon) shouts in the voice ofRama asking for Lakshmana for help. 33. 3. Lakshman draws the restricted boundary linefor Sita.4.Ravana makes Sita to cross the line so that he canabduct her.5. Ravana succeeds in abducting Sita 34. Sita is in deep troubleNow comes the Memento design pattern to Sitasrescue.6. Sita keeps throwing herornaments (mementos) forRama to trace the route to findher !No computer program would have applied this pattern before Sita did 35. ConclusionSo, Design Patterns are definitely not invented forComputer Science !We see, hear, experience and come across many patternsin our daily life.Gang-Of-Four patterns are not the complete list ofpatterns. 36. Thank You [email protected]