php设计模式专题附源码(解释器模式、工厂方法模式、外观模式、装饰模式、建造者模式)
2015-08-27 21:17
871 查看
<?php /** *解释器示例* *@create * _date:2010-01-04 * * 解释器模式(Interpreter) * 定义: 给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子 */ header("Content-type:text/html;charset=utf-8"); class Expression { function interpreter($str) { return $str; } } class ExpressionNum extends Expression { function interpreter($str) { switch ($str) { case "0": return "零"; case "1": return "一"; case "2": return "二"; case "3": return "三"; case "4": return "四"; case "5": return "五"; case "6": return "六"; case "7": return "七"; case "8": return "八"; case "9": return "九"; } } } class ExpressionCharater extends Expression { function interpreter($str) { return strtoupper($str); } } class Interpreter { function execute($string) { $expression = null; for ($i = 0; $i < strlen($string); $i++) { $temp = $string[$i]; switch (true) { case is_numeric($temp): $expression = new ExpressionNum(); break; default: $expression = new ExpressionCharater(); } echo $expression->interpreter($temp); } } } $obj = new Interpreter(); $obj->execute("12345abc"); ?>
<?php /** *工厂方法模式* *定义一个用于创建对象的接口,让子类决定将哪一个类实例化,使用一个类的实例化延迟到其子类*/ class DBFactory { public static function create($type) { switch($type) { case "Mysql": return new MysqlDB(); break; case "Postgre": return new PostgreDB(); break; case "Mssql": return new MssqlDB(); break; } } } /** class DBFactory { public static function create($type) { $class = $type . "DB"; return new $class; } } **/ interface DB { public function connect(); public function exec(); } class MysqlDB implements DB { public function __construct() { echo "mysqldb<br/>"; } public function connect() { } public function exec() { } } class PostgreDB implements DB { public function __construct() { echo "Postgredb<br/>"; } public function connect() { } public function exec() { } } class MssqlDB implements DB { public function __construct() { echo "mssqldb<br/>"; } public function connect() { } public function exec() { } } $oMysql = DBFactory::create("Mysql"); $oPostgre = DBFactory::create("Postgre"); $oMssql = DBFactory::create("Mssql");
<?php /** *外观模式示例* *为子系统中的一组接口提供一个一致的界面,定义一个高层接口,使得这一子系统更加的容易使用*/ class SubSytem1 { public function Method1() { echo "subsystem1 method1<br/>"; } } class SubSytem2 { public function Method2() { echo "subsystem2 method2<br/>"; } } class SubSytem3 { public function Method3() { echo "subsystem3 method3<br/>"; } } class Facade { private $_object1 = null; private $_object2 = null; private $_object3 = null; public function __construct() { $this->_object1 = new SubSytem1(); $this->_object2 = new SubSytem2(); $this->_object3 = new SubSytem3(); } public function MethodA() { echo "Facade MethodA<br/>"; $this->_object1->Method1(); $this->_object2->Method2(); } public function MethodB() { echo "Facade MethodB<br/>"; $this->_object2->Method2(); $this->_object3->Method3(); } } //实例化 $objFacade = new Facade(); $objFacade->MethodA(); $objFacade->MethodB();
<?php /** *装饰模式* *动态的给一个对象添加一些额外的职责,就扩展功能而言比生成子类方式更为灵活*/ header("Content-type:text/html;charset=utf-8"); abstract class MessageBoardHandler { public function __construct() { } abstract public function filter($msg); } class MessageBoard extends MessageBoardHandler { public function filter($msg) { return "处理留言板上的内容|" . $msg; } } $obj = new MessageBoard(); echo $obj->filter("留言内容:一定要学好装饰模式<br/>"); //---以下是使用装饰模式---- class MessageBoardDecorator extends MessageBoardHandler{ private $_handler = null; public function __construct($handler) { parent::__construct(); $this->_handler = $handler; } public function filter($msg) { return $this->_handler->filter($msg); } } //过滤html class HtmlFilter extends MessageBoardDecorator { public function __construct($handler) { parent::__construct($handler); } public function filter($msg) { return "过滤掉HTML标签|" . parent::filter($msg); ; //过滤掉HTML标签的处理这时只是加个文字没有进行处理 } } //过滤敏感词 class SensitiveFilter extends MessageBoardDecorator { public function __construct($handler) { parent::__construct($handler); } public function filter($msg) { return "过滤掉敏感词|" . parent::filter($msg); //过滤掉敏感词的处理这时只是加个文字没有进行处理 } } $obj = new HtmlFilter(new SensitiveFilter(new MessageBoard())); echo $obj->filter("留言内容:一定要学好装饰模式!<br/>");
<?php /** *建造者模式* *将一个复杂对象的构建与它的表示分离,使用同样的构建过程可以创建不同的表示*/ class Product { public $_type = null; public $_size = null; public $_color = null; public function setType($type) { echo "set product type<br/>"; $this->_type = $type; } public function setSize($size) { echo "set product size<br/>"; $this->_size = $size; } public function setColor($color) { echo "set product color<br/>"; $this->_color = $color; } } $config = array( "type" => "shirt", "size" => "xl", "color" => "red", ); //没有使用bulider以前的处理 $oProduct=new Product(); $oProduct->setType($config['type']); $oProduct->setSize($config['size']); $oProduct->setColor($config['color']); //创建一个builder类 class ProductBuilder { var $_config = null; var $_object = null; public function ProductBuilder($config) { $this->_object = new Product(); $this->_config = $config; } public function build() { echo "---inbuilder---<br/>"; $this->_object->setType($this->_config['type']); $this->_object->setSize($this->_config['size']); $this->_object->setColor($this->_config['color']); } public function getProduct() { return $this->_object; } } $objBuilder = new ProductBuilder($config); $objBuilder->build(); //$objProduct = $objBuilder->getProduct();
相关文章推荐
- tftp协议的实现
- 配置FTP服务
- phpmyadmin 尝试连接到 mysql 服务器被拒绝
- php fpm start.sh
- EditPlus+VisualStudio配置VC简易开发环境环境
- php大力力 [028节] 如何下载js文件,网上一个*.js无法下载啊??????
- php 内存不足而,报错
- php安装完后配置php.ini和php-fpm.conf
- mac 下 sphinx + mysql + php 实现全文搜索(xampp)(4)php api 解析
- PHP中isset、empty、is_null实验测试
- Erlang:请求php数据,GET、POST
- 探讨php中error_log函数输出内容的原子性问题
- THINKPHP 中密码在编辑中不填写即不修改,ignore和md5冲突的解决办法
- thinkphp模板中判断变量是否为空
- [源码]简易投票系统
- 何找出PHP后门
- PHP学习笔记(一)PHP环境配置
- PHP新手必须掌握的入门与实战技巧
- PHP的autoLoad自动加载机制 的几种方式
- 改进PHP的var_dump()方法使之适应显示从数据库中查出来的数据