Facade | Cheat Sheet

Structural Pattern — Design Patterns Series

Itchimonji
CP Massive Programming
4 min readJun 25, 2021

--

Facade is a very helpful pattern to decouple subsystems or dependent libraries from the client to simplify the use case or context. It is also good to separate different layers from each other in order to create comprehensible software architecture. This way, client and subsystem can be created independently.

Real life analogies are a door of an industrial plant or a support employee of an ordering company.

Real-life examples

  • Door of an industrial plant
  • Support employee of an ordering company
  • Gateway to a subsystem
  • Layer separating database and middleware

Meaning

  • Provision of an interface to a subsystem
  • Defines a higher level interface to simplify the use of a subsystem

Applicability (suitable if …)

  • A simple interface to a complex subsystem is supposed to be provided — such subsystems can become increasingly complicated in the course of the development process
  • The client is supposed to be decoupled from a subsystem (favors independence and portability of the subsystem)
  • Communication of a subsystem to the client or other subsystems is to take place only via an interface

Assets and Drawbacks

  • Shielding the client from the subsystem components — Minimizing the number of objects that need to be managed by the client
  • Loose coupling between client and subsystem; thus, components in the subsystem can be easily exchanged without the client being aware of it
  • Removal of circular dependencies
  • Independent implementation of client and subsystem (less compilation effort)

Related Patterns

  • Is very often used in combination with as well as an alternative to Abstract Factory
  • Is often confused with Mediator, or a combination of both patterns is chosen
  • Facade is mostly unique and therefore a Singleton

Example

Facades are often used to encapsulate subsystems. In this example I use the same approach to hide the production process of coffee.

At first, you can create a trader model that ship the coffee from Brazil to the target country.

Trader model

Next, you can implement a model for a coffee mill to grind the coffee beans.

Mill model

For a drink, you need to cook the coffee.

Drink model

The facade manages the coffee production process and provides only an interface to the client for getting the result.

Facade model

The client only needs to call the produce() function and does not (need to) know all the other models. The client is strongly decoupled from the subsystem.

Client

Conclusion

Design Patterns are an important resource and base knowledge for every developer — they are very helpful for solving programmatic problems, help with consistent communication with other developers about system design, and serve as a significant introduction into object composition (besides inheritance) and dependency inversion.

Structural Patterns are indispensable when objects are combined to create new functionalities. Comprehensive structures are built by using compositions of classes and objects. This provides flexibility due to variable composition structure depending on the runtime. One thus achieves an increase in efficiency and consistency.

--

--

Itchimonji
CP Massive Programming

Freelancer | Site Reliability Engineer (DevOps) / Kubernetes (CKAD) | Full Stack Software Engineer | https://patrick-eichler.com/links