Facade - shoonie/StudyingDesignPattern GitHub Wiki

Intent

Provide a unified interface to as set of interface in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

Diagram

disgram

Consequences

  1. It shields clients from subsystem components, thereby reducing the number of objects that clients deal with and making the subsystem easier to use.
  2. It promotes weak coupling between the subsystem and its clients.
  3. It doesn't prevent application from using subsystem classes if they need to.

Implementation

  1. Reducing client-subsystem coupling.
  2. Public versus private subsystem classes

Sample Code

//  code from http://www.bogotobogo.com/DesignPatterns/facade.php
#include <iostream>

// Subsystem 1
class SubSystemOne
{
public:
	void MethodOne() { std::cout << "SubSystem 1" << std::endl; };
};

// Subsystem 2
class SubSystemTwo
{
public:
	void MethodTwo() { std::cout << "SubSystem 2" << std::endl; };
};

// Subsystem 3 
class SubSystemThree
{
public:
	void MethodThree() { std::cout << "SubSystem 3" << std::endl; }
};


// Facade
class Facade
{
public:
	Facade()
	{
		pOne = new SubSystemOne();
		pTwo = new SubSystemTwo();
		pThree = new SubSystemThree();
	}

	void MethodA()
	{
		std::cout << "Facade::MethodA" << std::endl;
		pOne->MethodOne();
		pTwo->MethodTwo();
	}

	void MethodB()
	{
		std::cout << "Facade::MethodB" << std::endl;
		pTwo->MethodTwo();
		pThree->MethodThree();
	}
	~Facade()
	{
		delete pOne;
		delete pTwo;
		delete pThree;
	}
private:
	SubSystemOne * pOne;
	SubSystemTwo *pTwo;
	SubSystemThree *pThree;
};

int main()
{
	Facade *pFacade = new Facade();

	pFacade->MethodA();
	pFacade->MethodB();
	delete pFacade;
	return 0;
}
⚠️ **GitHub.com Fallback** ⚠️