Java描述设计模式(03):工厂方法模式
程序员文章站
2022-06-21 09:32:32
一、工厂方法模式 1、生活场景 系统常见的数据导出功能:数据导出PDF、WORD等常见格式。 2、工厂方法模式 是类的创建模式,又叫做虚拟构造子(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 { @override public 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 { @override public 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 { @override public boolean export(string data) { system.out.println("导出用户word文件"); return true; } } class exportlogwordfile implements exportfile { @override public boolean export(string data) { system.out.println("导出日志word文件"); return true; } } class exportuserpdffile implements exportfile { @override public boolean export(string data) { system.out.println("导出用户pdf文件"); return true; } } class exportlogpdffile implements exportfile { @override public boolean export(string data) { system.out.println("导出日志pdf文件"); return true; } }
二、spring框架中应用
1、场景描述
基于spring框架的配置实现如下流程:汽车工厂根据不同的国家,生产不同类型的汽车。
2、核心工厂类
public class productcar implements carfactory { private map<string, carentity> carmap = null; public productcar() { carmap = new hashmap<>(); carmap.put("china", new carentity("中国", "黑色","红旗")); carmap.put("america", new carentity("美国", "白色","雪佛兰")); } @override public carentity getcar(string type) { return carmap.get(type); } }
3、核心xml配置文件
<bean id="productcarfactory" class="com.model.design.spring.node03.factorymethod.productcar" /> <bean id="car1" factory-bean="productcarfactory" factory-method="getcar"> <constructor-arg name="type" value="china" /> </bean> <bean id="car2" factory-bean="productcarfactory" factory-method="getcar"> <constructor-arg name="type" value="america" /> </bean>
4、测试类
1)、代码块
public class springtest { @test public 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-parent 码云地址:知了一笑 https://gitee.com/cicadasmile/model-arithmetic-parent