php设计模式专题附源码(解释器模式、工厂方法模式、外观模式、装饰模式、建造者模式)

<?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();


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值