Facade Design Pattern in Java with Practical Example
1. What is design pattern.
A design pattern is a proven, reusable solution to a commonly occurring software design problem. It represents best practices developed by experienced software developers to solve recurring problems in object‑oriented design.
2.What is Facade Design Pattern.
The Facade Design Pattern is a structural design pattern that provides a simple interface to a complex system. It hides the complexity of multiple classes or subsystems and gives the client a single, easy-to-use interface.
3. Why Do We Need Facade Pattern?
Problem Statement:
In large applications:
- There are many classes and subsystems
- Clients need to interact with multiple objects
- Code becomes tightly coupled and complex
Façade solves:
- Complexity
- Tight coupling
- Difficult maintenance
4. Key Components of Facade Pattern
1. Facade Class
- Provides simplified interface
- Delegates calls to subsystems
2. Subsystem Classes
- Perform actual work
- Complex logic
3. Client
- Uses facade instead of directly interacting with subsystems
Facade Design pattern Example
Imagine an application that contains multiple interfaces for connecting to databases like SQL and Oracle, along with separate interfaces for generating reports such as HTML and PDF reports. The client application needs to interact with these interfaces to establish database connections and create reports as required. However, as the number of interfaces grows and their functionalities become more complex or difficult to understand, managing them directly becomes challenging for the client application.
- SqlHelper.java
package com.test.rkdigital.school.facade.design;
public class SqlHelper
{
public static Connection getSqlDBConnection(){
//get MySql DB connection using connection parameters
return null;
}
public void generateSqlPDFReport(String tableName, Connection con){
//get data from table and generate pdf report
}
public void generateSqlHTMLReport(String tableName, Connection con){
//get data from table and generate pdf report
}
}
2.Oracle10Helper.java
package com.test.rkdigital.school.facade.design;
public class Oracle10Helper {
public static Connection getOracle10DBConnection(){
//get MySql DB connection using connection parameters
return null;
}
public void generateOracle10PDFReport(String tableName, Connection con){
//get data from table and generate pdf report
}
public void generateOracle10HTMLReport(String tableName, Connection con){
}
}
3. FacadeHelper.java
package com.test.rkdigital.school.facade.design;
public class HelperFacade {
public static void generateReport(DBTypes dbType, ReportTypes
reportType, String tableName){
Connection con = null;
switch (dbType){
case MYSQL:
con = SqlHelper.getSqlDBConnection();
SqlHelper SqlHelper = new SqlHelper();
switch(reportType){
case HTML:
SqlHelper.generateMySqlHTMLReport(tableName, con);
break;
case PDF:
SqlHelper.generateMySqlPDFReport(tableName, con);
break;
}
break;
case ORACLE:
con = Oracle10Helper.getOracle10DBConnection();
Oracle10Helper oracleHelper = new OracleHelper();
switch(reportType){
case HTML:
oracleHelper.generateOracle10HTMLReport(tableName, con);
break;
case PDF:
oracleHelper.generateOracle10PDFReport(tableName, con);
break;
}
break;
}
}
public static enum DBTypes{
MYSQL,ORACLE;
}
public static enum ReportTypes{
HTML,PDF;
}
}
4. FacadePatternTest.java
public class FacadePatternTest {
public static void main(String[] args) {
String tableName="Employee";
//generating MySql HTML report and Oracle PDF report without
using Facade
Connection con = MySqlHelper.getMySqlDBConnection();
MySqlHelper mySqlHelper = new MySqlHelper();
mySqlHelper.generateMySqlHTMLReport(tableName, con);
Connection con1 = OracleHelper.getOracleDBConnection();
OracleHelper oracleHelper = new OracleHelper();
oracleHelper.generateOraclePDFReport(tableName, con1);
//generating MySql HTML report and Oracle PDF report using
Facade
HelperFacade.generateReport(HelperFacade.DBTypes.MYSQL,
HelperFacade.ReportTypes.HTML, tableName);
HelperFacade.generateReport(HelperFacade.DBTypes.ORACLE,
HelperFacade.ReportTypes.PDF, tableName);
}
}
Facade design pattern Interview Questions
1. Why do we need the Facade Design Pattern?
Answer:
In large applications, clients may need to interact with multiple classes to perform a single task. Facade pattern simplifies this by providing one unified interface.
Example:
Instead of interacting separately with database classes, report generators, and validation classes, the client can simply call one facade class.
2. What problem does Facade Pattern solve?
Answer:
It solves the problem of:
- Tight coupling between client and subsystem classes
- Complex code interaction
- Difficult maintenance
- Repetitive client logic
3. Is Facade Pattern a Creational, Structural, or Behavioral pattern?
Answer:
Facade belongs to the Structural Design Pattern category because it organizes relationships between classes.
4. What are the main components of Facade Pattern?
Answer:
- Client → Uses facade
- Facade Class → Simplified interface
- Subsystem Classes → Complex internal classes
5. Can Facade Pattern reduce code complexity?
Answer:
Yes, it reduces complexity by hiding unnecessary subsystem details from clients.
6. What are real-world examples of Facade Pattern?
Answer:
- Online shopping checkout system
- Banking systems
- Travel booking platforms
- Home theater system controller
Example: A single “Book Trip” button handles hotel, flight, and cab booking internally.
7. Does Facade Pattern violate the Open/Closed Principle?
Answer:
No, if designed properly. New subsystem features can be added without modifying client code.
8. Can multiple facades exist in one system?
Answer:
Yes, different facades can be created for different modules.
Example:
- PaymentFacade
- OrderFacade
- NotificationFacade
9. What are the advantages of Facade Pattern?
Answer:
- Reduces complexity
- Improves readability
- Reduces dependency on subsystem classes
- Easier maintenance
- Better abstraction
10. What are the disadvantages of Facade Pattern?
Answer:
- Can become a God class if too many responsibilities are added
- May hide useful subsystem functionality
11. Where is Facade Pattern used in Java?
Answer:
Examples include:
- Spring Framework
- Hibernate
- JavaMail API
These frameworks often provide simplified APIs over complex internal operations.