Facade Design pattern for Beginners

Omkar Kulkarni
Javarevisited
Published in
2 min readJan 10, 2021
Photo by Yunming Wang on Unsplash

Facade Design Pattern is one of the subtypes of Structural Design Pattern. It is used to expose minimal implementation to the client, that too in a simple way which he can be able to understand.

Facade provides an interface that the client can use to access the system. The business logic complexities can be hidden behind the interface.

Lets directly dive in and see how can we implement the Facade Design Pattern:

Consider a requirement where we have to generate different reports and the data for these reports is stored on different ORM’s like MYSQL and DB2. So here while generating the report we will have to first establish a connection and get the data from the DB. Once the data is populated we can send the data over to the method which generates the report.

Here we will create an interface and define the common methods that can be utilized all over.

public interface Reports {
public void getDataForReports(String reportType);
public void generateReport();
}

We will consider two types of Reports : Customer report and Orders report. The CustomerReport class would be defined like this.

public class CustomerReport implements Reports {

@Override
public void getDataForReports(String reportType) {
// BusinessLogic for getting the data for Customer Report
}

@Override
public void generateReport() {
// Business Logic for generating report;
}
}

And the OrdersReport would be defined like this.

public class OrderReports implements Reports{

@Override
public void getDataForReports(String reportType) {
// BusinessLogic for getting the data for Customer Report
}

@Override
public void generateReport() {
// Business Logic for generating report;
}
}

Now would come the ReportsGenerator class which would act as the facade. This class would hide the actual implementation of data fetching and generating the report.

public class ReportsGenerator {
public static String generateReport(String reportType) {
switch(reportType) {
case "Customer":
Reports customerReport = new CustomerReport();
customerReport.getDataForReports(reportType);
customerReport.generateReport();
return reportType + "generated successfully!";

case "OrderReport":
Reports orderReport = new OrderReports();
orderReport.getDataForReports(reportType);
orderReport.generateReport();
return reportType + "generated successfully!";
}
return "Invalid Report Type, Please select the correct one!";
}
}

Now lets design the ApiHandler class which would inturn call the generateReport() method of the ReportsGenerator class.

public class ApiHandler {
public static void main(String[] args) {
System.out.println(ReportsGenerator.generateReport("Customer"));
System.out.println(ReportsGenerator.generateReport("Order"));
}
}

When to use the Facade Design Pattern?

Facade should be used when we have a complex business logic in place and we have to expose only the simplified apis to the customer.

Advantages of Facade Desing Pattern :

  • We can decouple the complex system logic from the clients.
  • Wrapping the complex system logic is achieved using the Facade Design pattern.

Disadvantages of Facade Design Pattern :

  • The Facade class might become the super class in long run.
  • Extra level of abstraction gets added because of Facade.

--

--