无论是身处学校还是步入社会,大家都尝试过写作吧,借助写作也可以提高我们的语言组织能力。写范文的时候需要注意什么呢?有哪些格式需要注意呢?这里我整理了一些优秀的范文,希望对大家有所帮助,下面我们就来了解一下吧。
php中的设计模式详解篇一
导语:设计模式并不是一种用来解释的模式,它们并不是像链表那样的常见的数据结构,也不是某种特殊的应用或者框架设计。下面是php中的设计模式详解,一起来学习下吧:
descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context.
另一方面,设计模式提供了一种广泛的可重用的方式来解决我们日常编程中常常遇见的问题。设计模式并不一定就是一个类库或者第三方框架,它们更多的表现为一种思想并且广泛地应用在系统中。它们也表现为一种模式或者模板,可以在多个不同的场景下用于解决问题。设计模式可以用于加速开发,并且将很多大的想法或者设计以一种简单地方式实现。当然,虽然设计模式在开发中很有作用,但是千万要避免在不适当的场景误用它们。
目前常见的设计模式主要有23种,根据使用目标的不同可以分为以下三大类:
创建模式:用于创建对象从而将某个对象从实现中解耦合。
架构模式:用于在不同的对象之间构造大的对象结构。
行为模式:用于在不同的对象之间管理算法、关系以及职责。
单例模式是最常见的模式之一,在web应用的开发中,常常用于允许在运行时为某个特定的类创建一个可访问的实例。
/** * singleton class */ final class product { /** * @var self */ private static $instance; /** * @var mixed */ public $mix; /** * return self instance * * @return self */ public static function getinstance() { if (!(self::$instance instanceof self)) { self::$instance = new self(); } return self::$instance; } private function __construct() { } private function __clone() { } } $firstproduct = product::getinstance(); $secondproduct = product::getinstance(); $firstproduct->mix = 'test'; $secondproduct->mix = 'example'; print_r($firstproduct->mix); // example print_r($secondproduct->mix); // example
在很多情况下,需要为系统中的多个类创建单例的构造方式,这样,可以建立一个通用的抽象父工厂方法:
abstract class factoryabstract { protected static $instances = array(); public static function getinstance() { $classname = static::getclassname(); if (!(self::$instances[$classname] instanceof $classname)) { self::$instances[$classname] = new $classname(); } return self::$instances[$classname]; } public static function removeinstance() { $classname = static::getclassname(); if (array_key_exists($classname, self::$instances)) { unset(self::$instances[$classname]); } } final protected static function getclassname() { return get_called_class(); } protected function __construct() { } final protected function __clone() { } } abstract class factory extends factoryabstract { final public static function getinstance() { return parent::getinstance(); } final public static function removeinstance() { parent::removeinstance(); } } // using: class firstproduct extends factory { public $a = []; } class secondproduct extends firstproduct { } firstproduct::getinstance()->a[] = 1; secondproduct::getinstance()->a[] = 2; firstproduct::getinstance()->a[] = 3; secondproduct::getinstance()->a[] = 4; print_r(firstproduct::getinstance()->a); // array(1, 3) print_r(secondproduct::getinstance()->a); // array(2, 4)
注册台模式并不是很常见,它也不是一个典型的创建模式,只是为了利用静态方法更方便的存取数据。
/**
* registry class
*/
class package {
protected static $data = array();
public static function set($key, $value) {
self::$data[$key] = $value;
}
public static function get($key) {
return isset(self::$data[$key]) ? self::$data[$key] : null;
}
final public static function removeobject($key) {
if (array_key_exists($key, self::$data)) {
unset(self::$data[$key]);
}
}
}
package::set('name', 'package name');
print_r(package::get('name'));
// package name
工厂模式是另一种非常常用的模式,正如其名字所示:确实是对象实例的.生产工厂。某些意义上,工厂模式提供了通用的方法有助于我们去获取对象,而不需要关心其具体的内在的实现。
interface factory { public function getproduct(); } interface product { public function getname(); } class firstfactory implements factory { public function getproduct() { return new firstproduct(); } } class secondfactory implements factory { public function getproduct() { return new secondproduct(); } } class firstproduct implements product { public function getname() { return 'the first product'; } } class secondproduct implements product { public function getname() { return 'second product'; } } $factory = new firstfactory(); $firstproduct = $factory->getproduct(); $factory = new secondfactory(); $secondproduct = $factory->getproduct(); print_r($firstproduct->getname()); // the first product print_r($secondproduct->getname()); // second product
有些情况下我们需要根据不同的选择逻辑提供不同的构造工厂,而对于多个工厂而言需要一个统一的抽象工厂:
class config { public static $factory = 1; } interface product { public function getname(); } abstract class abstractfactory { public static function getfactory() { switch (config::$factory) { case 1: return new firstfactory(); case 2: return new secondfactory(); } throw new exception('bad config'); } abstract public function getproduct(); } class firstfactory extends abstractfactory { public function getproduct() { return new firstproduct(); } } class firstproduct implements product { public function getname() { return 'the product from the first factory'; } } class secondfactory extends abstractfactory { public function getproduct() { return new secondproduct(); } } class secondproduct implements product { public function getname() { return 'the product from second factory'; } } $firstproduct = abstractfactory::getfactory()->getproduct(); config::$factory = 2; $secondproduct = abstractfactory::getfactory()->getproduct(); print_r($firstproduct->getname()); // the first product from the first factory print_r($secondproduct->getname()); // second product from second factory
对象池可以用于构造并且存放一系列的对象并在需要时获取调用:
class factory { protected static $products = array(); public static function pushproduct(product $product) { self::$products[$product->getid()] = $product; } public static function getproduct($id) { return isset(self::$products[$id]) ? self::$products[$id] : null; } public static function removeproduct($id) { if (array_key_exists($id, self::$products)) { unset(self::$products[$id]); } } } factory::pushproduct(new product('first')); factory::pushproduct(new product('second')); print_r(factory::getproduct('first')->getid()); // first print_r(factory::getproduct('second')->getid()); // second
对于某个变量的延迟初始化也是常常被用到的,对于一个类而言往往并不知道它的哪个功能会被用到,而部分功能往往是仅仅被需要使用一次。
interface product { public function getname(); } class factory { protected $firstproduct; protected $secondproduct; public function getfirstproduct() { if (!$this->firstproduct) { $this->firstproduct = new firstproduct(); } return $this->firstproduct; } public function getsecondproduct() { if (!$this->secondproduct) { $this->secondproduct = new secondproduct(); } return $this->secondproduct; } } class firstproduct implements product { public function getname() { return 'the first product'; } } class secondproduct implements product { public function getname() { return 'second product'; } } $factory = new factory(); print_r($factory->getfirstproduct()->getname()); // the first product print_r($factory->getsecondproduct()->getname()); // second product print_r($factory->getfirstproduct()->getname()); // the first product
有些时候,部分对象需要被初始化多次。而特别是在如果初始化需要耗费大量时间与资源的时候进行预初始化并且存储下这些对象。
interface product { } class factory { private $product; public function __construct(product $product) { $this->product = $product; } public function getproduct() { return clone $this->product; } } class someproduct implements product { public $name; } $prototypefactory = new factory(new someproduct()); $firstproduct = $prototypefactory->getproduct(); $firstproduct->name = 'the first product'; $secondproduct = $prototypefactory->getproduct(); $secondproduct->name = 'second product'; print_r($firstproduct->name); // the first product print_r($secondproduct->name); // second product
构造者模式主要在于创建一些复杂的对象:
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
装饰器模式允许我们根据运行时不同的情景动态地为某个对象调用前后添加不同的行为动作。
class htmltemplate {
// any parent class methods
}
class template1 extends htmltemplate {
protected $_html;
public function __construct() {
$this->_html = "
__text__
"; } 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("
" . $this->_html . "
"); } 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("" . $this->_html . ""); } public function __call($name, $args) { $this->_element->$name($args[0]); } } adapter(适配器模式)
这种模式允许使用不同的接口重构某个类,可以允许使用不同的调用方式进行调用:
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."
"; }
behavioral patterns
测试模式主要为了让客户类能够更好地使用某些算法而不需要知道其具体的实现。
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;
}
}
某个对象可以被设置为是可观察的,只要通过某种方式允许其他对象注册为观察者。每当被观察的对象改变时,会发送信息给观察者。
interface observer { function onchanged($sender, $args); }
<
s("content_relate");【php中的设计模式详解】相关文章:
php中动态html的输出技术详解06-07php中isset()与empty()的使用区别详解06-07php中读取大文件实现方法详解09-23php中try catch捕获异常实例详解08-2108-20php面向对象的魔术方法详解09-03php字符串分割的详解06-08php中的trait09-17php数据类型转换详解07-26

一键复制