added facade composite and adapter

This commit is contained in:
Dominik Liebler 2011-08-21 19:44:19 +02:00
parent 72e6ff60f4
commit 1e15c7b6a4
3 changed files with 152 additions and 0 deletions

37
Adapter/Adapter.php Normal file
View File

@ -0,0 +1,37 @@
<?php
namespace DesignPatterns;
/**
* adapter pattern
*
* Purpose:
* to link two systems, that have different interfaces. An adapter defines interfaces that are equal for all linked
* systems and wrap functionality
*
* 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
*/
interface DatabaseAdapter
{
public function getTables();
}
class MySQL implements DatabaseAdapter
{
public function getTables()
{
return $this->select('SHOW TABLES');
}
}
class SQLite implements DatabaseAdapter
{
public function getTables()
{
return system('sqlite --list-tables');
}
}

68
Composite/Composite.php Normal file
View File

@ -0,0 +1,68 @@
<?php
namespace DesignPatterns;
/**
* composite pattern
*
* Purpose:
* to treat a group of objects the same way as a single instance of the object
*
* Example:
* - a form class instance handles all its form elements like a single instance of the form, when render() is called, it
* subsequently runs trough all its child elements and calls render() on them
*
*/
class Form
{
protected $_elements;
/**
* runs through all elements and calls render() on them, then returns the complete representation
* of the form
*
* from the outside, one will not see this and the form will act like a single object instance
*
* @return string
*/
public function render()
{
$formCode = '';
foreach ($this->_elements as $element) {
$formCode .= $element->render();
}
return $formCode;
}
public function addElement(FormElement $element)
{
$this->_elements[] = $element;
}
}
abstract class FormElement
{
abstract public function render();
}
class TextElement extends FormElement
{
public function render()
{
return 'this is a text element';
}
}
class InputElement extends FormElement
{
public function render()
{
return '<input type="text" />';
}
}
$form = new Form();
$form->addElement(new TextElement());
$form->addElement(new InputElement());
echo $form->render();

47
Facade/Facade.php Normal file
View File

@ -0,0 +1,47 @@
<?php
namespace DesignPatterns;
/**
* facade pattern
*
* Purpose:
* like a real facade, to hide complexity behind the wall
*
* Examples:
* - Database Abstraction Layers
*
*/
class Facade
{
private $_text;
private $_queryBuilder;
public function __construct($text)
{
$text .= ', called ' . __METHOD__ . ' and thought that would be fairly easy, but ...';
$this->_text = $text;
$this->_initQueryBuilder($text);
}
protected function _initQueryBuilder($sql)
{
$query = new QueryBuilder();
$query->setSql($sql);
$this->_queryBuilder = $query;
}
}
class QueryBuilder
{
protected $_sql;
public function setSql($sql)
{
$this->_sql = $sql;
}
}
// this is just a simple call, but behind the facade, there's much more going on
$foo = new Facade('very simple');