[ACCEPTED]-Explain Facade pattern with c++ example?-facade
Facade pattern: provides a unified - simplified interface to a complex subsystem or set of interfaces. It provides a higher level interface simultaneously decoupling the client from the complex subsystem.
An example to help you understand .. a cab 38 driver. You tell the cab driver 'Take me 37 to PointX' (unified simplified high-level 36 interface) who then begins on a sequence 35 of actions (turns the key, changes gears, presses 34 the accelerator, etc...) to perform the 33 task. He abstracts away the complexity of 32 underlying subsystems (gearbox, engine, etc.) so 31 that you don't have to worry about them. The 30 driver also decouples you from the actual 29 vehicle used... you do not directly interface 28 with the car. You could potentially give 27 him a Merc but your interface to the Driver 26 would still be TakeMeTo( X ).. you're not 25 tied down to any specific model/make of 24 the car.
In a real world example, you'll 23 find facades where you interface with third 22 party components or libraries. You don't 21 want your code to depend on a specific vendor, so 20 you introduce a facade interface to decouple. Also 19 you'll simplify this interface, e.g. your 18 facade interface would have a method called 17 SendData( string ) but internally the implementation 16 may call n methods on m sub-packages in 15 a specific order to get the task done. This 14 is what the diagram on the wikipedia page 13 shows.
e.g. Translating an example to C++ and keeping 12 it tiny
sResource = LWCPPSimple::get("http://www.perl.org")
Here the fictitious Library For WWW 11 in C++ is a facade that unifies protocol, network 10 and parsing aspects of the problem so that 9 I can concentrate on my primary focus of 8 fetching the resource. The get method hides/encapsulates/keeps-in-one-place 7 the complexity (and in some cases ugliness) of 6 HTTP, FTP and other varied protocols, request-response, connection 5 management, etc. Also if tomorrow the creators 4 of LWCPPSimple find a way to make get() to 3 be twice as fast, I get the performance 2 benefits for free. My client code doesn't 1 have to change.
class Engine
{
public:
void Start() { }
};
class Headlights
{
public:
void TurnOn() { }
};
// That's your facade.
class Car
{
private:
Engine engine;
Headlights headlights;
public:
void TurnIgnitionKeyOn()
{
headlights.TurnOn();
engine.Start();
}
};
int Main(int argc, char *argv[])
{
// Consuming facade.
Car car;
car.TurnIgnitionKeyOn();
return 0;
}
0
I've done a search and replace on the C# example. This 4 might not help you, because if you understand 3 C++ then you should be able to understand 2 the C# as it uses the same constructs and 1 keywords (classes, functions, namespaces, public, etc)
// "Subsystem ClassA"
#include <iostream>
class SubSystemOne
{
public:
void MethodOne()
{
std::cout << " SubSystemOne Method" << std::endl;
}
}
// Subsystem ClassB"
class SubSystemTwo
{
public:
void MethodTwo()
{
std::cout << " SubSystemTwo Method" << std::endl;
}
}
// Subsystem ClassC"
class SubSystemThree
{
public:
void MethodThree()
{
std::cout << " SubSystemThree Method" << std::endl;
}
}
// Subsystem ClassD"
class SubSystemFour
{
public:
void MethodFour()
{
std::cout << " SubSystemFour Method" << std::endl;
}
}
// "Facade"
class Facade
{
SubSystemOne one;
SubSystemTwo two;
SubSystemThree three;
SubSystemFour four;
public:
Facade()
{
}
void MethodA()
{
std::cout << "\nMethodA() ---- " << std::endl;
one.MethodOne();
two.MethodTwo();
four.MethodFour();
}
void MethodB()
{
std::cout << "\nMethodB() ---- " << std::endl;
two.MethodTwo();
three.MethodThree();
}
}
int Main()
{
Facade facade = new Facade();
facade.MethodA();
facade.MethodB();
return 0;
}
In one sense, a Facade is just an API for 13 clients that wants to interact with something 12 hidden.
The Facade is useful when exposing 11 a simple C API for something that's implemented 10 in C++ or simply more complex than the API. Or 9 to get a fixed barrier between a client 8 and a library when the library needs to 7 go through numerous iterative updates and 6 you want to affect the client as little 5 as possible. For instance, if a C based 4 library needs to be updated internally to 3 C++ or something else, or just swapped for 2 something completely different, then the 1 Facade is a good middle-layer for the client.
class A {
private B b; // Class A uses Class B, the "interface"
public int f() { return b.g(); }
};
class B {
private C c; // class B uses class C, a "subsystem"
private ... ...; // other subsystems can be added
public int g() { c.h(); return c.i(); }
};
class C { // a subsystem
public void h() { ... }
public int i() { return x; }
};
Class A will not directly use any methods 4 or directly affect the state of class C 3 or any other subsystem that class B contains. Only 2 one subsystem is shown here because it doesn't 1 matter how many subsystems there are.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.