一.外观设计模式所解决的问题
外观设计模式为子系统中的一组接口提供统一的接口,这种统一的接口屏蔽了直接调用子系统时的逻辑关系,使得调用子系统时更容易。
二.外观设计模式用例
假如一个应用中有两个接口MysqlHelper.class和OracleHelper.class,两个接口功能分别是连接mysql和oracle数据库,然后生成HTML报表或者PDF报表,代码说明如下:
MysqlHelper.class和OracleHelper.class
import java.sql.Connection; 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 } } 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 } }
外观类HelperFacade.class
import java.sql.Connectionclass HelperFacade{ public static void generateReport(DatabaseType dbType,ReportType 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=MySqlHelper.getMySqlDBConnection(); OracleHelper oracleHelper=new OracleHelper(); switch (reportType){ case HTML: oracleHelper.generateOracleHTMLReport(tableName,con); break; case PDF: oracleHelper.generateOraclePDFReport(tableName,con); break; } } } public static enum DatabaseType{ MYSQL,ORACLE } public static enum ReportType{ HTML,PDF } }
直接调用子接口和调用统一接口(外观类)对比:
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); } }
三.外观设计模式使用场景
1.外观设计模式可以运用在任何开发阶段,通常用在接口数量越来越多、系统变得复杂的时候。
2.子接口不应该有外观类的引用。
3.子接口提供的功能应该相似,因为外观接口就是通过包装子接口来提供一个接口来做一些相似的功能。
原文地址:https://www.cnblogs.com/quxiangxiangtiange/p/10247304.html
时间: 2024-10-02 14:05:00