Facade is a structural design pattern that provides a simplified (but limited) interface to a complex system of classes, library or framework.
While Facade decreases the overall complexity of the application, it also helps to move unwanted dependencies to one place.
Application of the pattern in PHP
Usage examples: The Facade pattern is commonly used in PHP applications, where the facade classes simplify the work with complex libraries or APIs.
Example: Structure of the Pattern
This example illustrates the structure of the Facade design pattern and focuses on following questions:
- What classes does it consists of?
- What roles do these classes play?
- In what way the elements of the pattern are related?
After learning about the pattern's structure it will be easier for you to grasp the following example, based on a real world PHP use case.
FacadeStructural.php: Structural Example
Example: Real World Example
Think of the Facade as a simplicity adapter for some complex subsystem. The Facade isolates complexity within a single class and allows other application code to use the straightforward interface.
In this example, the Facade hides the complexity of the YouTube API and FFmpeg library from the client code. Instead of working with dozens of classes, the client uses a simple method on the Facade.
FacadeRealWorld.php: Real world example