大话设计模式第十二章---外观模式PHP实现

<?php
class Sub_system_one {
    public function method_one() {
        echo "subsystem one method one<br/>";
    }
}
class Sub_system_two {
    public function method_two() {
        echo "subsystem one method two<br/>";
    }
}
class Sub_system_three {
    public function method_three() {
        echo "subsystem one method three<br/>";
    }
}
class Sub_system_four {
    public function method_four() {
        echo "subsystem one method four<br/>";
    }
}

class Facade {
    private $_one;
    private $_two;
    private $_three;
    private $_four;

    public function __construct() {
        $this->_one   = new Sub_system_one();
        $this->_two   = new Sub_system_two();
        $this->_three = new Sub_system_three();
        $this->_four  = new Sub_system_four();
    }

    public function method_A() {
        echo "method group A<br/>";
        $this->_one->method_one();
        $this->_two->method_two();
        $this->_four->method_four();
    }
    public function method_B() {
        echo "method group B<br/>";
        $this->_two->method_two();
        $this->_three->method_three();
    }
}


$facade = new Facade();
$facade->method_A();
$facade->method_B();

 

外观模式:

  为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。(其实就是多加了一层,把底层代码的实现给封装起来)

何时使用:

  首先,在设计初期阶段,应该要有意识将不同的两个层分离;

  其次,在开发阶段,子系统往往因为不断的重构演化而变得越来越复杂,增加外观Facade可以提供一个简单的接口,减少它们之间的依赖;

  第三,在维护一个遗留的大型系统时,可能这个系统已经非常难以维护和扩展,此时,你可以为新系统开发一个外观Facade类,来提供设计粗糙或高度复杂的遗留代码的比较清晰简单的接口,让新系统与Facade对象交互,Facade与遗留代码交互所有复杂的工作。

posted @ 2015-08-30 16:45  wy0314  阅读(488)  评论(0)    收藏  举报