设计模式的 C++ 实现---外观模式

前文回顾
单例模式(一)
单例模式(二)
观察者模式
简单工厂模式
工厂方法模式(一)
工厂方法模式(二)
抽象工厂模式(一)
抽象工厂模式(二)
原型模式

前言

外观模式本质就是对一系列子系统或者一组方法的进一步封装,使客户端的不需要知道子系统的实现细节,达到“一键操作”的目的。

实现举例

  1. 子系统1
#include <iostream>
using namespace std;

class SubSystemOne
{
public:
    SubSystemOne(){}
    void MethodOne()
    {
        cout << "执行子系统方法一"<<endl;
    }
};
  1. 子系统2
#include <iostream>
using namespace std;

class SubSystemTwo
{
public:
    SubSystemTwo(){}
    void MethodTwo()
    {
        cout << "执行子系统方法二" <<endl;
    }
};
  1. 子系统3
#include <iostream>
using namespace std;

class SubSystemThree
{
public:
    SubSystemThree(){}
    void MethodThree()
    {
        cout <<"执行子系统方法三"<<endl;
    }
};
  1. 子系统4
#include <iostream>
using namespace std;

class SubSystemFour
{
public:
    SubSystemFour(){}
    void MethodFour()
    {
        cout << "执行子系统方法四" << endl;
    }
};
  1. 外观类
#include "subsystemone.h"
#include "subsystemtwo.h"
#include "subsystemthree.h"
#include "subsystemfour.h"

class Facade
{
public:
    Facade(){}
    void MethodA()
    {
        cout << "***方法组A***"<<endl;
        one.MethodOne();
        two.MethodTwo();
        three.MethodThree();
    }
    void MethodB()
    {
        cout << "***方法组B***"<<endl;
        four.MethodFour();
        two.MethodTwo();
        three.MethodThree();
    }
private:
    SubSystemOne one;
    SubSystemTwo two;
    SubSystemThree three;
    SubSystemFour four;
};

  1. 客户端调用
#include "facade.h"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Facade facade;
    facade.MethodA();
    facade.MethodB();

    return a.exec();
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值