Facade design pattern with real time example

Facade design pattern:

A facade design pattern is easy to implement. it does not add any additional functionalities like a decorator pattern. As the name suggests, the facade is the face of the building. Sometimes you want to hide facade-design-pattern-java-real-time-examplethe complex functionality from the external world. it is achieved through user interfaces. A real-time example is we know how to operate an android mobile. but we don’t know how things are implemented in the background. through the interface, we can hide the implementation details but provide necessary details to the user.
Our application might have loads of complex code. facade objects make it easier for clients to operate or use complicated code.
Advantages of using facade design pattern:
1. we can easily use the complex code
2. loose coupling achieved through facade design patterns
3. facade is optional in your system.
Real-time example for java facade design pattern: create a Gmail account. If you want to create a Gmail account, we will enter the all required details, click on submit. It is an easy process for users. but there are different validation processes that will be running in the background. background processes are email duplicate checking, password validation, birthday validation, storing the details into the database, etc. if you see in the back end, there is a heavy load and complicated subsystem. Instead of showing background process implementations, they have given a simple and easy process for creating accounts.
Facade design is one of the easiest patterns to implement. we need to create a class with simplified methods that are required by the client. newly created class delegates responsibilities to existing system classes. clients can communicate with subsystem classes through facade classes. it acts as a mediator between client and subsystem classes.  clients no need to have access to subsystem classes. subsystem classes can be private to hide from external systems.

real-time example for java facade design pattern:

class FlightBooking //sub system class 1
{ 
private String ongoingDate;
private String returnDate;
public FlightBooking(String ongoingDate, String returnDate) {
super();
this.ongoingDate = ongoingDate;
this.returnDate = returnDate;
}
public void printFlightTicket()
{
System.out.println("Journey will start on "+ongoingDate);
System.out.println("return on "+returnDate);
}
}
class HotelBooking //sub system class 1
{
private String checkinDate;
private String checkoutDate;

public HotelBooking(String checkinDate, String checkoutDate) {
super();
this.checkinDate = checkinDate;
this.checkoutDate = checkoutDate;
}

public void printHotelBookings()
{
System.out.println("Hotel checkin date: "+checkinDate);
System.out.println("Hotel checkin date: "+checkoutDate);
}

}
class Facade
{
private String journeyBegindate;
private String journeyEndDate;
public Facade(String journeyBegindate, String journeyEndDate) {
super();
this.journeyBegindate = journeyBegindate;
this.journeyEndDate = journeyEndDate;
}
public void printJourneyTrip()
{
FlightBooking flight=new FlightBooking(journeyBegindate, journeyEndDate);
flight.printFlightTicket();
HotelBooking hotel= new HotelBooking(journeyBegindate, journeyEndDate);
hotel.printHotelBookings();
}
}

public class FacadePatternDemo { //client class
public static void main(String[] args) {
Facade facadeObj=new Facade("20/dec/2020", "31/dec/2020");
facadeObj.printJourneyTrip();
}
}


Hope you are understanding with the example of the facade pattern. thanks 🙂

 


Posted

in

by

Tags:

Comments

Leave a comment

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

Design a site like this with WordPress.com
Get started