Design Pattern Zoology

Post on 26-Jun-2015

1.149 views 0 download

Tags:

Transcript of Design Pattern Zoology

Design Pattern Zoology

Taxonomy and Field Guide of Common Design Patterns

Josh Adell <josh.adell@gmail.com>20120719

@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

Play Along

http://github.com/jadell/dpzoology

Coming Soon...

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

Stages of OOP

Larva

Stages of OOP

Pupa

Stages of OOP

Adult

Design Patterns Are

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

Design Patterns Are Not

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

Anti-patterns

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

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

SOLID Principles

• Single Responsibility

• Open/Closed

• Liskov Substitution

• Interface Segregation

• Dependency Inversion

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

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

Let's get started!

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

Delegate

• Delegator passes a request to a helper (the delegate)• Delegate handles request, passes result back• Delegator passes result back to caller

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

Chain-of-Responsibility

• Dispatcher calls a handler, that calls a handler...• Each handler responsible for passing to next• Stop when event is handled

Observer

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

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

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!

Good References

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

Questions?