design pattern zoology
TRANSCRIPT
![Page 1: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/1.jpg)
Design Pattern Zoology
Taxonomy and Field Guide of Common Design Patterns
Josh Adell <[email protected]>20120719
![Page 2: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/2.jpg)
@josh_adell
• Software developer: PHP, Javascript, SQL• http://www.dunnwell.com• http://joshadell.com
• http://mojolive.com/profile/joshadell
• http://github.com/jadell/neo4jphp• http://frostymug.herokuapp.com
![Page 3: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/3.jpg)
Play Along
http://github.com/jadell/dpzoology
![Page 4: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/4.jpg)
Coming Soon...
• What are design patterns?• Why design patterns?• Common design patterns with examples
![Page 5: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/5.jpg)
Stages of OOP
Larva
![Page 6: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/6.jpg)
Stages of OOP
Pupa
![Page 7: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/7.jpg)
Stages of OOP
Adult
![Page 8: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/8.jpg)
Design Patterns Are
• General solutions to common problems• Architectural templates• Named approaches to code structure
![Page 9: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/9.jpg)
Design Patterns Are Not
• Off-the-shelf solutions• One-size-fits-all panacea
![Page 10: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/10.jpg)
Anti-patterns
• Poorly or inappropriately used patterns• Increase code complexity and/or fragility
![Page 11: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/11.jpg)
Why use design patterns?
• Proven solutions• Simplify code structure• Promote loose coupling, high cohesion• Enhance testing/testability
Design Patterns are where the power of OOP comes from
![Page 12: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/12.jpg)
SOLID Principles
• Single Responsibility
• Open/Closed
• Liskov Substitution
• Interface Segregation
• Dependency Inversion
![Page 13: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/13.jpg)
Common Themes
• Code to an Interface, not an Implementation• One class == one responsibility• Favor Compostion over Inheritance• Replace branching with polymorphism• Low coupling, high cohesion
![Page 14: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/14.jpg)
Types of Patterns
• Architecturalo Client-Servero Cloud
• Applicationo MVCo Serviceso Message Queue
• UXo Call to Actiono Progressive
disclosure
• Codeo Adaptero Chain-of-Commando Commando Decoratoro Delegateo Factoryo Flyweighto Observero Singletono Strategyo Visitor
![Page 15: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/15.jpg)
Let's get started!
![Page 16: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/16.jpg)
Strategy
• Define a family of behaviors• Each behavior is accessed via a common interface• Client code doesn't care which behavior it uses• Behaviors can be shared across class hierarchies
![Page 17: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/17.jpg)
Delegate
• Delegator passes a request to a helper (the delegate)• Delegate handles request, passes result back• Delegator passes result back to caller
![Page 18: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/18.jpg)
Decorator
• Add behaviors to a base object dynamically• Base decorator passes through unmodified• Decorators that modify requests or responses• Decorators wrap a base object or decorator
![Page 19: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/19.jpg)
Chain-of-Responsibility
• Dispatcher calls a handler, that calls a handler...• Each handler responsible for passing to next• Stop when event is handled
![Page 20: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/20.jpg)
Observer
• Publish-subscribe• Broadcast an event, all observers catch and handle• Broadcaster doesn't care about observers
![Page 21: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/21.jpg)
Patterns derive from each other
• Delegate is based on Strategy• CoR is a string of Delegates that call each other• Decorator & Observer are CoR that always run to the end
![Page 22: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/22.jpg)
Wrap Up
• More patterns out there: Command, Facade, Pool, Mediator, Visitor, Adapter
• Use this vocabulary during technical discussions• Refactor spaghetti and long-winded code• Decrease coupling, increase cohesion!
![Page 23: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/23.jpg)
Good References
• http://www.oodesign.com• Gang of Four - Gamma, Helm, Johnson, Vlissides• Head-First Design Patterns
![Page 24: Design Pattern Zoology](https://reader034.vdocuments.us/reader034/viewer/2022052323/558d45cbd8b42a39318b4710/html5/thumbnails/24.jpg)
Questions?