+ Design Patterns [[toc]] TODO: add more from the kongress2002-design_patterns presentation ++ What are design patterns? > "Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice." > > ? Christopher Alexander, talking about buildings and towns * Many common programming problems * They've been solved over and over again * Recognize which problems are common * Name them to create a common vocabulary * Patterns are blueprints * They are meant to be modified * Not all suspension bridges are the same ++ What do patterns not do? * Patterns are not rules * Patterns are not implementations! ++ When to use them * When the Problem they solve is a problem you have ... * ... and when the Consequences are acceptable * Solve problems you have, not problems you think you might have ++ Common Patterns * Abstract Factory * Singleton * Observer * Model/View/Controller +++ Abstract Factory * Provides an interface for creating one of a family of objects without specifying that object's concrete class name. * Example: DB::connect() <code type="php"> require_once 'DB.php'; $type = 'mysql'; $options = array(...); $dbh = &DB::connect($type, $options); </code> ++++ Implementing Abstract Factory <code type="php"> function &factory($type) { @include_once("DB/${type}.php"); $classname = "DB_${type}"; if (!class_exists($classname)) { return PEAR::raiseError(null, DB_ERROR_NOT_FOUND, null, null, null, 'DB_Error', true); } return $obj =& new $classname; } </code> +++ Singleton * Ensures that there is only ever one instance of a class. * Can only be implemented as a suggestion in PHP4 ++++ Implementing Singleton <code type="php"> function &singleton() { static $registry; if (!isset($registry)) { $registry = new Registry(); } return $registry; } </code> ++++ Implementing Singleton in PHP5 PHP5's improved object model allows a better singleton implementation: <code type="php"> /** * The one instance. */ static private $instance = false; /** * Make the constructor private. */ private function __construct() {} /** * Use this static method to get at the one instance. */ static public function singleton() { if (!self::$instance) { self::$instance = new Singleton(); } return self::$instance; } </code> +++ Observer * Also called Subject-Observer, Listener, or Publish-Subscribe * Let an arbitrary number of objects react to state changes ++++ Implementing Observer: Subject <code type="php"> function attach(&$observer) { $this->_observers[$observer->getId()] =& $observer; } function detach(&$observer) { unset($this->_observers[$observer->getId()]); } function notify() { foreach ($this->_observers as &$observer) { $observer->update($this); } } </code> ++++ Implementing Observer: Observer <code type="php"> function update(&$subject) { // Do what needs to be done. } </code> +++ Model/View/Controller * MVC is a specialized Observer pattern, with Composite and Strategy completing the picture * Observer describes how Views and Models interact * Composite lets Views be made up of other Views * Strategy lets Views use different Controllers ++++ MVC: Model/View * Think of structured HTML as a Model, and CSS as a View * Goal is to completely seperate data and logic from presentation * Allows redesigns or new views (different stylesheet) without changing the Model ++++ MVC: View Compositing * When Smalltalk-80 introduced MVC it had a !CompositeView * The Composite pattern solves the problem of wanting to treat groups of objects like individual objects ++++ MVC: View/Controller * The Controller decides how a View handles user input * Think of a command line interface, web interface, and web services as potential Controllers * Controllers take user input and give them to both Views and Models as appropriate ++ PHP Implementations * http://www.google.com/search?q=php+mvc ... numerous * [http://phrame.sourceforge.net/ Phrame] * [http://www.tonybibbs.com/index.php?topic=MVCnPHP MVCnPHP] ++ Some Resources * The [http://c2.com/ppr/ Portland Pattern Repository] (the original wiki) * [http://www.phparchitect.com/ php|architect] * [http://www.phppatterns.com/ phpPatterns.com] (not updated recently)