Design Patterns: Facade
Updated: Jul 12, 2021
As always here is the link to the GitHub repo :)
If this post has helped you feel free to like, comment and share.
This post, is all about the Facade pattern. When/ how to use it.
First off this pattern is all about moving unwanted dependencies into one place. It's great when you are working with large libraries and need to keep objects separated. Below is a diagram that show how it works:
As you can see the Facade acts as a wrapper for the sub-systems. It's a one stop shop for the additional functionality.
For our use case, we will be making a billing system. Where the Facade is responsible for calling the various different sub-systems in order.
Sub-Systems (BillingSystem, BankingSystem): These are our sub systems, essentially they will be getting called by our Facade class. They are responsible for the majority of the functionality.
Facade: This class will need to hold references to the sub-systems and call them in order. Essentially... it's a wrapper.
This is an example of how this pattern can be implemented. Notice how the Facade is the only object to get it's method called, and not the sub systems.
For me this was one of those patterns that you will come across and use, without knowing what it is called. It's a common approach for wrapping a bunch of dependencies inside of an object and then only passing around the Facade.
I hope that this has helped you to understand the Facade pattern. See you next time.