天天看點

C++ 設計模式 外觀模式 The Facade PatternC++ 設計模式 外觀模式 The Facade Pattern

C++ 設計模式 外觀模式 The Facade Pattern

介紹

Facade Pattern 為一組複雜的子系統提供了一個統一的簡單接口,它是一種結構型設計模式。

它隐藏了子系統的複雜性,并向用戶端提供了一個簡單的接口來通路子系統。通過使用 Facade 模式,用戶端可以友善地使用子系統,而不必了解子系統的内部細節。

Facade 模式由以下三個角色組成:

  1. Facade 類:它是一個Facade類,它定義了用戶端可以使用的簡單接口。
  2. Subsystem 類:它是一組複雜的子系統,它們完成了用戶端請求的具體工作。
  3. Client 類:它是一個用戶端類,它通過使用 Facade 類來請求子系統。

Facade 模式的優點包括:

  1. 減少了用戶端和子系統的耦合。
  2. 提高了子系統的獨立性。
  3. 提高了用戶端的可用性。
  4. 提高了系統的整體容錯性。

在 C++ 中,Facade 模式可以使用繼承群組合的方式實作。Facade 類可以繼承自 Subsystem 類,也可以包含一個 Subsystem 類的對象。在 C++ 中,Facade 模式常常用于提供簡單的接口來通路一組複雜的系統,進而簡化用戶端代碼。

示例

#include <iostream>

// Subsystem class
class SubsystemA
{
public:
    void operationA()
    {
        std::cout << "Subsystem A operation" << std::endl;
    }
};

class SubsystemB
{
public:
    void operationB()
    {
        std::cout << "Subsystem B operation" << std::endl;
    }
};

// Facade class
class Facade
{
private:
    SubsystemA *subA;
    SubsystemB *subB;

public:
    Facade()
    {
        subA = new SubsystemA();
        subB = new SubsystemB();
    }

    void operation()
    {
        std::cout << "Facade operation starts" << std::endl;
        subA->operationA();
        subB->operationB();
        std::cout << "Facade operation ends" << std::endl;
    }
};

// Client class
int main()
{
    Facade *facade = new Facade();
    facade->operation();
    return 0;
}
           
C++ 設計模式 外觀模式 The Facade PatternC++ 設計模式 外觀模式 The Facade Pattern

維基百科示例

C++ Programming: Code patterns design - Wikibooks, open books for an open world

/*Facade is one of the easiest patterns I think... And this is very simple example.

Imagine you set up a smart house where everything is on remote. So to turn the lights on you push lights on button - And same for TV,
AC, Alarm, Music, etc...

When you leave a house you would need to push a 100 buttons to make sure everything is off and are good to go which could be little 
annoying if you are lazy like me 
so I defined a Facade for leaving and coming back. (Facade functions represent buttons...) So when I come and leave I just make one 
call and it takes care of everything...
*/

#include <string>
#include <iostream>

using namespace std;

class Alarm
{
public:
	void alarmOn()
	{
		cout << "Alarm is on and 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()
	{
		alarm.alarmOff();
		ac.acOn();
		tv.tvOn();
	}
};

int main()
{
	HouseFacade hf;

	//Rather than calling 100 different on and off functions thanks to facade I only have 2 functions...
	hf.goToWork();
	hf.comeHome();
}
           
C++ 設計模式 外觀模式 The Facade PatternC++ 設計模式 外觀模式 The Facade Pattern

優缺點

Facade 模式的優缺點:

優點:

  1. 可以隐藏代碼的複雜度,對用戶端友好。
  2. 降低了錯誤使用的可能性。
  3. 可以友善地将複雜系統移植到其他平台,因為用戶端隻依賴于Facade。

缺點:

  1. Facade可能承擔太多的責任,最終變成 God Object 這種反設計模式。

God object - Wikipedia

繼續閱讀