Builder(构造者)
构造者模式主要在于创建一些复杂的对象:
<?php
class Product {
private $name;
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
abstract class Builder {
protected $product;
final public function getProduct() {
return $this->product;
}
public function buildProduct() {
$this->product = new Product();
}
}
class FirstBuilder extends Builder {
public function buildProduct() {
parent::buildProduct();
$this->product->setName('The product of the first builder');
}
}
class SecondBuilder extends Builder {
public function buildProduct() {
parent::buildProduct();
$this->product->setName('The product of second builder');
}
}
class Factory {
private $builder;
public function __construct(Builder $builder) {
$this->builder = $builder;
$this->builder->buildProduct();
}
public function getProduct() {
return $this->builder->getProduct();
}
}
$firstDirector = new Factory(new FirstBuilder());
$secondDirector = new Factory(new SecondBuilder());
print_r($firstDirector->getProduct()->getName());
// The product of the first builder
print_r($secondDirector->getProduct()->getName());
// The product of second builder
<?php
class Product {
private $name;
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
abstract class Builder {
protected $product;
final public function getProduct() {
return $this->product;
}
public function buildProduct() {
$this->product = new Product();
}
}
class FirstBuilder extends Builder {
public function buildProduct() {
parent::buildProduct();
$this->product->setName('The product of the first builder');
}
}
class SecondBuilder extends Builder {
public function buildProduct() {
parent::buildProduct();
$this->product->setName('The product of second builder');
}
}
class Factory {
private $builder;
public function __construct(Builder $builder) {
$this->builder = $builder;
$this->builder->buildProduct();
}
public function getProduct() {
return $this->builder->getProduct();
}
}
$firstDirector = new Factory(new FirstBuilder());
$secondDirector = new Factory(new SecondBuilder());
print_r($firstDirector->getProduct()->getName());
// The product of the first builder
print_r($secondDirector->getProduct()->getName());
// The product of second builder
Structural Patterns
Decorator(装饰器模式)
装饰器模式允许我们根据运行时不同的情景动态地为某个对象调用前后添加不同的行为动作。
<?php
class HtmlTemplate {
// any parent class methods
}
class Template1 extends HtmlTemplate {
protected $_html;
public function __construct() {
$this->_html = "<p>__text__</p>";
}
public function set($html) {
$this->_html = $html;
}
public function render() {
echo $this->_html;
}
}
class Template2 extends HtmlTemplate {
protected $_element;
public function __construct($s) {
$this->_element = $s;
$this->set("<h2>" . $this->_html . "</h2>");
}
public function __call($name, $args) {
$this->_element->$name($args[0]);
}
}
class Template3 extends HtmlTemplate {
protected $_element;
public function __construct($s) {
$this->_element = $s;
$this->set("<u>" . $this->_html . "</u>");
}
public function __call($name, $args) {
$this->_element->$name($args[0]);
}
}
<?php
class HtmlTemplate {
// any parent class methods
}
class Template1 extends HtmlTemplate {
protected $_html;
public function __construct() {
$this->_html = "<p>__text__</p>";
}
public function set($html) {
$this->_html = $html;
}
public function render() {
echo $this->_html;
}
}
class Template2 extends HtmlTemplate {
protected $_element;
public function __construct($s) {
$this->_element = $s;
$this->set("<h2>" . $this->_html . "</h2>");
}
public function __call($name, $args) {
$this->_element->$name($args[0]);
}
}
class Template3 extends HtmlTemplate {
protected $_element;
public function __construct($s) {
$this->_element = $s;
$this->set("<u>" . $this->_html . "</u>");
}
public function __call($name, $args) {
$this->_element->$name($args[0]);
}
}
Adapter(适配器模式)
这种模式允许使用不同的接口重构某个类,可以允许使用不同的调用方式进行调用:
<?php
class SimpleBook {
private $author;
private $title;
function __construct($author_in, $title_in) {
$this->author = $author_in;
$this->title = $title_in;
}
function getAuthor() {
return $this->author;
}
function getTitle() {
return $this->title;
}
}
class BookAdapter {
private $book;
function __construct(SimpleBook $book_in) {
$this->book = $book_in;
}
function getAuthorAndTitle() {
return $this->book->getTitle().' by '.$this->book->getAuthor();
}
}
// Usage
$book = new SimpleBook("Gamma, Helm, Johnson, and Vlissides", "Design Patterns");
$bookAdapter = new BookAdapter($book);
echo 'Author and Title: '.$bookAdapter->getAuthorAndTitle();
function echo $line_in) {
echo $line_in."<br/>";
}
<?php
class SimpleBook {
private $author;
private $title;
function __construct($author_in, $title_in) {
$this->author = $author_in;
$this->title = $title_in;
}
function getAuthor() {
return $this->author;
}
function getTitle() {
return $this->title;
}
}
class BookAdapter {
private $book;
function __construct(SimpleBook $book_in) {
$this->book = $book_in;
}
function getAuthorAndTitle() {
return $this->book->getTitle().' by '.$this->book->getAuthor();
}
}
// Usage
$book = new SimpleBook("Gamma, Helm, Johnson, and Vlissides", "Design Patterns");
$bookAdapter = new BookAdapter($book);
echo 'Author and Title: '.$bookAdapter->getAuthorAndTitle();
function echo $line_in) {
echo $line_in."<br/>";
}
Behavioral Patterns
Strategy(策略模式)
测试模式主要为了让客户类能够更好地使用某些算法而不需要知道其具体的实现。
<?php
interface OutputInterface {
public function load();
}
class SerializedArrayOutput implements OutputInterface {
public function load() {
return serialize($arrayOfData);
}
}
class JsonStringOutput implements OutputInterface {
public function load() {
return json_encode($arrayOfData);
}
}
class ArrayOutput implements OutputInterface {
public function load() {
return $arrayOfData;
}
}
<?php
interface OutputInterface {
public function load();
}
class SerializedArrayOutput implements OutputInterface {
public function load() {
return serialize($arrayOfData);
}
}
class JsonStringOutput implements OutputInterface {
public function load() {
return json_encode($arrayOfData);
}
}
class ArrayOutput implements OutputInterface {
public function load() {
return $arrayOfData;
}
}
Observer(观察者模式)
某个对象可以被设置为是可观察的,只要通过某种方式允许其他对象注册为观察者。每当被观察的对象改变时,会发送信息给观察者。
<?php
interface Observer {
function onChanged($sender, $args);
}
interface Observable {
function addObserver($observer);
}
class CustomerList implements Observable {
private $_observers = array();
public function addCustomer($name) {
foreach($this->_observers as $obs)
$obs->onChanged($this, $name);
}
public function addObserver($observer) {
$this->_observers []= $observer;
}
}
class CustomerListLogger implements Observer {
public function onChanged($sender, $args) {
echo( "'$args' Customer has been added to the list n" );
}
}
$ul = new UserList();
$ul->addObserver( new CustomerListLogger() );
$ul->addCustomer( "Jack" );
<?php
interface Observer {
function onChanged($sender, $args);
}
interface Observable {
function addObserver($observer);
}
class CustomerList implements Observable {
private $_observers = array();
public function addCustomer($name) {
foreach($this->_observers as $obs)
$obs->onChanged($this, $name);
}
public function addObserver($observer) {
$this->_observers []= $observer;
}
}
class CustomerListLogger implements Observer {
public function onChanged($sender, $args) {
echo( "'$args' Customer has been added to the list n" );
}
}
$ul = new UserList();
$ul->addObserver( new CustomerListLogger() );
$ul->addCustomer( "Jack" );
Chain of responsibility(责任链模式)
这种模式有另一种称呼:控制链模式。它主要由一系列对于某些命令的处理器构成,每个查询会在处理器构成的责任链中传递,在每个交汇点由处理器判断是否需要对它们进行响应与处理。每次的处理程序会在有处理器处理这些请求时暂停。
<?php
interface Command {
function onCommand($name, $args);
}
class CommandChain {
private $_commands = array();
public function addCommand($cmd) {
$this->_commands[]= $cmd;
}
public function runCommand($name, $args) {
foreach($this->_commands as $cmd) {
if ($cmd->onCommand($name, $args))
return;
}
}
}
class CustCommand implements Command {
public function onCommand($name, $args) {
if ($name != 'addCustomer')
return false;
echo("This is CustomerCommand handling 'addCustomer'n");
return true;
}
}
class MailCommand implements Command {
public function onCommand($name, $args) {
if ($name != 'mail')
return false;
echo("This is MailCommand handling 'mail'n");
return true;
}
}
$cc = new CommandChain();
$cc->addCommand( new CustCommand());
$cc->addCommand( new MailCommand());
$cc->runCommand('addCustomer', null);
$cc->runCommand('mail', null);
<?php
interface Command {
function onCommand($name, $args);
}
class CommandChain {
private $_commands = array();
public function addCommand($cmd) {
$this->_commands[]= $cmd;
}
public function runCommand($name, $args) {
foreach($this->_commands as $cmd) {
if ($cmd->onCommand($name, $args))
return;
}
}
}
class CustCommand implements Command {
public function onCommand($name, $args) {
if ($name != 'addCustomer')
return false;
echo("This is CustomerCommand handling 'addCustomer'n");
return true;
}
}
class MailCommand implements Command {
public function onCommand($name, $args) {
if ($name != 'mail')
return false;
echo("This is MailCommand handling 'mail'n");
return true;
}
}
$cc = new CommandChain();
$cc->addCommand( new CustCommand());
$cc->addCommand( new MailCommand());
$cc->runCommand('addCustomer', null);
$cc->runCommand('mail', null);