In this post, We will talk and learn about the Facade Design Pattern in Java.
- Facade design pattern is used to help the client applications; it doesn’t hide subsystem interfaces from the client. Whether to use Facade or not is completely dependent on the client application.
- We can apply Facade design pattern at any point of development when we find the number of interfaces grows and the system gets complex.
- In Facade Design Pattern subsystem interfaces are not aware of Facade and they should not have any reference to the Facade interface.
Now Let’s move towards the implementation of the Facade Design Pattern.
Below is the complete source code:
ExcelReport.java
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.kkjavatutorials.facade; import java.sql.Connection; /** * @author KK JavaTutorials * Implementor has to implement this interface * if willing to generate ExcelReport */ public interface ExcelReport { public abstract void generateExcelReport(Connection connection,String tableName); } |
HtmlReport.java
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.kkjavatutorials.facade; import java.sql.Connection; /** * @author KK JavaTutorials *Implementor has to implement this interface * if willing to generate HtmlReport */ public interface HtmlReport { public abstract void generateHtmlReport(Connection connection,String tableName); } |
PdfReport.java
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.kkjavatutorials.facade; import java.sql.Connection; /** * @author KK JavaTutorials *Implementor has to implement this interface * if willing to generate PdfReport */ public interface PdfReport { public abstract void generatePdfReport(Connection connection,String tableName); } |
ExcelReportImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.kkjavatutorials.facade.impl; import java.sql.Connection; import com.kkjavatutorials.facade.ExcelReport; /** * @author KK JavaTutorials * Generate Excel report */ public class ExcelReportImpl implements ExcelReport { @Override public void generateExcelReport(Connection connection, String tableName) { System.out.println("EXCEL report generation logic is here.."); } } |
HtmlReportImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.kkjavatutorials.facade.impl; import java.sql.Connection; import com.kkjavatutorials.facade.HtmlReport; /** * @author KK JavaTutorials *Generate HTML report */ public class HtmlReportImpl implements HtmlReport { @Override public void generateHtmlReport(Connection connection, String tableName) { System.out.println("HTML report generation logic is here.."); } } |
PdfReportImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.kkjavatutorials.facade.impl; import java.sql.Connection; import com.kkjavatutorials.facade.PdfReport; /** * @author KK JavaTutorials *Generate PDF report */ public class PdfReportImpl implements PdfReport { @Override public void generatePdfReport(Connection connection, String tableName) { System.out.println("PDF report generation logic is here.."); } } |
ReportFacade.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package com.kkjavatutorials.facade.impl; import java.sql.Connection; import com.kkjavatutorials.facade.ExcelReport; import com.kkjavatutorials.facade.HtmlReport; import com.kkjavatutorials.facade.PdfReport; /** * @author KK JavaTutorials *ReportFacade which provides Unified *Interface to generate different types *of report. */ public class ReportFacade { private PdfReport pdfReport; private HtmlReport htmlReport; private ExcelReport excelReport; public ReportFacade() { pdfReport = new PdfReportImpl(); htmlReport = new HtmlReportImpl(); excelReport = new ExcelReportImpl(); } public void generatePdfReport(Connection connection, String tableName) { pdfReport.generatePdfReport(connection, tableName); } public void generateHtmlReport(Connection connection, String tableName) { htmlReport.generateHtmlReport(connection, tableName); } public void generateExcelReport(Connection connection, String tableName) { excelReport.generateExcelReport(connection, tableName); } } |
ClientTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package com.kkjavatutorials.client; import java.sql.Connection; import com.kkjavatutorials.facade.ExcelReport; import com.kkjavatutorials.facade.HtmlReport; import com.kkjavatutorials.facade.PdfReport; import com.kkjavatutorials.facade.impl.ExcelReportImpl; import com.kkjavatutorials.facade.impl.HtmlReportImpl; import com.kkjavatutorials.facade.impl.PdfReportImpl; import com.kkjavatutorials.facade.impl.ReportFacade; /** * @author KK JavaTutorials *Client program which uses Facade pattern */ public class ClientTest { public static void main(String[] args) { Connection connection = null; String tableName = "employee_table"; //Without Using Facade Pattern PdfReport pdfReport = new PdfReportImpl(); pdfReport.generatePdfReport(connection, tableName); HtmlReport htmlReport = new HtmlReportImpl(); htmlReport.generateHtmlReport(connection, tableName); ExcelReport excelReport = new ExcelReportImpl(); excelReport.generateExcelReport(connection, tableName); System.out.println("---------------------------------------------------"); //With Using Facade Pattern ReportFacade reportFacade = new ReportFacade(); reportFacade.generatePdfReport(connection, tableName); reportFacade.generateHtmlReport(connection, tableName); reportFacade.generateExcelReport(connection, tableName); } } |
Output of this client Program:
PDF report generation logic is here..
HTML report generation logic is here..
EXCEL report generation logic is here..
—————————————————
PDF report generation logic is here..
HTML report generation logic is here..
EXCEL report generation logic is here..
You May Also Like:
Singleton Design Pattern in java
Prototype Pattern in Java
Factory Pattern in Java
Abstract Factory Pattern in Java
Builder Pattern in Java
Adapter Design Pattern in Java
Decorator Design Pattern in Java
That’s all about the Facade Design Pattern in Java.
If you have any feedback or suggestion please feel free to drop in below comment box.