• php设计模式2


    代理模式

    <?php
    
    /**
     * 代理模式:为其他对象提供一个代理以控制这个对象的访问
                 它是给某一个对象提供一个替代者,使之在client对象和subject对象之间编码更有效率。
                 代理可以提供延迟实例化,控制访问等
    
     应用场景:如果需要创建一个资源消耗较大的对象,先创建一个消耗相对较小的对象来表示,真实对象只在需要时才会被真正创建。
               控制对原始对象的访问。
     *
     */
    
    /**
     *  抽象主题角色(Subject):天气
     *
     */
    interface Weather
    {
        public function request($city);
        public function display($city);
        public function isValidCity($city);
    
    }
    
    /**
     * 真实主题角色(RealSubject):
     *
     */
    class RealWeather implements Weather
    {
        protected $_url = 'http://www.google.com/ig/api?&oe=utf-8&hl=zh-cn&weather=';
        protected $_weatherXml = '' ;
        function __construct(){
    
        }
    
        public function request($city){
            $this->_weatherXml = file_get_contents($this->_url . $city );
        }
        public function display($city ){
    //        if ($this->_weatherXml == '') {
    //            $this->request($city);
    //        }
            echo  '天气预报';
        }
    
        public function isValidCity($city){
    
        }
    
    }
    
    /**
     * 代理角色(Proxy):延迟代理
     *
     */
    class ProxyWeather  implements Weather {
        private $_client ;
    
        private function client() {
            if (! $this->_client instanceof RealWeather) {
                $this->_client = new RealWeather();
            }
            return $this->_client;
        }
    
        public function request($city){
            $this->_client->request($city);
        }
    
        public function isValidCity($city) {
            return $this->_client->isValidCity($city);
        }
    
    
        public function display($city) {
            return $this->client()->display($city);
        }
    }
    /**
     * 代理角色(Proxy):动态代理
     *
     */
    class DynamicProxyWeather {
        protected $_subject;
    
        public function __construct($subject) {
            $this->_subject = $subject;
        }
    
        public function __call($method, $args) {
            return call_user_func_array([$this->_subject, $method], $args);
        }
    
    }
    
    // 客户端
    class Client{
    
        public static function proxy(){
            $proxy = new ProxyWeather();
            $proxy->display('beijing');
        }
    
        public static function dynamic(){
            $proxy = new DynamicProxyWeather(new RealWeather());
            $proxy->display('beijing');
        }
    }
    
    Client::proxy();
    Client::dynamic();

    桥连模式

    <?php
    /**
     * 桥连模式:实现系统在多个维度上的独立变化
                Bridge模式基于类的最小设计原则,通过使用封装、聚合及继承等行为让不同的类承担不同的职责。
                它的主要特点是把抽象(Abstraction)与行为实现(Implementation)分离开来,从而可以保持各部分的独立性以及应对他们的功能扩展。
                主要解决:在有多种可能会变化的情况下,用继承会造成类爆炸问题,扩展起来不灵活。
                eg:显示器有21、23、27寸,颜色有黑色、白色、花色,通常做法会产生9个类[21黑,21白,21花,23黑,23白,23花,27黑,27白,27花],
                如果使用Bridge模式,只需要6个类即可【21,23,27,黑色,白色,花色】
     */
    
    // 尺寸基类
    class BaseSize
    {
        protected $_size;
    
        public function setSize()
        {
            echo '尺寸是:' . $this->_size . '英寸;';
        }
    }
    
    // 颜色基类
    class BaseColor
    {
        protected $_color;
    
        public function setColor()
        {
            echo '颜色是:' . $this->_color;
        }
    }
    
    class Size21 extends BaseSize
    {
        public function __construct()
        {
            $this->_size = 21;
        }
    }
    
    class Size23 extends BaseSize
    {
        public function __construct()
        {
            $this->_size = 23;
        }
    }
    
    class Size27 extends BaseSize
    {
        public function __construct()
        {
            $this->_size = 27;
        }
    }
    
    class ColorBlack extends BaseColor
    {
        public function __construct()
        {
            $this->_color = '黑色';
        }
    }
    
    class ColorWhite extends BaseColor
    {
        public function __construct()
        {
            $this->_color = '白色';
        }
    }
    
    class ColorSuit extends BaseColor
    {
        public function __construct()
        {
            $this->_color = '花色';
        }
    }
    
    class Client
    {
        public function buy()
        {
            echo '我要一台显示器:';
            (new Size27())->setSize();
            (new ColorWhite())->setColor();
        }
    }
    
    (new Client())->buy();
    // 我要一台显示器:尺寸是:27英寸;颜色是:白色

    门面(外观)模式

    <?php
    
    /**
     * 外观(门面)模式:为了降低复杂性,常常将系统划分为若干个子系统,而一个功能常常是由子系统里面的若干个单元组合而成。
                        Facade模式定义了一个高层接口,这个接口又调用了子系统的多个类和方法。
                        这样,客户端只需要直接与Facade交互,客户端与子系统之间的复杂关系由Facade来实现,从而降低了系统的耦合度。
                        就是说,Facade对外隐藏了功能的具体实现细节,通过把这个功能的具体细节封装成一个接口让客户端调用。
                        Facade对象是外界访问子系统内部的唯一通道
     */
    
    //
    class Shell
    {
        public function set()
        {
            echo '手机装上外壳';
        }
    }
    
    // 屏幕
    class Screen
    {
        public function set()
        {
            echo '手机装上屏幕';
        }
    }
    
    // 电池
    class Battery
    {
        public function set()
        {
            echo '手机装上电池';
        }
    }
    
    // 富士康
    class Facade
    {
        // 组装手机
        public static function product()
        {
            // 如果顺序需要变动或者需要新增其他部件,在这里修改即可
    
            echo '开始组装手机';
    
            (new Shell())->set();
            (new Screen())->set();
            (new Battery())->set();
    
            echo '手机组装完成';
        }
    }
    
    // 苹果公司
    class Client
    {
        public function show()
        {
            // 复杂的组装交给 Facade 处理
            Facade::product();
        }
    }
    
    (new Client())->show();
    // 开始组装
    // 手机装上外壳
    // 手机装上屏幕
    // 手机装上电池
    // 手机组装完成

  • 相关阅读:
    [HNOI2006]鬼谷子的钱袋
    一日游与两道题
    [HNOI2009]梦幻布丁
    [Ahoi2009]self 同类分布
    50 days before NOI2017
    Topcoder SRM 606 div1题解
    Topcoder SRM 605 div1 题解
    Topcoder SRM 604 div1题解
    Topcoder SRM 603 div1题解
    Topcoder SRM 602 div1题解
  • 原文地址:https://www.cnblogs.com/cqingt/p/8822469.html
Copyright © 2020-2023  润新知