6.0.0-git
2024-03-19
Last Modified 2005-10-10 by Guest

Design Patterns

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()

require_once 'DB.php';

$type = 'mysql';
$options = array(...);
$dbh = &DB::connect($type, $options);

Implementing Abstract Factory

    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;
    } 

Singleton

  • Ensures that there is only ever one instance of a class.
  • Can only be implemented as a suggestion in PHP4

Implementing Singleton

    function &singleton()
    {
        static $registry;

        if (!isset($registry)) {
            $registry = new Registry();
        }

        return $registry;
    }

Implementing Singleton in PHP5

PHP5's improved object model allows a better singleton implementation:

    /**
     * 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;
    }

Observer

  • Also called Subject-Observer, Listener, or Publish-Subscribe
  • Let an arbitrary number of objects react to state changes

Implementing Observer: Subject

    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);
        }
    }

Implementing Observer: Observer

    function update(&$subject)
    {
        // Do what needs to be done.
    }

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 separate 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

Some Resources