Java描述设计模式(03):工厂方法模式
本文源码:GitHub·点这里 || GitEE·点这里
一、工厂方法模式
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
GitEE·地址
https://gitee.com/cicadasmile/model-arithmetic-parent

Java描述设计模式(03):工厂方法模式的更多相关文章
- 设计模式-03工厂方法模式(Factory Method Pattern)
插曲.简单工厂模式(Simple Factory Pattern) 介绍工厂方法模式之前,先来做一个铺垫,了解一下简单工厂模式,它不属于 GoF 的 23 种经典设计模式,它的缺点是增加新产品时会违背 ...
- Java设计模式之工厂方法模式(转) 实现是抽象工厂?
Java设计模式之工厂方法模式 责任编辑:覃里作者:Java研究组织 2009-02-25 来源:IT168网站 文本Tag: 设计模式 Java [IT168 技术文章] ...
- C#设计模式(3)——工厂方法模式
一.概念:定义一个用于创建对象的接口,让子类决定实例化哪一个类,工厂方法使一个类的实例化延迟到其子类. 二.代码实现 namespace 设计模式之工厂方法模式 { /// <summary&g ...
- 乐在其中设计模式(C#) - 工厂方法模式(Factory Method Pattern)
原文:乐在其中设计模式(C#) - 工厂方法模式(Factory Method Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 工厂方法模式(Factory Method Pa ...
- C#设计模式(3)——工厂方法模式(转)
C#设计模式(3)——工厂方法模式 一.引言 在简单工厂模式中讲到简单工厂模式的缺点,有一点是——简单工厂模式系统难以扩展,一旦添加新产品就不得不修改简单工厂方法,这样就会造成简单工厂的实现逻辑过 ...
- php设计模式之工厂方法模式
php设计模式之工厂方法模式 工厂方法模式 工厂方法模式(Factory Method Pattern)又称为工厂模式,也叫虚拟构造器(Virtual Constructor)模式或者多态工厂(Pol ...
- java 23 - 1 设计模式之工厂方法模式
转载: JAVA设计模式之工厂模式(简单工厂模式+工厂方法模式)
- JAVA设计模式之工厂方法模式
在阎宏博士的<JAVA与模式>一书中开头是这样描述工厂方法模式的: 工厂方法模式是类的创建模式,又叫做虚拟构造子(Virtual Constructor)模式或者多态性工厂(Polymor ...
- Java设计模式系列-工厂方法模式
原创文章,转载请标注出处:<Java设计模式系列-工厂方法模式> 一.概述 工厂,就是生产产品的地方. 在Java设计模式中使用工厂的概念,那就是生成对象的地方了. 本来直接就能创建的对象 ...
随机推荐
- 一起学Spring之Web基础篇
概述 在日常的开发中Web项目集成Spring框架,已经越来越重要,而Spring框架已经成为web开发的主流框架之一.本文主要讲解Java开发Web项目集成Spring框架的简单使用,以及使用Spr ...
- HA: Infinity Stones Vulnhub Walkthrough
下载地址: https://www.vulnhub.com/entry/ha-infinity-stones,366/ 主机扫描: 目录枚举 我们按照密码规则生成字典:gam,%%@@2012 cru ...
- linux学习第一周
1. 按系列罗列Linux的发行版,并描述不同发行版之间的联系与区别 2. 安装Centos7.6操作系统,创建一个自己名字的用户名,并可以正常登录,将主要步骤截图. 3. 配置环境变量,实现执行hi ...
- 自定义Metadata验证属性
一.定义 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...
- ReactNative: 使用AsyncStorage异步存储类
一.简介 AsyncStorage是一个简单的具有异步特性可持久化的键值对key-value的存储系统.它对整个APP而言,是一个全局的存储空间,可以用来替代H5中提供的window属性LocalSt ...
- 2019前端面试系列——CSS面试题
盒模型 /* 红色区域的大小是多少?200 - 20*2 - 20*2 = 120 */ .box { width: 200px; height: 200px; padding: 20px; marg ...
- oc:定时删除ES日志数据释放空间
修改方法: 1.直接编辑修改 查看当前logging-curator配置,了解当前定时删除大的策略. oc edit configmap/logging-curator 打开后,可以直接编辑保存. 2 ...
- 使用Mybatis实现动态SQL(二)
使用Mybatis实现动态SQL 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 写在前面: *本章节适合有Mybatis基础者观看* 使用Mybatis实现动态SQL ...
- Hibernate 框架入门
接着上一篇的 Hibernate 框架的了解,我们就继续学习 Hibernate 框架.这次就进入 Hibernate 框架的入门学习. 首先在学习 Hibernate 框架之前,我们要准备好我们需要 ...
- JS---DOM---点击操作---part2---14个案例
案例1:点击按钮禁用文本框 <input type="button" value="禁用文本框" id="btn" /> < ...