서브시스템을 더 쉽게 사용할 수 있도록 higher-level 인터페이스를 정의하고, 제공하는 패턴
단순화된 인터페이스를 통해서 서브시스템을 더 쉽게 사용할 수 있도록 하기위한 용도로 쓰인다.
DataBase Connection을 통해 HTML 리포트 또는 PDF 리포트를 생성
MySqlHelper.class
public class MySqlHelper {
public static Connection getMySqlDBConnection(){
// 실제 커넥션을 리턴해야 하지만, 예제이기에 null 을 리턴하겠습니다.
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
}
}
OracleHelper.class
public class OracleHelper {
public static Connection getOracleDBConnection(){
// 실제 커넥션을 리턴해야 하지만, 예제이기에 null 을 리턴하겠습니다.
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
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;
}
}
Main.class
public static void main(String[] args) {
String tableName="Test";
//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);
}
참조: