外观
来源:互联网 发布:c 解析json字符串 编辑:程序博客网 时间:2024/04/28 21:12
叙述
示例
<?php/*** 外观类,封装子系统对外提供的接口* 只是封装接口,尽量不要在这个类里添加子系统的行为* 这里只是组合子系统的方法,拼接成一个完善的操作,以简化client对子系统的访问* 降低子系统和client之间的耦合*/class SwitchFacade { private $_light = null; //电灯 private $_airConditioner = null; //空调 private $_fan = null; //电扇 private $_television = null; //电视 protected function getRealItem($item) { $realItem = $item; if(!property_exists($this, $realItem)) { $realItem = '_'.$item; if(!property_exists($this, $realItem)) { throw new Exception($item." 属性不存在"); } } return $realItem; } public function __get($item) { //echo $item."\n"; $realItem = $this->getRealItem($item); if($this->$realItem === null) { $this->setInnerClass($realItem); } return $this->$realItem; } protected function setInnerClass($realItem) { if(substr($realItem, 0, 1) === '_') { $method = 'set'.ucfirst(substr($realItem, 1)); if(method_exists($this, $method)) { $this->$method(); } } } public function setLight() { $this->_light = new Light(); } public function setAirConditioner() { $this->_airConditioner = new AirConditioner(); } public function setFan() { $this->_fan = new Fan(); } public function setTelevision() { $this->_television = new Television(); } /** * 封装对外提供的接口,白天开关灯 */ public function dayUp($op=0) { switch ($op) { case 1: $this->fan->on(); $this->airConditioner->on(); $this->television->on(); break; default: $this->fan->off(); $this->airConditioner->off(); $this->television->off(); break; } } /** * 封装对外提供的接口,晚上开关灯 */ public function night($op=1) { switch ($op) { case 1: $this->light->on(); $this->fan->on(); $this->airConditioner->on(); $this->television->on(); break; default: $this->light->off(); $this->fan->off(); $this->airConditioner->off(); $this->television->off(); break; } }}/*** 子系统*//*** 电灯*/class Light { private $_isOpen = 0; public function on() { print_ln(__METHOD__); $this->_isOpen = 1; } public function off() { print_ln(__METHOD__); $this->_isOpen = 0; }}/*** 电扇*/class Fan { private $_isOpen = 0; public function on() { print_ln(__METHOD__); $this->_isOpen = 1; } public function off() { print_ln(__METHOD__); $this->_isOpen = 0; }}/*** 空调*/class AirConditioner { private $_isOpen = 0; public function on() { print_ln(__METHOD__); $this->_isOpen = 1; } public function off() { print_ln(__METHOD__); $this->_isOpen = 0; }}/*** 电视*/class Television { private $_isOpen = 0; public function on() { print_ln(__METHOD__); $this->_isOpen = 1; } public function off() { print_ln(__METHOD__); $this->_isOpen = 0; }}class Client { public static function main() { $o = new SwitchFacade(); $o->dayUp(); $o->night(); }}Client::main();
0 0
- 外观
- 外观交点
- DataList外观
- Facade(外观)
- 页面外观
- 外观设置
- 外观模式
- 外观模式
- 外观模式
- 外观模式
- 外观模式
- 外观模式
- 外观模式
- 外观模式
- 外观模式
- 外观模式
- 外观模式
- 外观模式
- C语言opendir()函数:打开目录函数
- js 刷新页面window.location…
- [js积累]thead 中多出空td、多个do…
- Maximum Product Subarray
- 反转单链表的几种方法
- 外观
- ffmpeg常用基本命令(转)
- 模板
- java第一课——helloworld
- Java Socket编程——TCP协议(一)
- centos下安装redis
- 策略
- base64编解码
- eval 与 bind 区别