Facade Design Pattern is one of the Structural design patterns (such as Adapter pattern and Decorator pattern). Facade design pattern is used to help client applications to easily interact with the system.
According to GoF Facade design pattern is:
Provide a unified interface to a set of interfaces in a subsystem. Facade Pattern defines a higher-level interface that makes the subsystem easier to use.
Suppose we have an application with set of interfaces to use MySql/Oracle database and to generate different types of reports, such as HTML report, PDF report etc. So we will have different set of interfaces to work with different types of database. Now a client application can use these interfaces to get the required database connection and generate reports. But when the complexity increases or the interface behavior names are confusing, client application will find it difficult to manage it. So we can apply Facade design pattern here and provide a wrapper interface on top of the existing interface to help client application.
We can have two helper interfaces, namely MySqlHelper
and OracleHelper
.
package com.journaldev.design.facade;
import java.sql.Connection;
public class MySqlHelper {
public static Connection getMySqlDBConnection(){
//get MySql DB connection using connection parameters
return null;
}
public void generateMySqlPDFReport(String tableName, Connection con){
//get data from table and generate pdf report
}
public void generateMySqlHTMLReport(String tableName, Connection con){
//get data from table and generate pdf report
}
}
package com.journaldev.design.facade;
import java.sql.Connection;
public class OracleHelper {
public static Connection getOracleDBConnection(){
//get Oracle DB connection using connection parameters
return null;
}
public void generateOraclePDFReport(String tableName, Connection con){
//get data from table and generate pdf report
}
public void generateOracleHTMLReport(String tableName, Connection con){
//get data from table and generate pdf report
}
}
We can create a Facade pattern interface like below. Notice the use of Java Enum for type safety.
package com.journaldev.design.facade;
import java.sql.Connection;
public class HelperFacade {
public static void generateReport(DBTypes dbType, ReportTypes reportType, String tableName){
Connection con = null;
switch (dbType){
case MYSQL:
con = MySqlHelper.getMySqlDBConnection();
MySqlHelper mySqlHelper = new MySqlHelper();
switch(reportType){
case HTML:
mySqlHelper.generateMySqlHTMLReport(tableName, con);
break;
case PDF:
mySqlHelper.generateMySqlPDFReport(tableName, con);
break;
}
break;
case ORACLE:
con = OracleHelper.getOracleDBConnection();
OracleHelper oracleHelper = new OracleHelper();
switch(reportType){
case HTML:
oracleHelper.generateOracleHTMLReport(tableName, con);
break;
case PDF:
oracleHelper.generateOraclePDFReport(tableName, con);
break;
}
break;
}
}
public static enum DBTypes{
MYSQL,ORACLE;
}
public static enum ReportTypes{
HTML,PDF;
}
}
Now lets see client code without using Facade pattern and using Facade pattern interface.
package com.journaldev.design.test;
import java.sql.Connection;
import com.journaldev.design.facade.HelperFacade;
import com.journaldev.design.facade.MySqlHelper;
import com.journaldev.design.facade.OracleHelper;
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);
}
}
As you can see that using Facade pattern interface is a lot easier and cleaner way to avoid having a lot of logic at client side. JDBC Driver Manager class to get the database connection is a wonderful example of facade design pattern.
Thats all for Facade design pattern, stay tuned for more design pattern articles. :)
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Very informative. Thank you…
- Swetha
excellent article. simple and subtle description of the design pattern. Thank you.
- Gopinath
“Facade pattern is more like a helper for client applications, it doesn’t hide subsystem interfaces from the client. Whether to use Facade or not is completely dependent on client code”. That’s a very good reason to use a facade sometimes: to hide subsystems interfaces.
- You Know
The first time I understand this pattern :) Thank you. Also please fix the comment for the Oracle helper class. It looks like you first created the MySQL helper class and copied it with a new name. What I’m trying to say is that the “getOracleDBConnection” method has a misleading comment.
- Muhammad Gelbana
You have not used any interface here. Is the definition satisfied ? “Facade Pattern defines a higher-level interface that makes the subsystem easier to use” —
- Sachin Tailor
Hi Pankaj, Indeed a very interesting article, pardon me for asking, since im lack of skill in the design pattern, but i notice your HelperFacade, is kinda acting like a Factory Pattern that calls the Facades(MySql,Oracle,etc). So in this article, you were explaining both design pattern in one post, fantastic :) Am i correct?
- Ronald
simple and clear explanations. thanks.
- Hafeez
Why do you return null, return a connection obj, will look better
- Nikhil
very useful article
- Abhirag Kesarwani
Looks like Facade and Factory design patterns are somewhat similar.
- DEBARATI MAJUMDER