Facade

Intent

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

Motivation

To reduce complexity we break down a system into subsystems. However this increases the communication and dependencies between subsystems. One way to reduce this is to introduce a facade object that provides a single, simplified interface to the more general facilities of a subsystem

It glues together classes without hiding them completely

Applicability

Facade helps to decouple the client from the subsystem. Allows for subsystems to be independent

Use Facade when you want to layer subsystems. Each layer can talk to another through a facade

Subsystems lead to smaller classes that lend themselves to reuse and customization. But if a client does not want to customize, use a facade to provide the required functionality to client by gluing these classes

Key Objects

Facade Knows which subsystems are responsible for a request and delegates the request to them

Subsystem classes - implement subsystem functionality and handle work assigned by facade. They do not maintain a reference to the facade

Consequences

Promotes weak coupling between client and subsystems allowing you to vary components of subsystem without affecting the client

Does not prevent client from accessing subsystem classes if they need to

Shields clients from subsystems, reducing the number of objects that the client deals with

Implementation

Reducing client-subsystem coupling - Facade can be made into an abstract class with concrete subclasses to implement different subsystem functionality. Client can interact with subsystems through the interface of the abstract class

Public vc Private subsystem classes - If system allows it, segregate the subsystem classes into public and private classes. Public classes are available to client in addition to the facade, private classes are not

Applications

Service layer that talks to different DAOs?

In a UI framework have presentation classes responsible for the UI while the application facade talks to domain model and give the presentation classes data in a format that they can use