Home [Design Patterns] Facade Pattern
Post
Cancel

[Design Patterns] Facade Pattern

Definition

The facade pattern provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use from the client’s point of view.

Scenario

We have many classes, each of which does some small tasks. We want to make them work together and provide some services. Instead of showing all those details, we hide them behind the facade and make users easy to use. This also follows the principle of least knowledge.

Principle of least knowledge: a piece of code (in a class) is only allowed to invoke the class’s methods, the instances’ method that are held or stored by the class, the parameters passed in, or the object instantiated by the class.

Hiding implementation details from the users and not coupling lots of classes together also helps us write better unit tests that are resistant to refactoring.

Class Diagram

Facade Pattern Facade Pattern

Used Cases

  • When writing a library, many components and services work together. We can use the facade pattern when creating the API. (ex: libyt)

Reference

This post is licensed under CC BY 4.0 by the author.