C++设计模式-外观模式
发布日期:2021-06-30 11:01:51
浏览次数:12
分类:技术文章
本文共 2021 字,大约阅读时间需要 6 分钟。
目录
基本概念
外观模式(Facade):为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
增加外观Facade可以提供一个简单的接口,减少它们之间的依赖;
为新系统开发一个外观Facade类,来提供设计粗糙或高度复杂的遗留代码的比较清晰简单的接口,让新系统与Facade对象交互,Facade与遗留代码交互所有复杂的工作。
刚开始学面向对象程序设计的时候,基本上都会默认采用这种方法,因为它完美的提现了依赖倒转原则和迪米特法则的思想。
UML图如下:
代码与实例
程序运行截图如下:
源码如下:
Head.h
#ifndef HEAD_H#define HEAD_Hclass SubSystemOne;class SubSystemTwo;class SubSystemThree;class SubSystemFour;class Facade{public: Facade(); ~Facade(); void methodA(); void methodB(); void methodC();private: SubSystemOne *one; SubSystemTwo *two; SubSystemThree *three; SubSystemFour *four;};class SubSystemOne{public: void methodOne();};class SubSystemTwo{public: void methodTwo();};class SubSystemThree{public: void methodThree();};class SubSystemFour{public: void methodFour();};#endif //HEAD_H
Head.cpp
#include "Head.h"#include#include using namespace std;Facade::Facade(){ one = new SubSystemOne; two = new SubSystemTwo; three = new SubSystemThree; four = new SubSystemFour;}Facade::~Facade(){ delete one; delete two; delete three; delete four;}void Facade::methodA(){ cout << "方法组A" << endl; one->methodOne(); two->methodTwo(); three->methodThree();}void Facade::methodB(){ cout << "方法组B" << endl; three->methodThree(); four->methodFour(); two->methodTwo();}void Facade::methodC(){ cout << "方法组C" << endl; one->methodOne(); three->methodThree();}void SubSystemOne::methodOne(){ cout << "SubSystemOne::methodOne()" << endl;}void SubSystemTwo::methodTwo(){ cout << "SubSystemTwo::methodTwo()" << endl;}void SubSystemThree::methodThree(){ cout << "SubSystemThree::methodThree()" << endl;}void SubSystemFour::methodFour(){ cout << "SubSystemFour::methodFour()" << endl;}
main.cpp
#include "Head.h"#include#include using namespace std;int main(int *argc, int *argv[]){ Facade *facada = new Facade; facada->methodA(); cout << "---------------华丽分割线---------------"<< endl; facada->methodC(); cout << "---------------华丽分割线---------------"<< endl; facada->methodB(); delete facada; getchar(); return 0;}
转载地址:https://it1995.blog.csdn.net/article/details/92804847 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月26日 06时18分58秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Mybatis入门学习(三)
2019-04-30
Mybatis入门学习(四)
2019-04-30
Mybatis高级查询(一)
2019-04-30
Sublime text3删除当前行快捷键
2019-04-30
DOM事件处理方式
2019-04-30
快速激活Webstorm
2019-04-30
js 获取浏览器版本信息(全)
2019-04-30
JSONUtil工具类
2019-04-30
Spring 获取HttpServletRequest常用方法
2019-04-30
Mybatis 动态insert语句
2019-04-30
Mysql获取数据库的所有表,以及表所有字段信息
2019-04-30
bootstrap列表js
2019-04-30
bootstrap批量代码生成js
2019-04-30
Layer弹窗
2019-04-30
Mybatis批量保存
2019-04-30
Mybatis批量删除
2019-04-30
Nginx https支持配置
2019-04-30
springboot 整合jpa
2019-04-30