mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-02 13:07:27 +02:00
merged
This commit is contained in:
@@ -12,7 +12,7 @@ namespace DesignPatterns;
|
|||||||
* Examples:
|
* Examples:
|
||||||
* - different databases have the same interface to communicate although the underlying systems act differently
|
* - 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
|
interface DatabaseAdapter
|
||||||
{
|
{
|
||||||
|
@@ -10,6 +10,8 @@ namespace DesignPatterns;
|
|||||||
*
|
*
|
||||||
* Examples:
|
* Examples:
|
||||||
* - Database Abstraction Layers
|
* - 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
|
class Facade
|
||||||
|
59
FluentInterface/FluentInterface.php
Normal file
59
FluentInterface/FluentInterface.php
Normal 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 = ?');
|
@@ -46,6 +46,7 @@ class Multiton
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* gets the instance with the given name, e.g. Multiton::INSTANCE_1
|
* gets the instance with the given name, e.g. Multiton::INSTANCE_1
|
||||||
|
* uses lazy initialization
|
||||||
*
|
*
|
||||||
* @param string $instanceName
|
* @param string $instanceName
|
||||||
* @return Multiton
|
* @return Multiton
|
||||||
|
@@ -6,7 +6,7 @@ namespace DesignPatterns;
|
|||||||
* Prototype pattern
|
* Prototype pattern
|
||||||
*
|
*
|
||||||
* Purpose:
|
* 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:
|
* Examples:
|
||||||
* - Large amounts of data (e.g. create 1,000,000 rows in a database at once via a ORM)
|
* - Large amounts of data (e.g. create 1,000,000 rows in a database at once via a ORM)
|
||||||
|
8
README.markdown
Normal file
8
README.markdown
Normal 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!*
|
@@ -6,11 +6,11 @@ namespace DesignPatterns;
|
|||||||
* Singleton pattern
|
* Singleton pattern
|
||||||
*
|
*
|
||||||
* Purpose:
|
* 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:
|
* Examples:
|
||||||
* - DB Connector
|
* - 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 ...)
|
* - Lock file for the application (there is only one in the filesystem ...)
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user