设计模式学习笔记——工厂(Factory)模式
设计模式学习笔记——工厂(Factory)模式
@(设计模式)[设计模式, 工厂模式, factory]
- 设计模式学习笔记工厂Factory模式
- 基本介绍
- 工厂案例
- 类图
- 实现代码
- framework包
- Product抽象类
- Factory抽象类
- idcard包
- IDCard类
- IDCardFactory类
- 测试类
- 运行结果
- framework包
- 工厂模式中的角色
- Product产品
- Creator创建者
- ConcreteProduct具体的产品
- ConcreteCreator具体的创建者
- 类图
基本介绍
工厂(Factory
)模式可以看作模版模式的一种固定实现,如果用模版模式生成实例,这就可以视为工厂模式。
在工厂模式中,抽象的工厂类(父类)决定实例的生成方式和内部细节,但是并不决定所要生成的具体的类,具体的处理全权由具体的工厂类(子类)负责。也就是说抽象的工厂类并不知道自己要生成什么具体的产品,只要是其生成的抽象的产品类的子类就行了。
通过以上方式,可以很方便的将工厂类、产品类(父类)和它们的实现类进行解耦。比如说可以将父类放在framework
包中,子类放在其自己的包中。
工厂案例
类图
实现代码
framework包
Product抽象类
package com.pc.factory.example.framework;/*** 产品类* Created by Switch on 2017-02-09.*/
public abstract class Product {/*** 使用*/public abstract void use();
}
Factory抽象类
package com.pc.factory.example.framework;/*** 工厂类* Created by Switch on 2017-02-09.*/
public abstract class Factory {/*** 创建产品** @param owner 所有者* @return 产品*/public final Product create(String owner) {// 创建产品Product product = this.createProduct(owner);// 注册产品this.registerProduct(product);return product;}/*** 创建产品** @param owner 所有者* @return 产品*/protected abstract Product createProduct(String owner);/*** 注册产品** @param product 产品*/protected abstract void registerProduct(Product product);
}
idcard包
IDCard类
package com.pc.factory.example.idcard;import com.pc.factory.example.framework.Product;/*** IDCard类* Created by Switch on 2017-02-09.*/
public class IDCard extends Product {// 所有者private String owner;/*** 构造方法,需要提供所有者<br/>* 这里有个优化,访问范围为包内,也就是说只能让IDCardFactory创建实例** @param owner 所有者*/IDCard(String owner) {System.out.println("制作" + owner + "的ID卡。");this.owner = owner;}@Overridepublic void use() {System.out.println("使用" + owner + "的ID卡。");}/*** 获取所有者** @return 所有者*/public String getOwner() {return this.owner;}
}
IDCardFactory类
package com.pc.factory.example.idcard;import com.pc.factory.example.framework.Factory;
import com.pc.factory.example.framework.Product;import java.util.HashMap;
import java.util.Map;/*** IDCard工厂类* Created by Switch on 2017-02-09.*/
public class IDCardFactory extends Factory {// ID卡所有者Map集合private Map<String, Product> owners = new HashMap<>();@Overrideprotected Product createProduct(String owner) {return new IDCard(owner);}@Overrideprotected void registerProduct(Product product) {this.owners.put(((IDCard) product).getOwner(), product);}/*** 获取ID卡所有者Map集合** @return ID卡所有者Map集合*/public Map<String, Product> getOwners() {return this.owners;}
}
测试类
package com.pc.factory.example.test;import com.pc.factory.example.framework.Factory;
import com.pc.factory.example.framework.Product;
import com.pc.factory.example.idcard.IDCardFactory;
import org.junit.Test;import java.util.Map;/*** ID卡工厂测试类* Created by Switch on 2017-02-09.*/
public class IDCardFactoryTest {@Testpublic void testIDCard() {Factory factory = new IDCardFactory();Product product1 = factory.create("Switch");Product product2 = factory.create("Kity");Product product3 = factory.create("Tom");product1.use();product2.use();product3.use();// 扩展使用IDCardFactory idCardFactory = (IDCardFactory) factory;Map<String, Product> owners = idCardFactory.getOwners();for (Map.Entry<String, Product> entryOwners : owners.entrySet()) {System.out.println(entryOwners.getKey() + ":" + entryOwners.getValue());}}
}
运行结果
制作Switch的ID卡。
制作Kity的ID卡。
制作Tom的ID卡。
使用Switch的ID卡。
使用Kity的ID卡。
使用Tom的ID卡。
Kity:com.pc.factory.example.idcard.IDCard@e03bb5
Tom:com.pc.factory.example.idcard.IDCard@8c92f4
Switch:com.pc.factory.example.idcard.IDCard@1e4705b
工厂模式中的角色
Product(产品)
Product
角色属于框架这一方,是一个抽象类。它定义了在Factory Method
模式中生成的那些实例所持有的接口API
,但具体的处理则由子类ConcreteProduct
角色决定。在案例中,由Product
类扮演此角色。
Creator(创建者)
Creator
角色属于框架这一方,它是负责生成Product
角色的抽象类,但具体的处理则由子类ConcreteCreator
角色决定。在案例中,由Factory
类扮演此角色。
Creator
角色对于实际负责生成实例的ConcreteCreator
角色一无所知,它唯一知道的就是,只要调用Product
角色和生成实例的方法(类图中的factoryMethod
方法),就可以生成Product
的实例。在案例中,createProduct
方法是用于生成实例的方法。
不用new
关键字来生成实例,而是调用生成实例的专用方法来生成实例,这样就可以防止父类与其他具体类藕合。
ConcreteProduct(具体的产品)
ConcreteProduct
角色属于具体加工这一方,它决定了具体的产品。在案例中,由IDCard类扮演此角色。
ConcreteCreator(具体的创建者)
ConcreteCreator
角色属于具体加工这一方,它负责生成具体的产品。在案例中,由IDCardFactory
类扮演此角色。
类图
GitHub:DesignPatternStudy
——————参考《图解设计模式》
设计模式学习笔记——工厂(Factory)模式相关推荐
- 设计模式 - 学习笔记 - 工厂模式Factory Pattern
设计模式 - 学习笔记 - 工厂模式Factory Pattern 1. 简单工厂 1.1 应用场景 1.2 UML 1.3 优劣分析 好处 缺点 1.4 代码示例 抽象产品 AbstractProd ...
- 设计模式学习笔记-2 创建者模式-工厂方法模式
设计模式学习笔记-2 创建者模式-工厂方法模式 工厂模式介绍 工厂模式又称工厂方法模式,是一种创建型设计模式,其在父类中提供一个创建对象的方法,允许子类决定实例化对象的类型. 这种设计模式使Java开 ...
- 设计模式学习笔记——访问者(Visitor)模式
设计模式学习笔记--访问者(Visitor)模式 @(设计模式)[设计模式, 访问者模式, visitor] 设计模式学习笔记访问者Visitor模式 基本介绍 访问者案例 类图 实现代码 Visit ...
- C++设计模式学习笔记:策略模式
C++设计模式学习笔记:策略模式 策略模式设计商场促销 1.策略模式介绍 2.商场收银系统策略模式实现 3.策略模式与工厂模式结合 3.策略模式与工厂模式对比 策略模式设计商场促销 1.策略模式介绍 ...
- 设计模式学习笔记——解释器(Interpreter)模式
设计模式学习笔记--解释器(Interpreter)模式 @(设计模式)[设计模式, 解释器模式, Interpreter] 设计模式学习笔记解释器Interpreter模式 基本介绍 解释器案例 类 ...
- 设计模式学习笔记——命令(Command)模式
设计模式学习笔记--命令(Command)模式 @(设计模式)[设计模式, 命令模式, command] 设计模式学习笔记命令Command模式 基本介绍 命令案例 类图 实现代码 Command接口 ...
- 设计模式学习笔记——代理(Proxy)模式
设计模式学习笔记--代理(Proxy)模式 @(设计模式)[设计模式, 代理模式, proxy] 设计模式学习笔记代理Proxy模式 基本介绍 代理案例 类图 实现代码 Printable接口 Pri ...
- 设计模式学习笔记——状态(State)模式框架
设计模式学习笔记--状态(State)模式框架 @(设计模式)[设计模式, 状态模式, State] 设计模式学习笔记状态State模式框架 基本介绍 状态案例 类图 实现代码 State接口 Day ...
- 设计模式学习笔记——备忘录(Memento)模式
设计模式学习笔记--备忘录(Memento)模式 @(设计模式)[设计模式, 备忘录模式, memento] 设计模式学习笔记备忘录Memento模式 基本介绍 备忘录案例 类图 实现代码 Memen ...
最新文章
- Python 2大限来了!113天后自生自灭,官方不再维护更新 | 附升级指南
- CSS基础学习-8.CSS盒子模型_标准盒子9.CSS怪异盒子
- Selenium VS QTP
- 【转】Docker 运行时资源限制-内存memory、交换机分区Swap、CPU
- EL表达式和标准标签库
- SpringBoot 2.x (3):文件上传
- numpy使用where函数定位满足条件的下标
- 当Python的lambda表达式遇上变量作用域
- Synchronized 锁升级机制
- 使用windows 10 安装中文版语言
- stm32驱动ssd1306配置_自制FOC控制驱动器
- Mac OS X 下 U盘安装盘简易制作教程
- 实验一:常用网络命令操作
- python 操作微信 群发_python实现给微信公众号发送消息的方法
- 搞笑--网络小说-紫川 高考题-(附答案)
- 谷粒商城九商品服务之商品属性及仓储服务todo
- 【软件设计】5本软件设计书籍的读书笔记
- oracle 导出身份证号_ORACLE对身份证号码处理相关的SQL【收藏】
- 浅谈《网络数据安全标准体系建设指南》(征求意见稿)的意见(附全文)
- 自己动手实现音乐播放器app
热门文章
- 从源码分析 Spring 基于注解的事务
- Docker快速搭建Oracle12c
- matlab 的字符类型转换,数据类型转换
- MATLAB Simulink
- MathWorks 中国
- 【JAVA 第三章 流程控制语句】课后习题 输出正整数的顺序相反数
- 【记录】启用Windows 10下的linux子系统
- 添加谷歌翻译到你的网站
- Hexo博客的备份与恢复
- 【Python】七段数码管绘制问题
- Python 爬虫+tkinter界面 实现历史天气查询
- 给DataList分页有两个办法:1、自定义实现分页方法 2、用第三方控件(例如AspNetPager)