Design Pattern Download: The Facade Pattern
The Decorator pattern is a Structural Pattern used in software engineering when you want to add functionality to an existing object without the need to create a subclass.
Example
We have an E-commerce site and some controller service that runs behind our fronted. When a user orders an item we need to talk to 3 microservices: InventorySystem, PackagingSystem and ShippingSystem. Having to manage these three services and how they work and any other future microservices that may be needed can make the front end controller quite complex and difficult to understand. So we will create another Facade service called the OrderService which will orchestrate and call these other services for us so we only have to worry about the ends of the flow (providing the item and getting the tracking number).
When to use the Facade?
- When we want to provide a simplified interface for a complicated system or set of systems.
What is the Facade Pattern composed of?
- The Subsystems (the “under the hood” pieces that the facade interacts with so the client doesn’t have to)
- The Facade (the entry point the client uses to interface with the systems)
Implementation
The Subsystems
The Facade
Now in our front-end controller, we just need one OrderSystem instance which will handle the nitty gritty of all the steps needed to locate, package and ship an item.