Design Pattern Zoology

24
Design Pattern Zoology Taxonomy and Field Guide of Common Design Patterns Josh Adell <[email protected]> 20120719

Transcript of Design Pattern Zoology

Page 1: Design Pattern Zoology

Design Pattern Zoology

Taxonomy and Field Guide of Common Design Patterns

Josh Adell <[email protected]>20120719

Page 2: Design Pattern Zoology

@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

Play Along

http://github.com/jadell/dpzoology

Page 4: Design Pattern Zoology

Coming Soon...

• What are design patterns?• Why design patterns?• Common design patterns with examples

Page 5: Design Pattern Zoology

Stages of OOP

Larva

Page 6: Design Pattern Zoology

Stages of OOP

Pupa

Page 7: Design Pattern Zoology

Stages of OOP

Adult

Page 8: Design Pattern Zoology

Design Patterns Are

• General solutions to common problems• Architectural templates• Named approaches to code structure

Page 9: Design Pattern Zoology

Design Patterns Are Not

• Off-the-shelf solutions• One-size-fits-all panacea

Page 10: Design Pattern Zoology

Anti-patterns

• Poorly or inappropriately used patterns• Increase code complexity and/or fragility

Page 11: Design Pattern Zoology

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

SOLID Principles

• Single Responsibility

• Open/Closed

• Liskov Substitution

• Interface Segregation

• Dependency Inversion

Page 13: Design Pattern Zoology

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

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

Let's get started!

Page 16: Design Pattern Zoology

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

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

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

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

Observer

• Publish-subscribe• Broadcast an event, all observers catch and handle• Broadcaster doesn't care about observers

Page 21: Design Pattern Zoology

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

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

Good References

• http://www.oodesign.com• Gang of Four - Gamma, Helm, Johnson, Vlissides• Head-First Design Patterns

Page 24: Design Pattern Zoology

Questions?