码迷,mamicode.com
首页 > 其他好文 > 详细

外观模式

时间:2014-06-12 07:20:07      阅读:462      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   http   

定义

外观模式(Facade Pattern)要求一个子系统的外部与其内部的通信必须通过一个统一多的对象进行。外观模式定义了一个高层次的接口,使得子系统更易于使用。

外观模式通用类图

bubuko.com,布布扣

Facade 外观角色,客户端可以调用这个角色的方法。此角色知晓子系统的所有功能和责任。一般情况下,本角色会将所有从客户端发来的请求委派到相应的子系统去,也就是说该角色没有实际的业务逻辑,只是一个委托类。

Subsystem子系统角色

是一个类的集合。子系统并不知道外观的存在,对于子系统而言,外观仅仅是另外一个客户端而已。

外观模式的优点:

减少系统的相互依赖。

外观模式通过封装子系统,向上层模块提供统一的接口,从而降低的上层模块与子系统的过度耦合。

提高了灵活性。

提高安全性

缺点:

不符合开闭原则,即对修改关闭,对扩展开放,当业务逻辑比较复杂时,要修改外观的功能就必须去修改外观角色的代码。

通用源码

bubuko.com,布布扣
#include <iostream>

using namespace std;
class SubSystemOne
{
private:
    SubSystemOne(){};
    void MethodOne(){
        cout<<"子系统方法一"<<endl;
    }
    friend class Facade;
};
class SubSystemTwo
{
private:
    SubSystemTwo(){};
    void MethodTwo(){
        cout<<"子系统方法二"<<endl;
    }
    friend class Facade;
};
class SubSystemThree
{
private:
    SubSystemThree(){};
    void MethodThree(){
        cout<<"子系统方法三"<<endl;
    }
    friend class Facade;
};
class SubSystemFour
{
private:
    SubSystemFour(){};
    void MethodFour(){
        cout<<"子系统方法四"<<endl;
    }
    friend class Facade;
};

class Facade{
    SubSystemOne one;
    SubSystemTwo two;
    SubSystemThree three;
    SubSystemFour four;

public:
    void MethodA()
    {
        cout<<"方法组A"<<endl;
        one.MethodOne();
        two.MethodTwo();
        four.MethodFour();
    }
    void MethodB()
    {
        cout<<"方法组B"<<endl;
        two.MethodTwo();
        three.MethodThree();
    }
};
int main()
{
    Facade facade;
    facade.MethodA();
    facade.MethodB();
    return 0;
}
bubuko.com,布布扣

运行结果

bubuko.com,布布扣

外观模式,布布扣,bubuko.com

外观模式

标签:style   class   blog   code   java   http   

原文地址:http://www.cnblogs.com/xingchenfeng/p/3782665.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!