网站滑块验证怎么做,wordpress 获取首页,长沙高校网站制作公司,网站自己怎么做一、工厂方法模式1、生活场景系统常见的数据导出功能#xff1a;数据导出PDF、WORD等常见格式。2、工厂方法模式是类的创建模式#xff0c;又叫做虚拟构造子(Virtual Constructor)模式或者多态性工厂(Polymorphic Factory)模式。工厂方法模式的用意是定义一个创建产品对象的工…一、工厂方法模式1、生活场景系统常见的数据导出功能数据导出PDF、WORD等常见格式。2、工厂方法模式是类的创建模式又叫做虚拟构造子(Virtual Constructor)模式或者多态性工厂(Polymorphic Factory)模式。工厂方法模式的用意是定义一个创建产品对象的工厂接口将实际创建工作推迟到子类中。3、核心角色1)、抽象工厂角色这个角色的是工厂方法模式的核心任何在模式中创建对象的工厂类必须实现这个接口。在实际的系统中这个角色也常常使用抽象类实现。2)、具体工厂角色担任这个角色的是实现了抽象工厂接口的具体JAVA类。具体工厂角色含有与业务密切相关的逻辑并且受到使用者的调用以创建导出类。3)、抽象导出角色工厂方法模式所创建的对象的超类也就是所有导出类的共同父类或共同拥有的接口。在实际的系统中这个角色也常常使用抽象类实现。4)、具体导出角色这个角色实现了抽象导出角色所声明的接口工厂方法模式所创建的每一个对象都是某个具体导出角色的实例。4、代码UML关系图5、源代码实现// 客户端角色public class C01_FactoryMethod {public static void main(String[] args) {String data ;ExportFactory factory new ExportWordFactory () ;ExportFile exportWord factory.factory(user-word) ;exportWord.export(data) ;factory new ExportPdfFactory() ;ExportFile exportPdf factory.factory(log-pdf) ;exportPdf.export(data) ;}}// 抽象工厂角色interface ExportFactory {ExportFile factory (String type) ;}// 具体工厂角色class ExportWordFactory implements ExportFactory {Overridepublic ExportFile factory(String type) {if (user-word.equals(type)){return new ExportUserWordFile() ;} else if (log-word.equals(type)){return new ExportLogWordFile() ;} else {throw new RuntimeException(没有找到对象) ;}}}class ExportPdfFactory implements ExportFactory {Overridepublic ExportFile factory(String type) {if (user-pdf.equals(type)){return new ExportUserPdfFile() ;} else if (log-pdf.equals(type)){return new ExportLogPdfFile() ;} else {throw new RuntimeException(没有找到对象) ;}}}// 抽象导出角色interface ExportFile {boolean export (String data) ;}// 具体导出角色class ExportUserWordFile implements ExportFile {Overridepublic boolean export(String data) {System.out.println(导出用户Word文件);return true;}}class ExportLogWordFile implements ExportFile {Overridepublic boolean export(String data) {System.out.println(导出日志Word文件);return true;}}class ExportUserPdfFile implements ExportFile {Overridepublic boolean export(String data) {System.out.println(导出用户Pdf文件);return true;}}class ExportLogPdfFile implements ExportFile {Overridepublic boolean export(String data) {System.out.println(导出日志Pdf文件);return true;}}二、Spring框架中应用1、场景描述基于spring框架的配置实现如下流程汽车工厂根据不同的国家生产不同类型的汽车。2、核心工厂类public class ProductCar implements CarFactory {private Map carMap null;public ProductCar() {carMap new HashMap();carMap.put(china, new CarEntity(中国, 黑色,红旗));carMap.put(america, new CarEntity(美国, 白色,雪佛兰));}Overridepublic CarEntity getCar(String type) {return carMap.get(type);}}3、核心Xml配置文件4、测试类1)、代码块public class SpringTest {Testpublic void test01 (){ApplicationContext context01 new ClassPathXmlApplicationContext(/spring/spring-factorymethod.xml);CarEntity car1 (CarEntity)context01.getBean(car1) ;CarEntity car2 (CarEntity)context01.getBean(car2) ;System.out.println(car1);System.out.println(car2);}}2)、输出结果CarEntity{country中国, color黑色, name红旗}CarEntity{country美国, color白色, name雪佛兰}三、工厂方法小结工厂方法中把创建类的动作延迟就是通过对应的工厂来生成类的对象这种设计方式符合“开闭”原则。缺点就是当产品的种类过多的时候需要定义很多产品对应的工厂类。四、源代码地址GitHub·地址https://github.com/cicadasmile/model-arithmetic-parentGitEE·地址https://gitee.com/cicadasmile/model-arithmetic-parent