Skip to content

Latest commit

 

History

History
58 lines (39 loc) · 2.83 KB

File metadata and controls

58 lines (39 loc) · 2.83 KB

Intend

Provide a unified interface to a set of interfaces in a subsystem.

The Facade pattern defines a higher-level interface that makes the subsystem easier to use.

You should use the Facade pattern in the following cases :

  • You want to provide a simple interface to a complex system. This way, you will provide your clients with a default view of the subsystem that is enough for their needs.
  • You want to avoid tight coupling between your client and the objects of the subsystem.
  • You want to layer your subsystems.

How it's done

UML

Participants

  • Facade :
    • High-level interface that knows which subsystem is responsible for each possible requests.
    • Delegates all the hard-work to the appropriate subsystem object.
  • Subclass systems :
    • The classes that implement subsystem functionalities.
    • Do the work assigned by the Facade object.
    • Have no knowlegde about the Facade - They do not keep any reference to it.

Note : A Facade object can often be implemented as a Singleton since only 1 is needed in most cases.

How to implement

This is pretty straight-forward :)

  1. Declare and implement the interface of the Facade class.
  • Keep in mind this Facade should delegate the calls from the client to the appropriate objects in the subsystem.
  • It should be responsible for initializing the subsystem and managing its cycle of life (unless the Client already does that, which seems unlikely.)
  1. Make sure the client exclusively communicates with subsystem using the Facade.
  2. If the Facade grows too much, consider extracting part of its behaviour to a new RefinedFacade class.

Note : UML class diagram taken from here

Pros & cons

Pros

  • Decouples clients from subsystem : Clients are decoupled from the subsystem and work through the Facade object. They only know about the simple Facade interface and are independent of the complex subsystem (loose coupling).
  • Decouples subsystems

Cons

  • A facade can become a god object coupled to all classes of an app.
  • The client only gets to use what is defined in the Facade interface, so it might become uncapable of using the subsystems at their best if the Facade is poorly defined.

Notes

Here are some usefull ressources :