Tutorial

Facade Design Pattern in Java

Published on August 3, 2022
author

Pankaj

Facade Design Pattern in Java

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.

Facade Design Pattern

facade design pattern, facade pattern, facade design pattern in java 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.

Facade Design Pattern - Set of Interfaces

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
	}
	
}

Facade Design Pattern Interface

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;
	}
}

Facade Design Pattern Client Program

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.

Facade Design Pattern Important Points

  • Facade design 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.
  • Facade design pattern can be applied at any point of development, usually when the number of interfaces grow and system gets complex.
  • Subsystem interfaces are not aware of Facade and they shouldn’t have any reference of the Facade interface.
  • Facade design pattern should be applied for similar kind of interfaces, its purpose is to provide a single interface rather than multiple interfaces that does the similar kind of jobs.
  • We can use Factory pattern with Facade to provide better interface to client systems.

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.

Learn more about our products

About the authors
Default avatar
Pankaj

author

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.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
March 12, 2020

Hi Pankaj I am still not clear what is the basic difference between Facade Design Patten and Factory Design Pattern, Thank you

- Raveendra

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    September 8, 2019

    can u please place the git hub link for these code provided , looks like the you forgot !..

    - Rama krishna

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      August 12, 2019

      Interfaces are not used in code example but mentioned in the description? Can someone help me understand this?

      - ABHISHEK KUMAR

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        May 17, 2019

        Very useful article… Thanks

        - Alex

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          February 18, 2019

          Excellent information. Thanks for the great contribution

          - Rishikant Sharma

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            March 26, 2018

            I have been reading all your design patterns. Thanks for the explanation. It indeed helped me.

            - Prasanti

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              January 27, 2018

              Superbly described… Thanks Pankaj… Please post some more Java Tricky interview questions in future…

              - SUPRATIM BHATTACHARYA

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                September 4, 2017

                thank you, really clear and easy to understand

                - day

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  August 18, 2017

                  Looks like Facade and Factory design patterns are somewhat similar.

                  - DEBARATI MAJUMDER

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    July 2, 2017

                    very useful article

                    - Abhirag Kesarwani

                      Try DigitalOcean for free

                      Click below to sign up and get $200 of credit to try our products over 60 days!

                      Sign up

                      Join the Tech Talk
                      Success! Thank you! Please check your email for further details.

                      Please complete your information!

                      Become a contributor for community

                      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                      DigitalOcean Documentation

                      Full documentation for every DigitalOcean product.

                      Resources for startups and SMBs

                      The Wave has everything you need to know about building a business, from raising funding to marketing your product.

                      Get our newsletter

                      Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

                      New accounts only. By submitting your email you agree to our Privacy Policy

                      The developer cloud

                      Scale up as you grow — whether you're running one virtual machine or ten thousand.

                      Get started for free

                      Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

                      *This promotional offer applies to new accounts only.