外观

来源:互联网 发布: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
原创粉丝点击