当前位置: 代码迷 >> 综合 >> Facade Design Pattern
  详细解决方案

Facade Design Pattern

热度:61   发布时间:2024-01-14 13:22:06.0

何为Facade模式

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.

Facade(外观)模式为一系列子系统接口提供了一个统一的接口,此模式提供了一个在子系统之上的“层”,使得子系统更容易使用。

Facade模式的结构


在上述结构的外观模式Facade类隔离客户端的子系统。客户端只与外观类交互,而无需了解子系统类。

例子   (java)

Set of  Interface(建立两个子系统Package1和Package2,分别叫做MySql和Oracle.)
Mysql.java
import java.sql.Connection;public class MySql {public static Connection getMySqlDBConnection(){//get MySql DB connection using connection parametersreturn 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}
}
Oracle.java
import java.sql.Connection;public class Oracle {public static Connection getOracleDBConnection(){//get MySql DB connection using connection parametersreturn 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 Interface(创建Facade类,叫做facade.)
Facade.java
public class Facade {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;<span style="white-space:pre">	</span>//use enum for safety}public static enum ReportTypes{HTML,PDF;}
}

Client program(以下测试程序分别使用facade接口和不使用facade接口)
Test.java
import java.sql.Connection;public class Test {public static void main(String[] args) {String tableName="Employee";//generating MySql HTML report and Oracle PDF report without using FacadeConnection 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 FacadeHelperFacade.generateReport(HelperFacade.DBTypes.MYSQL, HelperFacade.ReportTypes.HTML, tableName);HelperFacade.generateReport(HelperFacade.DBTypes.ORACLE, HelperFacade.ReportTypes.PDF, tableName);}}
使用facade接口可以避免大量的逻辑在客户端,JDBC驱动程序管理类获取数据的连接方式是facade模式一个很好地例子。
  相关解决方案