Patterns in your Pastry

74
Patterns In Your Pastry Patterns 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 Watts james.watts @ cakephp.org CakeFest 2013: The CakePHP Conference August 31 st , 2013 – San Francisco, USA

description

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

Transcript of Patterns in your Pastry

Page 1: 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

Page 2: Patterns in your Pastry

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

Page 3: Patterns in your Pastry

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

Page 4: Patterns in your Pastry

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

Page 5: Patterns in your Pastry

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

Page 6: Patterns in your Pastry

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

Page 7: Patterns in your Pastry

MVCMVC

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

Page 8: Patterns in your Pastry

- M

- V

- C

Page 9: Patterns in your Pastry

- Model

- V

- C

Page 10: Patterns in your Pastry

- Model

- View

- C

Page 11: Patterns in your Pastry

- Model

- View

- Controller

Page 12: Patterns in your Pastry

- Business

- Presentation

- Mediation

Page 13: Patterns in your Pastry

- Business

- Presentation

- Mediation

Page 14: Patterns in your Pastry

- Business

- Presentation

- Mediation

“ separation of concerns ”

Page 15: Patterns in your Pastry

Neither layer . . .

Page 16: Patterns in your Pastry

Neither layer . . .

- wants to know

Page 17: Patterns in your Pastry

Neither layer . . .

- wants to know

- should know

Page 18: Patterns in your Pastry

Neither layer . . .

- wants to know

- should know

- or can know

Page 19: Patterns in your Pastry

Neither layer . . .

- wants to know

- should know

- or can know

about the other

Page 20: Patterns in your Pastry

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

Page 21: Patterns in your Pastry

Single Point of Entry

Page 22: Patterns in your Pastry

Single Point of EntryObvious Jokes Omitted

Page 23: Patterns in your Pastry
Page 24: Patterns in your Pastry

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

Page 25: Patterns in your Pastry

Override steps

Of a process

Page 26: Patterns in your Pastry

Without Modifying

The Process

Page 27: Patterns in your Pastry

“ Hooks “

Page 28: Patterns in your Pastry
Page 29: Patterns in your Pastry

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

Page 30: Patterns in your Pastry

Create an Object

Without exposing

Instantiation logic

Page 31: Patterns in your Pastry

Animal

Page 32: Patterns in your Pastry

Animal

Cat

Page 33: Patterns in your Pastry

Animal Dog

Cat

Page 34: Patterns in your Pastry

Animal Dog

Cat

Moose

Page 35: Patterns in your Pastry

Animal Factory

Page 36: Patterns in your Pastry

Create ( 'dog' )

Page 37: Patterns in your Pastry
Page 38: Patterns in your Pastry
Page 39: Patterns in your Pastry

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

Page 40: Patterns in your Pastry

Don't Require

Instantiation

Page 41: Patterns in your Pastry

Static

Page 42: Patterns in your Pastry
Page 43: Patterns in your Pastry

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

Page 44: Patterns in your Pastry

ORM

Page 45: Patterns in your Pastry

ORMObject Relational Mapping

Page 46: Patterns in your Pastry

Database Row = Object

Page 47: Patterns in your Pastry

Array

Page 48: Patterns in your Pastry

CRUD

Page 49: Patterns in your Pastry

CRUDCreate, Read, Update, Delete

Page 50: Patterns in your Pastry
Page 51: Patterns in your Pastry

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

Page 52: Patterns in your Pastry

Add Behaviors

To an Object

Page 53: Patterns in your Pastry

Without Modifying

Other Instances

Page 54: Patterns in your Pastry

“ MIXIN “ OR “ TRAIT “

Page 55: Patterns in your Pastry
Page 56: Patterns in your Pastry

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

Page 57: Patterns in your Pastry

There can only be one

Page 58: Patterns in your Pastry

Only Allow a

Single Instance

Page 59: Patterns in your Pastry
Page 60: Patterns in your Pastry
Page 61: Patterns in your Pastry

StrategyStrategy

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

Page 62: Patterns in your Pastry

Change a Process

Based on Criteria

Page 63: Patterns in your Pastry

Common Interface

Variable process

Page 64: Patterns in your Pastry
Page 65: Patterns in your Pastry

AdaptorAdaptor

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

Page 66: Patterns in your Pastry

Adapts an Interface

Page 67: Patterns in your Pastry

Normalization

Page 68: Patterns in your Pastry

“ Wrapper ”

Page 69: Patterns in your Pastry
Page 70: Patterns in your Pastry

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

Page 71: Patterns in your Pastry

Notifications

Page 72: Patterns in your Pastry

State Change

Page 73: Patterns in your Pastry

Event Driven

Page 74: Patterns in your Pastry