Facade Design Pattern in Java

by | Sep 25, 2020 | Java

If you look up the meaning of Facade, the work means the front of a building. Facade only represents the beautiful exterior with glass walls and fancy decorations. What it hides from an onlooker is the complex pipelining, electrical circuits, and drainage system. While these components are essential to make a beautiful house, it is of no use to a guest or visitor who is only concerned with the exterior. In JAVA as well, the Facade Design Pattern works similarly. It only creates a user-friendly interface and hides complex classes and libraries. A Facade class or interface can be easily recognized if it delegates services and requests to other classes while keeping itself clean.

Definition

The Facade design pattern in JAVA creates a simple and user-friendly interface over a complex series of subsystems and interfaces. The Facade interface is what a client interacts with as he/she is not concerned about what happens inside the program. This pattern also makes it very easy to edit subsystems without having to make any change to the facade interface.

Example and Code

Let us consider an online booking portal for this example. There are two kinds of rooms available to users. They will have to choose either from the tariff card.

//The first part will be the interface of the online portal
package structural.facade;
public interface BookingPortal {
public Room getRooms();
}
//Let us define the two room types
package structural.facade;
public class PremiumRoom implements BookingPortal {
public Room getRooms()
{
BookPremRoom obj1 = new BookPremRoom();
return obj1;
}


package structural.facade;
public class VIProom implements BookingPortal {
public Room getRooms()
{
BookVIP obj1 = new BookVIP();
return obj1;
}


// Now we will create a facade interface that will provide users a simple interface to book rooms of either type
package structural.facade;
public class RoomBooking
{
public PremiumRoom bookPRoom() {
PremiumRoom pr = new PremiumRoom();
BookPremRoom room = (room)pr.getRooms();
return room;
}
public VIProom bookVIPoom() {
VIProom vip = new VIProom();
BookVIP room = (room)vip.getRooms();
return room;
}

When to Use the Facade Design Pattern?

There are two situations when the Facade Design Pattern becomes very important.

  • You can use this pattern when you have developed a complex system but you want your client to also have access to it. As your client will not be able to understand the complexities and navigate through the code, you can create a facade interface that will interact with your client.
  • If the code you have developed is not compatible with the existing system in place, you can create a simpler and compatible facade interface that will act as a bridge between the two. You will no longer have to edit the entire code to make it compatible.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.