五种常见的PHP设计模式

来源:互联网 发布:org.apache.http.impl 编辑:程序博客网 时间:2024/06/05 23:50

直奔主题


工厂模式

松散耦合,工厂模式是一种类,它具有创建对象的方法。可以直接使用工厂类创建对象,而不用new。

清单一显示一个工厂类的示例

<?phpinterface IUser{    function getName();}class User implements IUser{    function getName()    {        return 'gphjl';    }}class UserFactory{    public static function create()    {        return new User();    }}$user = UserFactory::create();echo $user->getName();

单例模式

通过单例模式可以保证系统中一个类只有一个实例且易被外界访问,从而方便对实例个数的控制并节约系统资源。

清单二显示一个单例示例

<?phpclass Singleton{    //保证类实例的私有静态成员变量    private static $_instance;    //构造函数和克隆函数必须声明为私有的,防止外部程序new类从而失去单例模式的意义    private function __construct(){}    private function __clone(){}    //检测类是否被实例化,返回唯一实例的引用    public static function getInstance()    {        if (!(self::$_instance instanceof self)) {            self::$_instance = new self;        }        return self::$_instance;    }}//调用单例类Singleton::getInstance();

观察者模式

观察者模式提供了避免组件之间紧密耦合的另一种方法。此模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象,当改主题对象发生改变时,会通知所有观察者对象,使它们能自动更新自己。

清单三显示一个观察者模式的示例

<?php//抽象观察者:为所有具体观察者提供一个接口,在得到主题通知时更改自己interface IObserver{     function update();}//具体观察者:实现抽象观察者所要求的更新接口,使自己本身状态与主题状态协调class ConcreteObserver implements IObserver{     function update()     {         // TODO     }}//抽象主题:把所有观察者放入一个聚类中,每个主题可以有任意多个观察者。抽象主题提供一个接口,可以增加删除观察者对象interface ISubject{    function add($observer);}//具体主题:实现接口是本身变的可观察,当具体主题内部状态改变时,给登记过的观察者发出通知class ConcreteSubject implements ISubject{     private $_observers = array();     public function add($observer)     {          $this->_observers[] = $observer;     }     public function notify()     {          foreach ($this->_observers as $observer) {              $observer->update();          }     }}$subject = new ConcreteSubject();$subject->add(new ConcreteObserver());$subject->notify();

命令链模式

命令链模式以松散耦合主题为基础,发送命令,消息和请求,或通过一组处理程序发送任意内容。每个处理程序能判断自己是否能处理请求,如果可以则处理该请求。此模式可以为系统添加或移除处理程序,而不影响其他处理程序。

清单四显示一个命令链模式的示例

<?phpinterface ICommand{    function onCommand($args);}class CommandChain{    private $_commands = array();    public function addCommand($command)    {        $this->_commands[] = $command;    }    public function runCommand($args)    {        foreach ($this->_commands as $command) {            $command->onCommand($args);        }    }}class UserCommand implements ICommand{    public function onCommand($args)    {        if ('user' == $args) {            echo 'This command is User';        }    }}class EmailCommand implements ICommand{    public function onCommand($args)    {        if ('email' == $args) {            echo 'This command is Email';        }    }}$commandChain = new CommandChain();$commandChain->addCommand(new UserCommand());$commandChain->addCommand(new EmailCommand());$commandChain->runCommand('user');$commandChain->runCommand('email');


策略模式

定义一系列算法,把每个算法封装起来。策略模式非常适合复杂数据管理系统和数据处理系统,二者在数据筛选,搜索,处理方面需要较高的灵活性。

清单五显示一个策略模式的示例

<?php//抽象策略类:定义所有支持的算法的公共接口interface IStrategy{    function travelMethod();}//具体策略类class AirplaneStrategy implements IStrategy{    public function travelMethod()    {        echo 'trval by Airplane';    }}class TrainStrategy implements IStrategy{    public function travelMethod()    {        echo 'trval by Train';    }}//环境类class Context{    private $_strategy;    public function __construct($strategy)    {        $this->_strategy = $strategy;    }    public function travel()    {        echo $this->_strategy->travelMethod();    }}$method = new Context(new AirplaneStrategy());$method->travel();



0 0
原创粉丝点击