Facade模式通过提供一个接口给客户,从而隐藏系统的复杂性。客户可以通过该接口来访问系统。Facade定义一个高层的接口让子系统可以被容易的使用。例如让一个类的方法通过调用其他几个类来执行复杂的过程。
#include <iostream>
#include <string>
using namespace std;
class Alarm
{
public:
void alarmOn()
{
cout<<"Alarm is on and the house is secured"<<endl;
}
void alarmOff()
{
cout<<"Alarm is off and you can go into the house"<<endl;
}
};
class Ac
{
public:
void acOn()
{
cout<< "Ac is on "<<endl;
}
void acOff()
{
cout<<"Ac is off"<<endl;
}
};
class TV
{
public:
void tvOn()
{
cout<< "Tv is on"<<endl;
}
void tvoff()
{
cout<<"Tv is off"<<endl;
}
};
class HouseFacade
{
Alarm alarm;
Ac ac;
TV tv;
public:
HouseFacade(){}
void gotowork()
{
ac.acOff();
tv.tvoff();
alarm.alarmOn();
}
void comehome()
{
ac.acOn();
tv.tvOn();
alarm.alarmOff();
}
};
int main()
{
HouseFacade hf;
hf.gotowork();
hf.comehome();
return 0;
}