Facade模式

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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章