This commit is contained in:
Dominik Liebler
2011-09-03 12:24:38 +02:00
7 changed files with 74 additions and 4 deletions

View File

@ -12,7 +12,7 @@ namespace DesignPatterns;
* Examples:
* - different databases have the same interface to communicate although the underlying systems act differently
*
* this is a VERY basic example which won't work at all
* this is a VERY basic example which won't work at all!
*/
interface DatabaseAdapter
{

View File

@ -10,6 +10,8 @@ namespace DesignPatterns;
*
* Examples:
* - Database Abstraction Layers
* - Doctrine2: EntityManager is the facade that one sees from the outside, but in there is much more going on, Unit of
* Work, etc.
*
*/
class Facade

View File

@ -0,0 +1,59 @@
<?php
namespace DesignPatterns;
/**
* fluent interface pattern
*
* Purpose:
* to write code that is easy readable just like "real" sentences
*
* Examples:
* - Doctrine2's QueryBuilder works something like that example class below
* - PHPUnit uses fluent interfaces to build mock objects
*
*/
class SQL
{
protected $_fields = array();
protected $_from = array();
protected $_where = array();
/**
*
* @param array $fields
* @return SQL
*/
public function select(array $fields = array())
{
$this->_fields = $fields;
return $this;
}
/**
*
* @param string $table
* @param string $alias
* @return SQL
*/
public function from($table, $alias)
{
$this->_from[] = $table . ' AS ' . $alias;
return $this;
}
/**
* @param string $condition
* @return SQL
*/
public function where($condition)
{
$this->_where[] = $condition;
return $this;
}
}
$instance = new SQL();
$instance->select(array('foo', 'bar'))
->from('foobar', 'f')
->where('f.bar = ?');

View File

@ -46,6 +46,7 @@ class Multiton
/**
* gets the instance with the given name, e.g. Multiton::INSTANCE_1
* uses lazy initialization
*
* @param string $instanceName
* @return Multiton

View File

@ -6,7 +6,7 @@ namespace DesignPatterns;
* Prototype pattern
*
* Purpose:
* to avoid the the cost of creating objects the standard way (new Foo()) and instead create a prototype and clone it
* to avoid the cost of creating objects the standard way (new Foo()) and instead create a prototype and clone it
*
* Examples:
* - Large amounts of data (e.g. create 1,000,000 rows in a database at once via a ORM)

8
README.markdown Normal file
View File

@ -0,0 +1,8 @@
# design patterns in PHP #
This is a collection of known design patterns and some sample code how to implement them in PHP. Every pattern has a
small list of examples (most of them from Zend Framework or Doctrine2 as I'm most familiar with this software).
I think the problem with patterns is that often people do know them but don't know when to apply which.
*Please feel free to fork and add your own examples!*

View File

@ -6,11 +6,11 @@ namespace DesignPatterns;
* Singleton pattern
*
* Purpose:
* - to have only one instance of this object in the application, that handles all calls
* to have only one instance of this object in the application, that will handle all calls
*
* Examples:
* - DB Connector
* - Logger (May also be a Multiton if there are many Logfiles for several purposes)
* - Logger (may also be a Multiton if there are many log files for several purposes)
* - Lock file for the application (there is only one in the filesystem ...)
*
*/