c++设计模式(外观模式)

继续,今天是外观模式

外观模式(《大话设计模式》):

  • 为子系统中的一组接口提供一个一致 的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
  • 在设计初期,应该有意识的将不同的两个层分离,如:经典的三层结构,需要考虑在数据访问层和业务逻辑层,业务逻辑层和表示层的层与层之间建立外观Faced
  • 在开发阶段,子系统往往因为不断的重构演化而变得越来越复杂,外部调用越来越困难,增加外观可以提供一个简单的接口,减少他们之间的依赖
  • 在维护一个遗留的大型系统时,可能这个系统已经非常难以维护和扩展了,为新系统开发一个外观类,来提供设计粗糙或高度复杂的遗留代码的比较清晰的简单的接口,让新系统与外观类对象交互 ,外观类对象与遗留代码交互所有复杂的工作

简单示例:

#include <iostream>
#include <string>
using namespace std;
//外观模式示例

class subSystemOne
{
public:
	subSystemOne() {}
	~subSystemOne() {}
	void methodOne()
	{
		cout << "子系统方法一" << endl;
	}
};
class subSystemTwo
{
public:
	subSystemTwo() {}
	~subSystemTwo() {}
	void methodTwo()
	{
		cout << "子系统方法二" << endl;
	}
};
class subSystemThree
{
public:
	subSystemThree() {}
	~subSystemThree() {}
	void methodThree()
	{
		cout << "子系统方法三" << endl;
	}
};
//外观类

class Faced
{
private:
	subSystemOne* one;
	subSystemTwo* two;
	subSystemThree* three;
public:
	Faced() 
	{
		one = new subSystemOne();
		two = new subSystemTwo();
		three = new subSystemThree();
	}
	~Faced() 
	{
		delete one;
		delete two;
		delete three;
	}

	void MethodA()
	{
		cout << "方法组合A" << endl;
		one->methodOne();
		two->methodTwo();
		three->methodThree();
	}
	void MethodB()
	{
		cout << "方法组合B" << endl;
		two->methodTwo();
		one->methodOne();
		three->methodThree();
	}

};
int main()
{
	
	Faced* f = new Faced();
	f->MethodA();
	f->MethodB();

	system("pause");
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值