• 外观模式


    原文链接:http://www.orlion.ga/744/

    解释:

        通过在必须的逻辑和方法的集合前创建简单的外观接口,外观设计模式隐藏了来自调用对象的复杂性。

    代码:

    /**
     * 外观模式
     *
     */
     class SwitchFacade
    {
    	private $_light 	= null;	 	//电灯
    	private $_ac	 	= null;		//空调
    	private $_fan	 	= null;		//电扇
    	private $_tv	 	= null;		//电视
    	
    	public function __construct()
    	{
    		$this->_light = new Light();
    		$this->_fan = new Fan();
    		$this->_ac = new AirConditioner();
    		$this->_tv = new Television();
    	}
    	/**
    	 * 晚上开电灯
    	 *
    	 */
    	public function method1($isOpen =1) {
    		if ($isOpen == 1) {
    			$this->_light->on();
    			$this->_fan->on();
    			$this->_ac->on();
    			$this->_tv->on();
    		}else{
    			$this->_light->off();
    			$this->_fan->off();
    			$this->_ac->off();
    			$this->_tv->off();
    		}
    
    	}
    	/**
    	 * 白天不需要电灯
    	 *
    	 */
    	public function method2() {
    		if ($isOpen == 1) {
    			$this->_fan->on();
    			$this->_ac->on();
    			$this->_tv->on();
    		}else{
    			$this->_fan->off();
    			$this->_ac->off();
    			$this->_tv->off();
    		}
    	}
    }
    
    /******************************************子系统类 ************/
    /**
     * 
    */ 
    class Light
    {	
    	private $_isOpen = 0;
    	public function on() {
    		echo 'Light is open', '<br/>';
    		$this->_isOpen = 1; 
    	}
    	public function off() {
    		echo 'Light is off', '<br/>';
    		$this->_isOpen = 0;
    	}
    }
    
    class Fan
    {
    	private $_isOpen = 0;
    	public function on() {
    		echo 'Fan is open', '<br/>';
    		$this->_isOpen = 1; 
    	}
    	public function off() {
    		echo 'Fan is off', '<br/>';
    		$this->_isOpen = 0;
    	}
    }
    
    class AirConditioner
    {
    	private $_isOpen = 0;
    	public function on() {
    		echo 'AirConditioner is open', '<br/>';
    		$this->_isOpen = 1; 
    	}
    	public function off() {
    		echo 'AirConditioner is off', '<br/>';
    		$this->_isOpen = 0;
    	}
    }
    class Television
    {
    	private $_isOpen = 0;
    	public function on() {
    		echo 'Television is open', '<br/>';
    		$this->_isOpen = 1; 
    	}
    	public function off() {
    		echo 'Television is off', '<br/>';
    		$this->_isOpen = 0;
    	}
    }
    /**
     * 客户类
     *
     */
    class client {
    	static function open() {
    		$f = new  SwitchFacade();
    		$f->method1(1);
    	}
    
    	static function close() {
    		$f = new  SwitchFacade();
    		$f->method1(0);
    	}
    }
    client::open();

        代码来自:http://blog.csdn.net/hguisu/article/details/7533759,《PHP设计模式》这本书上给出的例子跟建造者模式太像了,而上边的例子很合适,而且原文讲的也很好。

  • 相关阅读:
    2019牛客多校第二场H题(悬线法)
    hdu6212 Zuma(区间dp)
    uva1428树状数组
    UVA1395 (最苗条的最小生成树)
    牛客练习赛53 C题bitset
    Love Live!
    Princess principal
    New Game!- 牛客
    P3311 [SDOI2014]数数
    [HNOI2008]GT考试
  • 原文地址:https://www.cnblogs.com/orlion/p/5350923.html
Copyright © 2020-2023  润新知