Patterns in your Pastry

Post on 14-May-2015

1.609 views 0 download

Tags:

description

Talk on design patterns in CakePHP given at CakeFest 2013 in San Francisco, USA.

Transcript of Patterns in your Pastry

Patterns In Your PastryPatterns In Your Pastry

Image credit: Image credit: EvelynGiggles (EvelynIsHere)EvelynGiggles (EvelynIsHere) - http://www.flickr.com/photos/evelynishere/3662124836 - http://www.flickr.com/photos/evelynishere/3662124836

James Wattsjames.watts @ cakephp.org

CakeFest 2013: The CakePHP ConferenceAugust 31st, 2013 – San Francisco, USA

WARNINGWARNING

Image credit: Image credit: Vegan Feast Catering (VeganFeast)Vegan Feast Catering (VeganFeast) - http://www.flickr.com/photos/veganfeast/4139656092 - http://www.flickr.com/photos/veganfeast/4139656092

WARNINGWARNING

You may feel hungryYou may feel hungryat some point duringat some point during

this presentationthis presentationImage credit: Image credit: Vegan Feast Catering (VeganFeast)Vegan Feast Catering (VeganFeast) - http://www.flickr.com/photos/veganfeast/4139656092 - http://www.flickr.com/photos/veganfeast/4139656092

Design PatternsDesign Patterns

Image credit: Image credit: Andrew Michaels (dragontomato)Andrew Michaels (dragontomato) - http://www.flickr.com/photos/dragontomato/3050561189 - http://www.flickr.com/photos/dragontomato/3050561189

There are many!There are many!

Image credit: Image credit: James Lee (jronaldlee)James Lee (jronaldlee) - http://www.flickr.com/photos/jronaldlee/7033839441 - http://www.flickr.com/photos/jronaldlee/7033839441

Image source: Image source: AmazonAmazon - http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612 - http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612

MVCMVC

Image credit: Image credit: Helen (afeitar)Helen (afeitar) - http://www.flickr.com/photos/afeitar/8039909030 - http://www.flickr.com/photos/afeitar/8039909030

- M

- V

- C

- Model

- V

- C

- Model

- View

- C

- Model

- View

- Controller

- Business

- Presentation

- Mediation

- Business

- Presentation

- Mediation

- Business

- Presentation

- Mediation

“ separation of concerns ”

Neither layer . . .

Neither layer . . .

- wants to know

Neither layer . . .

- wants to know

- should know

Neither layer . . .

- wants to know

- should know

- or can know

Neither layer . . .

- wants to know

- should know

- or can know

about the other

Front ControllerFront Controller

Image credit: Image credit: Zrendavir_ (zrendavir)Zrendavir_ (zrendavir) - http://www.flickr.com/photos/zrendavir/3074941476 - http://www.flickr.com/photos/zrendavir/3074941476

Single Point of Entry

Single Point of EntryObvious Jokes Omitted

Template MethodTemplate Method

Image credit: Image credit: Phil and Pam Gradwell (philandpam)Phil and Pam Gradwell (philandpam) - http://www.flickr.com/photos/philandpam/2195674013 - http://www.flickr.com/photos/philandpam/2195674013

Override steps

Of a process

Without Modifying

The Process

“ Hooks “

FactoryFactory

Image credit: Image credit: Philippe Du Berger (DubyDub2009)Philippe Du Berger (DubyDub2009) - http://www.flickr.com/photos/urbexplo/4360787743 - http://www.flickr.com/photos/urbexplo/4360787743

Create an Object

Without exposing

Instantiation logic

Animal

Animal

Cat

Animal Dog

Cat

Animal Dog

Cat

Moose

Animal Factory

Create ( 'dog' )

UtilityUtilityImage credit: Image credit: Kiran Foster (rueful)Kiran Foster (rueful) - http://www.flickr.com/photos/rueful/5697846897 - http://www.flickr.com/photos/rueful/5697846897

Don't Require

Instantiation

Static

Active RecordActive Record

Image credit: Image credit: Marcin Wichary (mwichary)Marcin Wichary (mwichary) - http://www.flickr.com/photos/mwichary/2481681915 - http://www.flickr.com/photos/mwichary/2481681915

ORM

ORMObject Relational Mapping

Database Row = Object

Array

CRUD

CRUDCreate, Read, Update, Delete

DecoratorDecorator

Image credit: Image credit: U.S. Army Corps of Engineers (usacehq)U.S. Army Corps of Engineers (usacehq) - http://www.flickr.com/photos/usacehq/8723898265 - http://www.flickr.com/photos/usacehq/8723898265

Add Behaviors

To an Object

Without Modifying

Other Instances

“ MIXIN “ OR “ TRAIT “

SingletonSingleton

Image credit: Image credit: Kevin Jones (kj-an)Kevin Jones (kj-an) - http://www.flickr.com/photos/kj-an/2294604279 - http://www.flickr.com/photos/kj-an/2294604279

There can only be one

Only Allow a

Single Instance

StrategyStrategy

Image credit: Image credit: Lucas Arts/Walt DisneyLucas Arts/Walt Disney

Change a Process

Based on Criteria

Common Interface

Variable process

AdaptorAdaptor

Image credit: Image credit: gwire (gwire)gwire (gwire) - http://www.flickr.com/photos/gwire/336458831 - http://www.flickr.com/photos/gwire/336458831

Adapts an Interface

Normalization

“ Wrapper ”

ObserverObserver

Image credit: Image credit: nociveglia (gwire)nociveglia (gwire) - http://www.flickr.com/photos/40385177@N07/4256525264 - http://www.flickr.com/photos/40385177@N07/4256525264

Notifications

State Change

Event Driven