zoukankan      html  css  js  c++  java
  • 外观模式

    原文链接: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设计模式》这本书上给出的例子跟建造者模式太像了,而上边的例子很合适,而且原文讲的也很好。

  • 相关阅读:
    JZOJ 3034. 【NOIP2012模拟10.17】独立集
    JZOJ 3035. 【NOIP2012模拟10.17】铁轨
    JZOJ 1259. 牛棚安排
    数位DP JZOJ 3316. 非回文数字
    JZOJ 3046. 游戏
    JZOJ 3013. 填充棋盘
    debian 安装oracle提供的java8
    java 汉字转拼音 PinYin4j
    debian ssh设置root权限登陆 Permission denied, please try again
    java并发下订单生成策略
  • 原文地址:https://www.cnblogs.com/orlion/p/5350923.html
Copyright © 2011-2022 走看看