PHP设计模式系列(七):外观模式

来源:互联网 发布:java工作流开发 编辑:程序博客网 时间:2024/05/16 02:48

外观模式

外观模式(Facade Pattern):外部与一个子系统的通信必须通过一个统一的外观对象进行,为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。外观模式又称为门面模式,它是一种对象结构型模式。

模式结构

外观模式包含如下角色:

  • Facade: 外观角色
  • SubSystem:子系统角色

结构图

这里写图片描述

PHP代码实现

<?php/** * 外观模式 *///SubSystem:子系统角色class SubSystemOne{    public function MethodOne(){        var_dump('子系统方法一');    }}class SubSystemTwo{    public function MethodTwo(){        var_dump('子系统方法二');    }}class SubSystemThree{    public function MethodThree(){        var_dump('子系统方法三');    }}class SubSystemFour{    public function MethodFour(){        var_dump('子系统方法四');    }}//Facade: 外观角色class Facade{    public function __construct(){        $this->one=new SubSystemOne();        $this->two=new SubSystemTwo();        $this->three=new SubSystemThree();        $this->four=new SubSystemFour();    }    public function MethodA(){        var_dump('方法组A');        $this->one->MethodOne();        $this->two->MethodTwo();    }    public function MethodB(){        var_dump('方法组B');        $this->three->MethodThree();        $this->four->MethodFour();    }}$a=new Facade();$a->MethodA();$a->MethodB();

运行结果

string '方法组A' (length=10)string '子系统方法一' (length=18)string '子系统方法二' (length=18)string '方法组B' (length=10)string '子系统方法三' (length=18)string '子系统方法四' (length=18)
1 0
原创粉丝点击