您的位置:首页 > 娱乐 > 明星 > 设计模式——外观模式

设计模式——外观模式

2025/2/23 13:07:59 来源:https://blog.csdn.net/weixin_42903300/article/details/139494553  浏览:    关键词:设计模式——外观模式

外观模式(Facade)

为系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。

在这里插入图片描述

#include <iostream>using namespace std;// 四个系统子类
class SubSystemOne
{
public:void MethodOne(){cout << "子系统方法一" << endl;}
};class SubSystemTwo
{
public:void MethodTwo(){cout << "子系统方法二" << endl;}
};class SubSystemThree
{
public:void MethodThree(){cout << "子系统方法三" << endl;}
};class SubSystemFour
{
public:void MethodFour(){cout << "子系统方法四" << endl;}
};// 外观类,需要了解所有子系统的方法或属性,进行组合,以备外界调用
class Facade
{
private:SubSystemOne one;SubSystemTwo two;SubSystemThree three;SubSystemFour four;public:Facade() : one(SubSystemOne()), two(SubSystemTwo()), three(SubSystemThree()), four(SubSystemFour()) {}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();facade.MethodA();facade.MethodB();return 0;
}

输出:

方法组A()------
子系统方法一
子系统方法二
子系统方法四
方法组B()------
子系统方法二
子系统方法三

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com