版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_38126105/article/details/84557060
外观模式:是将子系统的功能组合在一起,通过外观类,提供统一的接口。什么时候该使用外观模式呢?在设计初期,应该将软件分层,在层与层之间建立外观类,这样就可以为为复杂的新系统提供一个简单的接口。另一种情况是,在维护一个大型遗留系统时。可以使用外观模式,而将原来的系统当做一个子系统放进其中。
#include <iostream>
#include <memory>
using namespace std;
/*外观模式
* 为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口
* 使得这一子系统更加容易使用.外观类需要了解所有的子系统的功能,进行组合,以供外界
* 调用
*/
class CSystem1
{
public:
CSystem1() {}
void Off() { cout << "子系统1关闭" << endl; }
void On() { cout << "子系统1开启" << endl; }
};
class CSystem2
{
public:
CSystem2() {}
~CSystem2() {}
void Off() { cout << "子系统2关闭" << endl; }
void On() { cout << "子系统2开启" << endl; }
};
class CSystem3
{
public:
CSystem3() {}
~CSystem3() {}
void Off() { cout << "子系统3关闭" << endl; }
void On() { cout << "子系统3开启" << endl; }
};
//外观类
class CFacede
{
public:
CFacede():
m_PtrSystem1(new CSystem1()),m_PtrSystem2(new CSystem2()),m_PtrSystem3(new CSystem3())
{}
void run1() { m_PtrSystem1->Off(); m_PtrSystem2->Off(); m_PtrSystem3->Off(); }
void run2() { m_PtrSystem1->On(); m_PtrSystem2->Off(); m_PtrSystem3->On(); }
private:
std::shared_ptr<CSystem1> m_PtrSystem1;
std::shared_ptr<CSystem2> m_PtrSystem2;
std::shared_ptr<CSystem3> m_PtrSystem3;
};
int main()
{
std::unique_ptr<CFacede> PtrFacede = std::make_unique<CFacede>();
PtrFacede->run1();
PtrFacede->run2();
}