# Facade Pattern

Most of the times you will find yourself structuring the codebase into multiple subsystems, each subsystem has a few modules, and of course when trying to consume a subsystem functionality in any place in your code you'll find that it needs a few complex calls and operations to the subsystem internal modules.

## Intent

Provide a simple, unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface (more complex body) that make the subsystems easier to use and consume.

## When to use

1. When you need to have a limited but straightforward interface to a complex subsystem.
2. When there are many dependencies between client code and the subsystem classes, so a facade is introduced to decouple the client from the subsystem.
3. When you want to layer your subsystems, a facade can be used as an entry point to each level of the subsystem.

## Structure

![](https://2149973296-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKKrm5lFe01Ro4BXbPp%2Fsync%2Fed5935c1768babb2d2f95cbae5c8a8f6f0fe27fb.png?generation=1603463564919468\&alt=media)

* **Facade:** knows the subsystem classes and what they're responsible for, and delegates the client requests to the appropriate objects in the appropriate order.
* **Subsystem Classes:** they're the classes that form the subsystem (either a pre-existing package, a 3rd-party library or an API).

## Note

Remember that Facade pattern can easily become a **god object** with a lot of tightly coupled dependencies.

## Examples

|                                                                          Source Code                                                                          |                                                                                                       UML                                                                                                       |
| :-----------------------------------------------------------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| [Example 1](https://github.com/khaled-hamam/ts-design-patterns/tree/369ec4e91fe87ca563f4d94388bceb028fa467bd/library/structural-patterns/facade/example_1.ts) | ![Figure 2](https://2149973296-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MKKrm5lFe01Ro4BXbPp%2Fsync%2F15e794c08037fbca8a3d44f08b4b3b09c81c9eef.png?generation=1603463564995364\&alt=media) |

You can find the tests [here](https://github.com/khaled-hamam/ts-design-patterns/tree/369ec4e91fe87ca563f4d94388bceb028fa467bd/library/structural-patterns/facade/index.test.ts).
