Top Banner
Sheraton Gateway Suite O'Hare May 20, 2008
83
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008

Page 2: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008

Design Patterns in PHP

Page 3: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008

Jason E. Sweathttp://[email protected]

Design Patterns in PHP

Page 4: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 2Sheraton Gateway Suite O'HareMay 20, 2008

Technical Conference Presentation Pattern

2

• Design Pattern–The Classic Tech Talk

• Implementation:–Tell ‘em what your are going to tell ‘em–Tell ‘em–Tell ‘em what you told ‘em

• Forces:–Communication information to an audience

Page 5: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 3

The “Pattern” of this presentation

• Definition of Design Patterns

• Catalog of Design Patterns–Name and define each pattern–Review example PHP code–Forces driving use

• Conclusion

Page 6: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 4

What is a Design Pattern?

• Commonly recognized, well developed and tested solution to a specific problem in a given context

• A specific way to organize a portion of your program—a code structure

• A name – a short hand way to communicate a design decision with other programmers familiar with patterns

• Obligatory reference: Christopher Alexander “A Pattern Language”

Page 7: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 5

What a Design Pattern Isn’t

• Library• Framework• Copy & Paste Example Code

Page 8: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 5

What a Design Pattern Isn’t

• Library• Framework• Copy & Paste Example Code

Your code and your problems are required to realize a design pattern.

Page 9: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 6

Reification

• Reify - To make real• Reification – The process of making real

–Definition from “Holub on Patterns”

Page 10: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 7

Pattern Usage

• Write code as usual• Feel a pain point (complex design issues,

bad code smells)• Be aware of Design Patterns, specifically

ones which solve the problem you are experiencing

• Refactor/write your code to apply the pattern to your problem

• Help apply DRY principal

Page 11: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 8

Pattern Anti-Usage

• Big up front design–Plan which patterns you will need ahead of time–Violation of YAGNI

• Apply patterns where the problem they solve does not exist–Create complexity where there is no need for it–This applies to nearly every isolated example!!

Page 12: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 8

Pattern Anti-Usage

• Big up front design–Plan which patterns you will need ahead of time–Violation of YAGNI

• Apply patterns where the problem they solve does not exist–Create complexity where there is no need for it–This applies to nearly every isolated example!!

Design Patterns can make coding complex problems easier.

Use them wisely and appropriately.

Page 14: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 10

Catalog Approach

• Name• Problem – what problem and context is the

pattern a solution to• Example – some concrete web application

related problems• Code – in PHP• Forces

–Why you would want to use this pattern–What would cause you to avoid it

Many pattern catalogs are much more detailed. We are going to limit the scope of the catalog to these essential elements.

Page 15: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 11

Singleton

• Problem: How can you make sure you are using a single instance of a class, and provide easy access to this instance?

• Example: database connections, factories, registries

Page 16: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 12

Singleton

• Code

• Tests

Page 17: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 13

Singleton - Forces

• For–Ensure object is configured correctly before

access–Ensure there is only one instance of the object

possible

• Against–Global access–Hard coding dependencies

Page 18: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 14

Factory

• Problem: How can you centralize logic related to creation of objects?

• Example: Create instances of a CatalogItems

Page 19: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 14

Factory

• Problem: How can you centralize logic related to creation of objects?

• Example: Create instances of a CatalogItems

There are several related creational patterns: FactoryMethod, AbstractFactory, Builder. In PHP, any method which returns an object is often referred to as a Factory. The dynamic typing of PHP may make implementing these in strict accordance with the patterns excessive for most projects needs.

Page 20: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 15

FactoryMethod

• Class Diagram

Page 21: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 16

FactoryMethod

• code

Page 22: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 17

FactoryMethod

• More “PHPish” code

Page 23: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 18

FactoryMethod - Code in action

Page 24: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 19

FactoryMethod - Forces

• For–Isolate construction complexity–Promote decoupling code

• Against–One additional layer of indirection between the

construction of an object and it’s use

Page 25: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 20

TemplateMethod

• Problem: How can you structure a class hierarchy to allow new implementations via subclassing?

• Example: different shipping calculation models in a shopping cart

Page 26: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 21

TemplateMethod

• code

Page 27: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 22

TemplateMethod

• Code continued

Page 28: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 23

TemplateMethod

• Tests

Page 29: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 24

TemplateMethod - Forces

• For–Lets subclasses vary algorithm behavior–Reduces code duplication

• Against–Forces inheritance

Page 30: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 25

Strategy

• Problem: How can you encapsulate an algorithm in an object, so that it can be selected at run-time?

• Example: different shipping calculation models in a shopping cart

Page 31: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 25

Strategy

• Problem: How can you encapsulate an algorithm in an object, so that it can be selected at run-time?

• Example: different shipping calculation models in a shopping cart

Quote seen on the internet: “Eventually, everything starts to look like a Strategy.” This is because the Strategy pattern implementation emphasizes the OO principal of polymorphism, making it very powerful in practice.

Page 32: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 26

Strategy

• code

Page 33: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 27

Strategy

• More code

Page 34: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 28

Strategy

• Tests

Page 35: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 29

Strategy - Forces

• For–Heart of favor composition over inheritance–Elimination of replicated control logic–Select “guts” of an object at run-time

• Against–Code complexity

Page 36: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 30

State

• Problem: How can you allow an object to alter its behavior when its internal state changes, such that the object will appear to change its class?–Put another way: How can I implement an object

which behaves like the Strategy pattern, but can change Strategies over the lifetime of the object

• Example: same shopping cart checkout–Perhaps you want to list the shipping rates for all

available options reusing the strategy code already implemented in the prior example

Page 37: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 31

State - Code

Page 38: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 32

State

• Tests

Page 39: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 33

State – Forces

• For–Power of Strategy with more flexibility

• Against–Similar code complexity as Strategy

Page 40: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 34

Proxy

• Problem: How can you provide surrogate control to an object?

• Example: remote resources, lazy instantiation, access control

Page 41: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 35

Proxy (Access)

• Code

Page 42: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 36

Proxy (Access)

• Tests

Page 43: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 37

Proxy (Lazy)

• Code

Page 44: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 38

Proxy – Forces

• For–Need to add additional functionality without

changing the original classes responsibilities–Need flexibility to change functionality after the

object is already instantiated

• Against–Is the flexibility required?

Page 45: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 39

Decorator

• Problem: How can you add capabilities to an object dynamically?

• Example: caching, error/warning indication

The Decorator pattern can allow you to “trump” inheritance, allowing you to deploy a very flexible solution without repeating code in many locations inside of a class hierarchy.

Page 46: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 40

Decorator

• Illustrating the problem in UML– A hypothetical widget library to generate forms

– But we might want to have some widgets labeled…

Page 47: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 41

Decorator

But sometimes you want to flag the control as in error after form validation, and this might apply to either the normal or a labeled widget…

Page 48: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 42

Decorator

Yuck… and we only showed two kinds of “Widget”

Page 49: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 43

Decorator

Page 50: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 44

Decorator

• Code

Page 51: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 45

Decorator

Page 52: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 46

Decorator - Forces

• For–Eliminate code replication in class hierarchy–Flexibility–Powerful use of Polymorphism

• Against–Additional code to create the object–May be hard to understand the effects of objects

decorated multiple times

Page 53: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 47

SPL

• Standard PHP Library• Documentation

–Official - http://www.php.net/manual/en/ref.spl.php–Real - http://www.php.net/~helly/php/ext/spl/

• SPL is a collection of interfaces and classes that are meant to solve standard problems.–Many SPL classes implement design patterns

Page 54: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 48

Iterator

• Problem: How can you process a collection of objects in a uniform manner?

• Examples: arrays, directories/files, db record sets, XML elements

Page 55: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 49

Iterator

• foreach is your friend• Hooray for object handles!

Page 56: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 50

Iterator

• If you are too lazy to look up the methods in the Iterator interface, PHP will let you know:

Page 57: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 51

Iterator - Code

Page 58: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 52

Iterator

• Tests

Page 59: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 53

Iterator - Forces

• For–Uniform handling of different data sources

• Against–Can your code compete with an array?

Page 60: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 54

Observer

• Problem: How can you effectively separate objects which produce data from objects which need to consume this data?

• Example: logging to one or more styles of output (text file, syslog, email, HTML output, etc.)

Page 61: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 55

Observer

Page 62: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 56

Observer

• In SPL – interfaces

Page 63: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 57

Observer

• Code – • A Subject

Page 64: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 58

Observer

• Code – putting it to use:

Page 65: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 59

Observer

• Tests

Page 66: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 60

Observer - Forces

• For–Effective separation of the producer of data from

the consumers

• Against–The common theme – code complexity, did you

really need the flexibility Observer provides?

Page 67: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 61

A pair of Data Access Patterns

• In PHP, nearly every application accesses data in a database – what patterns have evolved in this area?–ActiveRecord–TableDataGateway

CREATE TABLE `bookmark` ( `id` INT NOT NULL AUTO_INCREMENT , `url` VARCHAR( 255 ) NOT NULL , `name` VARCHAR( 255 ) NOT NULL , `description` MEDIUMTEXT, `tag` VARCHAR( 50 ) , `created` DATETIME NOT NULL , `updated` DATETIME NOT NULL ,PRIMARY KEY ( `id` ))

Page 68: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 62

A database access library

• http://adodb.sf.net/ require_once 'adodb/adodb.inc.php';

class DB { //static class, we do not need a constructor private function __construct() {} public static function conn() { static $conn; if (!$conn) { $conn = adoNewConnection('mysql'); $conn->connect('localhost', 'phpauser‘ , 'phpapass', 'phpa'); $conn->setFetchMode(ADODB_FETCH_ASSOC); } return $conn; }}

Page 69: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 63

ActiveRecord

• How can we have and object which both knows business logic and how to persist itself in a database?

Page 70: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 64

ActiveRecord - Code

• Some properties

• And a little business logic – id is read only

class Bookmark { const NEW_BOOKMARK = -1; protected $id = -1; protected $conn; public $url; public $name; public $description; public $tag; public $created; public $updated; public function getId() { return $this->id; }}

Page 71: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 65

ActiveRecord – Code – constructor, find self?

class Bookmark { const SELECT_BY_ID = 'select * from bookmark where id = ?'; public function __construct($id=false, $conn=false) { $this->conn = ($conn) ? $conn : DB::conn(); if ($id) { $rs = $this->conn->execute( self::SELECT_BY_ID ,array((int)$id));

if ($rs) { $row = $rs->fetchRow(); foreach($row as $field => $value) { $this->$field = $value; } } else { throw new Exception('DB Error: '.$this->conn->errorMsg()); } } }}

Page 72: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 66

ActiveRecord – code

• Great, now we can find ourself, how can we make changes?

public function save() { if ($this->id == self::NEW_BOOKMARK) { $this->insert(); } else { $this->update(); } $this->setTimeStamps(); }

Page 73: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 67

ActiveRecord – update()

const UPDATE_SQL = " update bookmark set url = ?, name = ?, description = ?, tag = ?, updated = now() where id = ? "; protected function update() { $this->conn->execute( self::UPDATE_SQL ,array( $this->url, $this->name, $this->description, $this->tag, $this->id)); }

Page 74: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 68

ActiveRecord - Forces

• For–Simplest data pattern for “row at a time” handling

of data

• Against–Mixing data access and business logic in a single

class – low cohesion

Page 75: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 69

TableDataGateway

• How can we give ourselves access to many rows of data from a table easily?

Page 76: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 70

TableDataGateway - code

class BookmarkGateway { protected $conn; public function __construct($conn) { $this->conn = $conn; }

public function findAll() { $rs = $this->conn->execute('select * from bookmark'); if ($rs) { return $rs->getArray(); } else { throw new Exception('DB Error: ' .$this->conn->errorMsg()); } }}

Page 77: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 71

TableDataGateway – add()

const INSERT_SQL = " insert into bookmark (url, name, description, tag , created, updated) values (?, ?, ?, ?, now(), now()) "; public function add($url, $name, $description, $group) { $rs = $this->conn->execute( self::INSERT_SQL ,array($url, $name, $description, $group)); if (!$rs) { throw new Exception('DB Error: '.$this->conn->errorMsg()); } }

Page 78: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 72

TableDataGateway – Forces

• For–Simplest data pattern for multiple row access of

data from a table–Centralize database access for table in a single

location in your code

• Against–Managing code in separate locations – sql

statement is now located in a find method instead of embedded in code

Page 79: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 73

Reading Resources

Page 80: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 73

Reading Resources

• GoF - Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides Design Patterns: Elements of reusable object-oriented software. Addison-Wesley, 1995

Page 81: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 73

Reading Resources

• GoF - Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides Design Patterns: Elements of reusable object-oriented software. Addison-Wesley, 1995

• PoEAA - Martin Fowler Patterns of Enterprise Application Architecture Addison-Wesley, 2003

Page 82: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 73

Reading Resources

• GoF - Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides Design Patterns: Elements of reusable object-oriented software. Addison-Wesley, 1995

• PoEAA - Martin Fowler Patterns of Enterprise Application Architecture Addison-Wesley, 2003

• Allen Holub Holub on Patterns: Learning design patterns by looking at code Apress, 2004

• Robert Martin Agile Software Development Prentice Hall, 2003• Clifton Nock Data Access Patterns Addison-Wesley, 2004• George Schlossnagle Advanced PHP Programming Sams Pub., 2004• Alan Shalloway and James R. Trott Design Patterns Explained, Addison-

Wesley, 2005• Rebecca Wirfs-Brock and Alan McKean Roles, Responsibilities and

Collaborations Addison-Wesley, 2003• Matt Zandstra Php 5 Objects, Patterns, Practice Apress, 2004

Page 83: jason sweat-design patterns

Sheraton Gateway Suite O'HareMay 20, 2008 73

Reading Resources

• GoF - Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides Design Patterns: Elements of reusable object-oriented software. Addison-Wesley, 1995

• PoEAA - Martin Fowler Patterns of Enterprise Application Architecture Addison-Wesley, 2003

• Allen Holub Holub on Patterns: Learning design patterns by looking at code Apress, 2004

• Robert Martin Agile Software Development Prentice Hall, 2003• Clifton Nock Data Access Patterns Addison-Wesley, 2004• George Schlossnagle Advanced PHP Programming Sams Pub., 2004• Alan Shalloway and James R. Trott Design Patterns Explained, Addison-

Wesley, 2005• Rebecca Wirfs-Brock and Alan McKean Roles, Responsibilities and

Collaborations Addison-Wesley, 2003• Matt Zandstra Php 5 Objects, Patterns, Practice Apress, 2004

• Jason E. Sweat Php Architect's Guide to Php Design Patterns Marco Tabini & Associates, 2005