public interface ExportFactory { ExportFile factory(String type); }
public class ExportHtmlFactory implements ExportFactory{ @Override public ExportFile factory(String type) { // TODO Auto-generated method stub if("standard".equals(type)){ return new ExportStandardHtmlFile(); }else if("financial".equals(type)){ return new ExportFinancialHtmlFile(); }else{ throw new RuntimeException("没有找到对象"); } } }
public class ExportPdfFactory implements ExportFactory { @Override public ExportFile factory(String type) { // TODO Auto-generated method stub if("standard".equals(type)){ return new ExportStandardPdfFile(); }else if("financial".equals(type)){ return new ExportFinancialPdfFile(); }else{ throw new RuntimeException("没有找到对象"); } } }
public interface ExportFile { boolean export(String data); }
public class ExportFinancialHtmlFile implements ExportFile{ @Override public boolean export(String data) { // TODO Auto-generated method stub /** * 业务逻辑 */ System.out.println("导出财务版HTML文件"); return true; } }
public class ExportFinancialPdfFile implements ExportFile{ @Override public boolean export(String data) { // TODO Auto-generated method stub /** * 业务逻辑 */ System.out.println("导出财务版PDF文件"); return true; } }
public class ExportStandardHtmlFile implements ExportFile{ @Override public boolean export(String data) { // TODO Auto-generated method stub /** * 业务逻辑 */ System.out.println("导出标准HTML文件"); return true; } }
public class ExportStandardPdfFile implements ExportFile { @Override public boolean export(String data) { // TODO Auto-generated method stub /** * 业务逻辑 */ System.out.println("导出标准PDF文件"); return true; } }
public class Test000 { public static void main(String[] args) { // TODO Auto-generated method stub String data = ""; ExportFactory exportFactory = new ExportHtmlFactory(); ExportFile ef = exportFactory.factory("financial"); ef.export(data); } }
工厂方法模式的活动序列图
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:java 23种设计模式(二、工厂方法模式) - Python技术站