大牧絮叨设计模式:工厂方法模式
文章目录
- 1、 工厂方法模式概述
- 1.1、 工厂方法模式核心组件
- 1.2、 工厂方法模式优缺点
- 2、 `Java`实现
- (1) 核心工厂声明
- (2) 核心产品声明
- (3) 产品具体实现
- (4) 工厂具体实现
- (5) 代码测试
- 3、 `Python`实现
- 4、 `Go`实现
- 4.1、 定义工厂及产品接口
- 4.2、 构建具体产品类型
- 4.3、 构建具体工厂类型
- 4.4、测试代码
1、 工厂方法模式概述
工厂方法模式是一种创建模式
,又被称为虚拟构造子模式(Virtual Constructor)
或者多态性工厂模式(Polymoriphoic Factory)
。工厂方法模式是目标是定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类中。
1.1、 工厂方法模式核心组件
工厂方法模式是在简单工厂模式上的改进,主要包含如下几个角色及组件
- 抽象工厂(Creator):整个工厂模式的核心角色,它与应用无关,主要在创建模式中规范和产品对应的工厂对象的标准化定义。
- 具体工厂(Concrete Creator):实现了抽象工厂的具体工厂类,该类型是和应用直接交互的具体实现类,在应用程序中调用,用于创建产品对象。
- 抽象产品(Product):工厂方法模式创建的所有类型的超级父类,该类型和具体业务有关,用于规范工厂方法模式中创建的方法对象具备的公共特征行为。
- 具体产品(Concrete Product):该类型实现了抽象产品 父类,是工厂方法模式中具体创建的实例对象。
1.2、 工厂方法模式优缺点
优点:
在简单工厂模式上的改进,核心工厂类不再负责所有产品的构建,而是将具体的工作交给子类进行实现,不再接触和业务相关的具体细节,如此进一步抽象的结果,最直接的作用就是在满足OCP原则
的基础上实现了功能的扩展。
缺点:
软件的水平功能扩展已经非常可观,但是对于新功能扩展,灵活性上稍有欠缺,在横向扩展时如果出现新的业务逻辑就需要更改原有的工厂类型代码予以满足了。
在本章节的代码演示中,为了能用最简洁的逻辑结构说明工厂方法模式,不进行多层构建,大家看代码的时候可以自行拓展。
2、 Java
实现
(1) 核心工厂声明
package com.damu.inter;/*** <p>项目文档: 工厂接口</p>* @author 大牧* @version V1.0*/
public interface IFactory<T> {/*** 获取具体产品实例的方法* @return 返回创建的实例对象*/T product();
}
(2) 核心产品声明
package com.damu.inter;/*** <p>项目文档: 产品接口</p>** @author 大牧* @version V1.0*/
public interface IProduct {/*** 产品类型的公共方法* @return 返回产品信息*/String getInformation();
}
(3) 产品具体实现
为了简洁起见,我们直接实现IProduct
接口完成具体产品类的定义,不再进行多层声明。
package com.damu.inter.product.impl;import com.damu.inter.IProduct;/*** <p>项目文档: 产品具体实现</p>** @author 大牧* @version V1.0*/
public class PhoneProduct implements IProduct {@Overridepublic String getInformation() {return "电视很NB,报纸很NB,杂志很NB,游戏机很NB,小说很NB,最终都被手机干掉了";}
}
package com.damu.inter.product.impl;import com.damu.inter.IProduct;/*** <p>项目文档: TODO</p>** @author 大牧* @version V1.0*/
public class ComputerProduct implements IProduct {@Overridepublic String getInformation() {return "电脑,官方称呼计算机,主要用于进行数据运算的一台机器。";}
}
(4) 工厂具体实现
package com.damu.inter.factory.impl;import com.damu.inter.IFactory;
import com.damu.inter.IProduct;
import com.damu.inter.product.impl.PhoneProduct;/*** <p>项目文档: 具体工厂</p>** @author 大牧* @version V1.0*/
public class PhoneFactory implements IFactory<IProduct> {@Overridepublic PhoneProduct product() {// 工厂标准方法中,完成指定产品对象的构建return new PhoneProduct();}
}
package com.damu.inter.factory.impl;import com.damu.inter.IFactory;
import com.damu.inter.IProduct;
import com.damu.inter.product.impl.ComputerProduct;/*** <p>项目文档: TODO</p>** @author 大牧* @version V1.0*/
public class ComputerFactory implements IFactory<IProduct> {@Overridepublic ComputerProduct product() {// 工厂方法标注方法:完成对象的创建并返回return new ComputerProduct();}
}
(5) 代码测试
package com.damu;import com.damu.inter.IFactory;
import com.damu.inter.IProduct;
import com.damu.inter.factory.impl.ComputerFactory;
import com.damu.inter.factory.impl.PhoneFactory;
import com.damu.inter.product.impl.ComputerProduct;
import com.damu.inter.product.impl.PhoneProduct;/*** <p>项目文档: TODO</p>** @author 大牧* @version V1.0*/
public class Main {public static void main(String[] args) {// 创建工厂对象IFactory<IProduct> phoneFactory = new PhoneFactory();// 通过工厂穿件具体对象IProduct phoneProduct = phoneFactory.product();System.out.println(phoneProduct.getInformation());// 创建工厂对象IFactory<IProduct> computerFactory = new ComputerFactory();// 通过工厂创建具体对象IProduct computerProduct = computerFactory.product();System.out.println(computerProduct.getInformation());}
}
在测试代码中,我们可以观察到在获取到统一的工厂实例对象后,通过工厂实例创建的具体产品对象,是根据在构建的时候的具体工厂决定的,也就是具体工厂和具体产品之间的业务关系是比较紧密的,运行结果如下:
/Library/../classes com.damu.Main
电视很NB,报纸很NB,杂志很NB,游戏机很NB,小说很NB,最终都被手机干掉了
电脑,官方称呼计算机,主要用于进行数据运算的一台机器。
3、 Python
实现
还原Java工厂方法模式
的实现
"""
工厂方法模式
"""
import abcclass IFactory(metaclass=abc.ABCMeta):"""工厂接口"""@abc.abstractmethoddef product(self):raise NotImplementedError("该方法必须在工厂子类中实现")class IProduct(metaclass=abc.ABCMeta):"""产品接口"""@abc.abstractmethoddef get_information(self):raise NotImplementedError("该方法必须在产品子类中实现")class PhoneProduct(IProduct):"""手机产品"""def get_information(self):return "电视很NB,报纸很NB,杂志很NB,游戏机很NB,小说很NB,最终都被手机干掉了"class ComputerProduct(IProduct):"""电脑产品"""def get_information(self):return "电脑,官方称呼计算机,主要用于进行数据运算的一台机器"class PhoneFactory(IFactory):"""手机工厂"""def product(self):"""生产手机对象的工厂方法"""return PhoneProduct()class ComputerFactory(IFactory):"""电脑工厂"""def product(self):"""生产电脑对象的工厂方法"""return ComputerProduct()if __name__ == "__main__":"""测试代码"""# 创建工厂实例phoneFactory = PhoneFactory()# 创建产品phone = phoneFactory.product()print(phone.get_information())# 创建电脑工厂computerFactory = ComputerFactory()# 创建产品computer = computerFactory.product()print(computer.get_information())
4、 Go
实现
4.1、 定义工厂及产品接口
package mainimport "fmt"/*
定义产品接口
*/
type IProduct interface {// 获取产品信息的方法GetInformation() string
}/*
定义工厂接口*/
type IFactory interface {// 生产产品的方法product() IProduct
}
4.2、 构建具体产品类型
/*
定义具体产品:手机、电脑*/
type PhoneProduct struct {}
// 实现工厂方法
func (phone PhoneProduct) GetInformation() string {return "电视很NB,报纸很NB,杂志很NB,游戏机很NB,小说很NB,最终都被手机干掉了"
}/*
产品:电脑*/
type ComputerProduct struct{}
// 实现工厂方法
func (computer ComputerProduct) GetInformation() string {return "电脑,官方称呼计算机,主要用于进行数据运算的一台机器。"
}
4.3、 构建具体工厂类型
/*
具体工厂:手机工厂*/
type PhoneFactory struct{}
// 实现接口方法
func (phoneFactory PhoneFactory) product() IProduct {return new(PhoneProduct)
}/*
具体工厂:电脑工厂*/
type ComputerFactory struct{}
// 实现接口方法
func (computerFactory ComputerFactory) product() IProduct {return new(ComputerProduct)
}
4.4、测试代码
func main() {// 创建工厂对象phoneFactory := new(PhoneFactory)// 创建具体对象phone := phoneFactory.product()fmt.Println(phone.GetInformation())// 创建工厂对象computerFactory := new(ComputerFactory)// 创建具体对象computer := computerFactory.product()fmt.Println(computer.GetInformation())
}
大牧絮叨设计模式:工厂方法模式相关推荐
- 大牧絮叨设计模式:建造者模式
文章目录 1. `建造模式` 概述 1.1. 核心组件 1.2. 优点缺陷 2. `Java` 实现 2.1. 抽象建造者`Builder` 2.2. 内聚组件`Aggregation Product ...
- 大牧絮叨设计模式:原型模式
文章目录 1. `原型模式`概述 1.1. 核心组件 1.2. 优点缺陷 2. `Java`实现 2.1. 原型抽象 2.2. 原型实现 2.3. 原型对象管理器 2.4. 消费者 2.5. `深.浅 ...
- 4. 星际争霸之php设计模式--工厂方法模式
题记 ============================================================================== 本php设计模式专辑来源于博客(jy ...
- java 工厂方法_java设计模式-工厂方法模式
1.工厂方法(FactoryMethod)模式的定义 定义一个创建产品对象的工厂接口,将产品对象的实际创建工作推迟到具体子工厂类当中.这满足创建型模式中所要求的"创建与使用相分离" ...
- 大牧絮叨设计模式:抽象工厂模式
文章目录 1. 抽象工厂模式概述 1.1. 核心组件 1.2. 优点缺点 2. `Java`实现 2.1. 工厂及产品结构的定义 2.2. 具体工厂类的实现 3. `Python`实现 4. `Go` ...
- 大牧絮叨设计模式:简单工厂模式
文章目录 1. 简单工厂模式概述 1.1.简单工厂模式核心组件 1.2.简单工厂模式优缺点 2.` Java`实现 公共父类定义 产品具体实现类 简单工厂定义 代码运行测试: 3. `Python`实 ...
- 设计模式 | 工厂方法模式及典型应用
工厂方法模式 工厂方法模式(Factory Method Pattern):定义一个用于创建对象的接口,让子类决定将哪一个类实例化.工厂方法模式让一个类的实例化延迟到其子类. 工厂方法模式又简称为工厂 ...
- Java设计模式—工厂方法模式抽象工厂模式
工厂方法模式与抽象工厂模式都是设计模式中重要而且常见的模式. 工厂方法模式:定义一个用于创建对象的接口,让子类决定实例化哪一个类.工厂方法使一个类的实例化延迟到其子类. 通用类图如下: 在 ...
- Java设计模式-工厂方法模式和抽象工厂模式
工厂方法模式定义: 即定义一个创建对象的接口(即抽象工厂类),让其子类(具体工厂类)决定实例化哪一个类(具体产品类)."一对一"的关系 1,一抽象工厂类派生出多个具体工厂类: 2, ...
最新文章
- 智能+制造,聪明的公司都走上了智能制造的道路
- 孟晚舟升任华为轮值董事长,任正非曾表态:她无技术背景,不会成为接班人...
- POJ - 3347 Kadj Squares(思维+几何)
- hdu 1317 XYZZY【Bellheman_ford 判断正环小应用】
- golang time包梳理
- 《独家记忆》见面会高甜宠粉 张超现场解锁隐藏技能
- Leaflet文档阅读笔记-Extending Leaflet: Handlers and Controls笔记
- CodeForces 297A Parity Game (脑补题)
- CSDN编辑器 修改代码颜色
- numpy 常用工具函数 —— np.bincount/np.average
- Flutter TextField 文本输入框的基本属性及详解
- (完整版)原因可能是堆被损坏,这也说明 中或它所加载的任何DLL 中有bug】的解决
- 计算机硬盘检测不到,硬盘检测不到怎么解决
- 抖音巨量千川投放受到口碑分影响?该怎么提高口碑分?
- django经度纬度计算两点距离实例及微信商家付款给用户接口实例
- 老男孩读PCIe之一:从PCIe速度说起
- 九度 题目1013:开门人和关门人
- eNSP配置防火墙进入Web界面
- 边缘计算系列之MEC介绍
- Java字节转字符串
热门文章
- BERT文本分类实战
- Linux 的带宽管理系统
- java dozer_java开发工具类之Dozer的使用
- TVS管选型(SP1115-01UTG)
- Dialogue System for Unity文档中英对照版(简雨原创翻译)第五篇(第三方插件拓展)
- 优惠券数据库结构设计
- netty应用场景之三点
- Armbian 完全卸载 Python
- 关于《设计模式》与《设计模式沉思录》中提到的“常露齿嘻笑的猫”(Cheshire Cat)的说明...
- Linux基本命令的记录(vi命令,查看文件内容,显示进程,切换用户等)