Facade Design Pattern is easy to understand using its name. What does façade mean? In dictionary says the front of an attractive building or commonly saying the appearance of something or someone that is more pleasant or better than its actual.
Façade is used to hide or encapsulate the complexity of subsystems. A building design uses an attractive outer facade to hide the complexity of infrastructure. In the same way, the Facade Design Pattern creates a separate façade in front of the implementation.
This pattern comes under structural design patterns. One’s Windows booting shows a loading screen but it hides the process behind the customer care departments in some huge companies that are working as a façade. They know the functionalities in every department and guide the clients accordingly. Let’s move on to some simple examples to clarify this further.
Facade Design Pattern Real World Example
Let’s take a travel guide company. There are travel guides in the company and travel packages for travelers. Travelers just select their packages, but behind the scenes, there are a lot of arrangements for each package with hotels, busses, tickets, and so on.
According to example, travel guides work as façades. Let’s move on to the implementation and class diagram of the Facade Design Pattern.
Facade Design Pattern in Java
According to the diagram, clients do not want to think about arrangements just need to travel according to the package. The complexity of travel management hides and that’s not showing outside. Let’s develop Java code for the example
// Define the functionalities for the packages interface TrevelPackage{ public TrevelPackage getTrevelPackage(); } // implements each package by adding features class BudgetPackage implements TrevelPackage{ @Override public TrevelPackage getTrevelPackage() { System.out.println(" Budget Trevel Package"); return this; } } class GoldPackage implements TrevelPackage{ @Override public TrevelPackage getTrevelPackage() { System.out.println(" Gold Trevel Package"); return this; } } class LuxuryPackage implements TrevelPackage{ @Override public TrevelPackage getTrevelPackage() { System.out.println(" Luxury Trevel Package"); return this; } } // Guiders contact with each package and update with informations class TravelGuide { private BudgetPackage budgetPackage; private GoldPackage goldPackage; private LuxuryPackage luxuryPackage; public BudgetPackage getBudgetPackage() { budgetPackage =new BudgetPackage(); return budgetPackage; } public GoldPackage getGoldPackage() { goldPackage =new GoldPackage(); return goldPackage; } public LuxuryPackage getLuxuryPackage() { luxuryPackage = new LuxuryPackage(); return luxuryPackage; } } // Client looking for information throught the Guiders public class FacadeClient { public static void main(String[] args) { TravelGuide guide = new TravelGuide(); BudgetPackage budgetPackage=guide.getBudgetPackage(); budgetPackage.getTrevelPackage(); GoldPackage goldPackage=guide.getGoldPackage(); goldPackage.getTrevelPackage(); LuxuryPackage luxuryPackage=guide.getLuxuryPackage(); luxuryPackage.getTrevelPackage(); } }
Output
Budget Trevel Package Gold Trevel Package Luxury Trevel Package
Advantages of Facade Design Pattern
- Hide the complexity from a client using a façade.
- Loosely couple client and system.
- The client needs to use a small number of objects or object types to get a big service.
Summary
Facade Design Pattern is a common concept in the real world that receives and maps with programming. It hides the complexity and gives an attractive interface hiding actuality.
Design Patterns Book
Design Patterns are one of the most famous solutions in programming and millions of people follow them to fix their tasks, design projects, and so on. Most of the patterns are created based on basic OOP concepts (If your beginner then read our basic OOP, Encapsulation, Inheritance, Polymorphism, and Abstraction articles with great examples )
There are a lot of articles and design pattern books available with different examples to read. Other than our article if you are interested in this topic you could go through the below books as well.
Books | Description |
---|---|
Head First Design Patterns: A Brain-Friendly Guide | My favorite and it’s more readable. There are good examples and they look good to beginners as well. |
Design Patterns: Elements of Reusable Object-Oriented Software | There are 23 design patterns. showing how to select an appropriate pattern for your case. |
Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software | There are a lot of simple examples showing how to use design patterns in the correct way. Those are based on SOLIC principles. |
There are a lot of design pattern ebooks on Amazon. Those design patterns are written with great examples in a different programming language. I included a few of them according to their best ranking.