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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:RabbitMQ笔记-使用rabbitmq-c让生产者发送数据
下一篇:Spring Boot笔记-接收RabbitMQ队列中的消息

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月26日 06时18分58秒