Problem

A complex system needs a public interface. The inner workings of that system should not be available to every other system.

Solution

Provide a simplified public interface for the use by other systems. Wrap the whole complicated thing in that interface.

Related Patterns

Discussion

Providing the interface lowers the learning curve required to successfully utilize our system by other clients. It also promotes decoupling from other potential clients. On the other hand, it could limit the features and flexibility available to "power users".

Examples

A website can be a Facade to a more complicated subsystem. For example, databases can be accessed and updated from the web, a common task accomplished by server-side scripting. The user only interacts with the public website, which hides the inner complexity of the actual system.

Code

Here, FooFacade is a Facade for Foo and Bar. When we call foo.start() from main, the internals of the Facade are also initialized.

class Foo{
public:
  quuz(){cout << "quuz" << endl;}
  quux(){cout << "quux" << endl;}
}
class Bar{
public:
  baz(){cout << "baz" << endl;}
  qux(){cout << "qux" << endl;}
}

class FooFacade{
private:
  Foo foo; Bar bar;
public:
  FooFacade(){
    this.foo = new Foo();
    this.bar = new Bar();
  }
  start(){
    foo.quuz();
    foo.quux();
    bar.baz();
    bar.qux();
  }
}

int main(){
  FooFacade foo = new FooFacade();
  foo.start();
}
// output:
quuz
quux
baz
qux