设计模式-01简单工厂模式详解 详细代码对比
目录
- ChatGpt问答
- 原生代码
- 简单工厂模式代码
- 简单工厂模式(Simple Factory Pattern)
- 新增boat 对比两种方法
- 原生代码
- 为什么使用强制转换?
- 简单工厂模式
- 简单工厂方法总结
- 与原生代码的区别:
- 优点:
- 缺点:
- 参考
本文将介绍什么事简单工厂模式,为什么要使用这个模式?比起原生代码它是如何降低代码的耦合度的,以及详细的代码对比,结合ChatGPT的问答进行讲解,也是自己学习的记录,有不对的地方请指正。
ChatGpt问答
Q:Give an example to illustrate the benefits of reducing the coupling
of the simple factory model in java, which is best reflected in code
举个例子来说明java中简单工厂模式减少耦合的好处,在代码中最能体现
Certainly! Let’s illustrate the benefits of reducing coupling by using a simple factory model in Java. We’ll start with a coupled implementation and then refactor it to reduce coupling.
当然! 让我们说明一下在 Java 中使用简单工厂模型来减少耦合的好处。 我们将从耦合实现开始,然后重构它以减少耦合。
Coupled Implementation (High Coupling):
Consider a simple factory that produces different types of vehicles (e.g., cars and bicycles) based on a provided type. In this coupled example, the factory method has a high level of coupling with the concrete vehicle classes.
考虑一个简单的工厂,它根据提供的类型生产不同类型的车辆(例如汽车和自行车)。 在这个耦合示例中,工厂方法与具体车辆类具有高度耦合。
原生代码
class VehicleFactory {public Vehicle createVehicle(String type) {if ("car".equalsIgnoreCase(type)) {return new Car();} else if ("bicycle".equalsIgnoreCase(type)) {return new Bicycle();} else {throw new IllegalArgumentException("Invalid vehicle type");}}
}class Car {void start() {System.out.println("Car started.");}
}class Bicycle {void start() {System.out.println("Bicycle started.");}
}public class Main {public static void main(String[] args) {VehicleFactory factory = new VehicleFactory();Vehicle vehicle1 = factory.createVehicle("car");vehicle1.start(); // Output: Car started.Vehicle vehicle2 = factory.createVehicle("bicycle");vehicle2.start(); // Output: Bicycle started.}
}
In this example, the VehicleFactory class has high coupling because it directly creates instances of Car and Bicycle. If we need to add more types of vehicles in the future, we would need to modify the factory method, resulting in increased coupling and reduced flexibility.
在这个例子中,VehicleFactory类具有高耦合性,因为它直接创建Car和Bicycle的实例。 如果以后需要增加更多类型的车辆,就需要修改工厂方法,导致耦合性增加,灵活性降低。
简单工厂模式代码
Reduced Coupling Implementation (Low Coupling):
To reduce coupling, we can use interfaces and abstract classes to define a common contract for all vehicles. This way, the factory method doesn’t need to know the concrete classes of vehicles.
interface Vehicle {void start();
}class Car implements Vehicle {public void start() {System.out.println("Car started.");}
}class Bicycle implements Vehicle {public void start() {System.out.println("Bicycle started.");}
}class VehicleFactory {public Vehicle createVehicle(String type) {if ("car".equalsIgnoreCase(type)) {return new Car();} else if ("bicycle".equalsIgnoreCase(type)) {return new Bicycle();} else {throw new IllegalArgumentException("Invalid vehicle type");}}
}public class Main {public static void main(String[] args) {VehicleFactory factory = new VehicleFactory();Vehicle vehicle1 = factory.createVehicle("car");vehicle1.start(); // Output: Car started.Vehicle vehicle2 = factory.createVehicle("bicycle");vehicle2.start(); // Output: Bicycle started.}
}
In this refactored example, we’ve introduced an interface Vehicle, which both Car and Bicycle implement. The VehicleFactory now returns instances of the Vehicle interface instead of concrete classes. This reduces coupling because the factory method doesn’t depend on specific implementations, making it easier to extend the system with new types of vehicles without modifying existing code.
在这个重构的示例中,我们引入了一个“Vehicle”接口,“Car”和“Bicycle”都实现了该接口。 “VehicleFactory”现在返回“Vehicle”接口的实例,而不是具体类。 这减少了耦合,因为工厂方法不依赖于特定的实现,从而可以更轻松地使用新型车辆扩展系统,而无需修改现有代码。
简单工厂模式(Simple Factory Pattern)
是一种创建型设计模式,它提供了一种简单的方法来创建对象,而不需要直接暴露对象的创建逻辑给客户端。
简单工厂模式又称静态工厂方法模式。它存在的目的很简单:定义一个用于创建对象的接口。
组成结构:
-
工厂类角色(Creator):这是本模式的核心,含有一定的商业逻辑和判断逻辑。在java中它往往由一个具体类实现。
-
抽象产品角色(Product):它一般是具体产品继承的父类或者实现的接口。在java中由接口或者抽象类来实现。
-
具体产品角色(ConcreteProduct):工厂类所创建的对象就是此角色的实例。在java中由一个具体类实现。
用类图表示它们之间的关系

抽象产品角色–Vehicle 交通工具
具体产品角色 – Car、Bicycle
工厂类角色–VehicleFactory
新增boat 对比两种方法
原生代码
Certainly! In a high coupling approach, you would directly create instances of vehicle classes within the factory method, which can lead to tight dependencies between the factory and the concrete vehicle classes. Here’s an example of a high coupling approach:
当然! 在高耦合方法中,您将直接在工厂方法中创建车辆类的实例,这可能导致工厂和具体车辆类之间的紧密依赖关系。 这是一个高耦合方法的示例:
class Car {void start() {System.out.println("Car started.");}
}class Bicycle {void start() {System.out.println("Bicycle started.");}
}class Boat {void start() {System.out.println("Boat started.");}
}class VehicleFactory {public Object createVehicle(String type) {if ("car".equalsIgnoreCase(type)) {return new Car();} else if ("bicycle".equalsIgnoreCase(type)) {return new Bicycle();} else if ("boat".equalsIgnoreCase(type)) {return new Boat();} else {throw new IllegalArgumentException("Invalid vehicle type");}}
}public class Main {public static void main(String[] args) {VehicleFactory factory = new VehicleFactory();Car car = (Car) factory.createVehicle("car");car.start(); // Output: Car started.Bicycle bicycle = (Bicycle) factory.createVehicle("bicycle");bicycle.start(); // Output: Bicycle started.Boat boat = (Boat) factory.createVehicle("boat");boat.start(); // Output: Boat started.}
}
In this high coupling approach:
- Each vehicle class (
Car,Bicycle,Boat) is directly instantiated within thecreateVehiclemethod of theVehicleFactory. - To use a specific vehicle, you need to cast the object returned by the factory to the corresponding vehicle class (
Car,Bicycle,Boat). - Adding a new type of vehicle would require modifying the
VehicleFactoryto create that vehicle type directly.
在这种高耦合方法中:
- 每个车辆类(“Car”、“Bicycle”、“Boat”)都直接在“VehicleFactory”的“createVehicle”方法中实例化。
- 要使用特定的车辆,需要将工厂返回的对象转换为相应的车辆类(
Car、Bicycle、Boat)。 - 添加新类型的车辆需要修改
VehicleFactory以直接创建该车辆类型。
High coupling can make the code less flexible and harder to maintain because any changes to the factory method or the concrete vehicle classes may affect other parts of the code that rely on them. It’s generally a best practice to reduce coupling and use interfaces or abstract classes to achieve better flexibility and maintainability.
高耦合会使代码不太灵活且难以维护,因为对工厂方法或具体车辆类的任何更改都可能会影响依赖它们的代码的其他部分。 通常,最佳实践是减少耦合并使用接口或抽象类来实现更好的灵活性和可维护性。
为什么使用强制转换?
Car car = (Car) factory.createVehicle(“car”)
In the high coupling approach I provided earlier, you are correct that type casting is required when you retrieve the created objects from the factory. This is a limitation of high coupling because you’re directly creating instances of specific classes within the factory method. Let me explain this in more detail:
In the createVehicle method of the VehicleFactory, the factory creates instances of Car, Bicycle, or Boat based on the type parameter.
Since the createVehicle method is declared to return an Object, when you retrieve the created object, it’s treated as an Object reference.
To use the specific functionality of a Car, for example, you need to cast the Object reference back to Car to access Car-specific methods like start.
This type casting introduces potential issues because if you provide the wrong type for the type parameter or if you introduce a new vehicle type in the future, you might get runtime errors (ClassCastException) if the casting doesn’t match the actual type.
In contrast, a low coupling approach, as demonstrated in the previous example, uses interfaces or abstract classes to create a common contract for all vehicles. This way, you can avoid type casting and handle all vehicles uniformly by relying on the common interface. It provides better flexibility, maintainability, and type safety, as you don’t need to cast objects to specific types when using them.
在我之前提供的高耦合方法中,当您从工厂检索创建的对象时,需要进行类型转换,这是正确的。 这是高耦合的限制,因为您是在工厂方法中直接创建特定类的实例。 让我更详细地解释一下:
在 VehicleFactory 的 createVehicle 方法中,工厂根据类型参数创建 Car、Bicycle 或 Boat 的实例。
由于 createVehicle 方法被声明为返回一个对象,因此当您检索创建的对象时,它将被视为对象引用。
例如,要使用汽车的特定功能,您需要将对象引用强制转换回汽车以访问汽车特定的方法,例如启动。
这种类型转换会带来潜在的问题,因为如果您为类型参数提供了错误的类型,或者如果您将来引入新的车辆类型,并且转换与实际类型不匹配,则可能会出现运行时错误 (ClassCastException)。
相反,低耦合方法(如前面的示例所示)使用接口或抽象类为所有车辆创建通用契约。 这样,您可以避免类型转换并依靠通用接口统一处理所有车辆。 它提供了更好的灵活性、可维护性和类型安全性,因为您在使用对象时不需要将对象强制转换为特定类型。
简单工厂模式
Q:show me High Coupling method
Certainly! Let’s add a new Boat vehicle to the existing code. We’ll follow the same approach of using an interface for Boat and updating the factory method to create Boat instances without modifying the existing code.
当然! 让我们在现有代码中添加一个新的“Boat”车辆。 我们将遵循相同的方法,使用“Boat”接口并更新工厂方法来创建“Boat”实例,而无需修改现有代码。
Adding a Boat Vehicle (Low Coupling):
interface Vehicle {void start();
}class Car implements Vehicle {public void start() {System.out.println("Car started.");}
}class Bicycle implements Vehicle {public void start() {System.out.println("Bicycle started.");}
}class Boat implements Vehicle {public void start() {System.out.println("Boat started.");}
}class VehicleFactory {public Vehicle createVehicle(String type) {if ("car".equalsIgnoreCase(type)) {return new Car();} else if ("bicycle".equalsIgnoreCase(type)) {return new Bicycle();} else if ("boat".equalsIgnoreCase(type)) {return new Boat();} else {throw new IllegalArgumentException("Invalid vehicle type");}}
}public class Main {public static void main(String[] args) {VehicleFactory factory = new VehicleFactory();Vehicle vehicle1 = factory.createVehicle("car");vehicle1.start(); // Output: Car started.Vehicle vehicle2 = factory.createVehicle("bicycle");vehicle2.start(); // Output: Bicycle started.Vehicle vehicle3 = factory.createVehicle("boat");vehicle3.start(); // Output: Boat started.}
}
In this updated code, we’ve introduced a new Boat class that implements the Vehicle interface. The VehicleFactory has been updated to handle the creation of Boat instances when “boat” is passed as the type. This allows us to add a new type of vehicle (in this case, a boat) without modifying the existing code for the factory or the other vehicles. This demonstrates the flexibility and low coupling achieved by using interfaces and an abstract factory approach.
在此更新的代码中,我们引入了一个新的“Boat”类,它实现了“Vehicle”接口。 “VehicleFactory”已更新,可以在“boat”作为类型传递时处理“Boat”实例的创建。 这使我们能够添加一种新型车辆(在本例中为船),而无需修改工厂或其他车辆的现有代码。 这展示了通过使用接口和抽象工厂方法实现的灵活性和低耦合。
简单工厂方法总结
与原生代码的区别:
从三个维度来说明:
抽象产品角色–Vehicle 交通工具
具体产品角色 – Car、Bicycle
工厂类角色–VehicleFactory
1 简单工厂模式中抽象产品角色Vehicle 使用interface,原生代码是class
2 简单工厂模式中具体产品角色Car,需要实现接口,原生代码为单独类
3 简单工厂模式中具体工厂类角色–VehicleFactory 返回对象是Vehicle 接口,原生代码的返回对象是Object
优点:
- 将对象的创建逻辑集中在工厂类中,降低了客户端的复杂度。
- 隐藏了创建对象的细节,客户端只需要关心需要创建何种对象,无需关心对象是如何创建的。
可以通过修改工厂类来轻松添加新的产品类
缺点:
- 如果产品的类太多,会导致工厂类中的代码变得很复杂,难以维护。
- 添加新产品时,需要修改工厂类,也就是会在OperationFactory类中新增case语句,这违背了开闭原则。
总体而言,简单工厂模式适用于创建对象的逻辑相对简单,且产品类的数量较少的场景。对于更复杂的对象创建和对象之间的依赖关系,可以考虑使用其他创建型设计模式,如工厂方法模式或抽象工厂模式。
参考
1 设计模式-01.简单工厂方法https://juejin.cn/post/7267091509953855543
2 JAVA设计模式(三) – 工厂模式
https://juejin.cn/post/7257441765976162361?searchId=202309061335355A323BC0D43AD67B950D#heading-0
相关文章:
设计模式-01简单工厂模式详解 详细代码对比
目录 ChatGpt问答原生代码简单工厂模式代码 简单工厂模式(Simple Factory Pattern)新增boat 对比两种方法原生代码为什么使用强制转换?简单工厂模式 简单工厂方法总结与原生代码的区别:优点:缺点: 参考 本文将介绍什么…...
IPD-PDT-POP角色的名称、定位和职责说明书
在IPD推进中,有一个不是很关键但却离不开的角色叫做POP,POP这个角色通常是设置在PDT团队中。 那么IPD的PDT团队中的POP这个角色到底是什么意思呢?POP如何开展工作,以及POP的主要岗位职责有哪些呢?华研荟今天给大家分享…...
在MySQL中查看数据库和表的数据大小
在MySQL中查看数据库和表的数据大小 在管理和维护MySQL数据库时,了解数据库和表的数据大小是非常重要的。这可以帮助您监控数据库的增长、优化性能以及规划存储需求。本博客将介绍如何使用SQL查询来查看MySQL数据库和表的数据大小。 查看MySQL数据库的总数据大小 …...
Android前端音视频数据接入GB28181平台意义
技术背景 在华脉智联研发Android平台GB28181前端音视频接入模块之前,业内听到最多的是,如何用Android端在没有国标摄像头设备的前提下,模拟GB28181的信令和媒体流交互流程,实现GB28181整体方案的测试。 Android端真的没有必要做…...
Ubuntu 20.04上docker安装Redis
要在Ubuntu 20.04上使用Docker安装Redis,您可以按照以下步骤进行操作: 1.更新系统包列表:sudo apt update2.安装Docker:sudo apt install docker.io3.启动Docker服务并设置其开机自启动:sudo systemctl start docker …...
linux 压缩webfile文件夹 webfile.tar.gz和webfile.tar的区别
linux 压缩webfile文件夹 在Linux中,你可以使用tar命令来压缩文件夹。以下是将文件夹压缩为名为"webfile.tar"的示例命令: cd到webfile所在的文件夹,然后执行 tar -cvf webfile.tar webfile/上述命令中,-c选项表示创建…...
基于SSM的农产品推广应用网站
末尾获取源码 开发语言:Java Java开发工具:JDK1.8 后端框架:SSM 前端:采用Vue技术开发 数据库:MySQL5.7和Navicat管理工具结合 服务器:Tomcat8.5 开发软件:IDEA / Eclipse 是否Maven项目&#x…...
人大金仓分析型数据库身份鉴别
目录 前言 数据库总参数 口令尝试次数 口令更换周期 明文密码替换 用户登录信息显示 前言 身份鉴别功能包含口令尝试次数限制、口令更换周期管理、明文密码替换和用户登录信息显示等几部分。 数据库总参数 identity_auth.enable 是否使用身份鉴别功能,布尔值…...
基于SpringBoot的在线教育平台系统
基于SpringBootVue的线教育平台系统,前后端分离 开发语言:Java数据库:MySQL技术:SpringBoot、Vue、Mybaits Plus、ELementUI工具:IDEA/Ecilpse、Navicat、Maven 【主要功能】 角色:管理员、学生、老师 …...
基于大规模测量和多任务深度学习的电子鼻系统目标识别、浓度预测和状态判断
Target discrimination, concentration prediction, and status judgment of electronic nose system based on large-scale measurement and multi-task deep learning 摘要 为了实现响应特征的自动提取,简化模型的训练和应用过程,设计了一种双块知识…...
Unity游戏客户端进阶路线(只针对本人)
一、初级Unity游戏开发工程师需要掌握以下内容: 1.掌握编程语言,一般都会学C#,需要掌握C#编程语言的基础内容以及Unity3D的引擎配置。 2.掌握Unity引擎,对使用的引擎有深入的了解,掌握它的特性以及局限性。 3.熟悉游戏…...
【C++】封装map和set(红黑树实现)
前言: 前面,我们学习了set和map的用法,这两个容器可以完成查找,排序等操作,后来我们在学习过二叉搜索树的基础上又学习了两种特殊的二叉搜索树——AVL树和红黑树,他们俩可以是效率进一步提高,其…...
【补】代码随想录算法训练营day38|动态规划 |509. 斐波那契数|70. 爬楼梯|746. 使用最小花费爬楼梯
动态规划,英文:Dynamic Programming,简称DP,如果某一问题有很多重叠子问题,使用动态规划是最有效的。所以动态规划中每一个状态一定是由上一个状态推导出来的,这一点就区分于贪心,贪心没有状态推…...
C语言sizeof()计算空间大小为8的问题
在练习数据结构过程中,定义指针p,并且申请了10个char类型空间,但在计算p所指空间大小时候,发现了一些奇怪的现象。 #include <stdio.h> #include <stdlib.h>int main(){char s[12];printf("the size of memory …...
时序分解 | MATLAB实现基于LMD局部均值分解的信号分解分量可视化
时序分解 | MATLAB实现基于LMD局部均值分解的信号分解分量可视化 目录 时序分解 | MATLAB实现基于LMD局部均值分解的信号分解分量可视化效果一览基本介绍程序设计参考资料 效果一览 基本介绍 LMD局部均值分解 直接替换Excel即可运行包含频谱图相关系数图 Matlab语言 1.算法新颖…...
景区AR虚拟三维场景沉浸式体验成为新兴的营销手段
科技的迅速崛起正在改变我们的世界,旅游业也在这股浪潮中掀起了一场全新的变革。增强现实(AR)技术正成为旅行中的一股强大力量,通过增添趣味和交互性,为旅程注入了前所未有的活力。本文将带您深入了解AR如何为旅游带来全新的体验,…...
【深度学习】 Python 和 NumPy 系列教程(五):Python容器:3、集合Set详解(初始化、访问元素、常用操作、常用函数)
目录 一、前言 二、实验环境 三、Python容器(Containers) 0、容器介绍 1、列表(List) 2、元组(Tuple) 3、集合(Set) 1. 初始化 2. 访问集合元素 3. 常用操作 a. 添加单个…...
单片机C语言实例:6、定时器的应用
一、定时器0控制LED闪烁 实例程序1: #include<reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义sbit LED P1^2; //定义LED端口/*------------------------------------------------定时器初始化子程序 …...
ChatGPT Prompting开发实战(五)
一、如何编写有效的prompt 对于大语言模型来说,编写出有效的prompt能够帮助模型更好地理解用户的意图(intents),生成针对用户提问来说是有效的答案,避免用户与模型之间来来回回对话多次但是用户不能从LLM那里得到有意义的反馈。本文通过具体…...
MySQL——DQL union合并、limit限制与DDL建表和删表
一、Union 合并 union:是实现两个查询结果的合并。 例如:当我们查询员工名字为manager 和 salesman的员工名字和 工作? select e.ename,e.job from emp e where e.jobmanager or e.job salesman; select e.ename,e.job from emp e where e.job in(man…...
OpenClaw长期运行:Qwen3.5-9B自动化系统的维护与更新
OpenClaw长期运行:Qwen3.5-9B自动化系统的维护与更新 1. 为什么需要长期维护? 去年冬天,我部署了一个基于OpenClaw和Qwen3.5-9B的自动化系统来处理日常的文档整理工作。最初几周运行得很顺利,直到某个凌晨,系统突然停…...
FastJson内存泄漏实战:我是如何用MAT工具定位到IdentityHashMap这个坑的
FastJson内存泄漏深度剖析:从MAT工具实战到IdentityHashMap陷阱破解 凌晨三点,手机突然响起刺耳的告警声——生产环境某核心服务的堆内存使用率突破95%。作为值班工程师,我瞬间清醒过来。这不是普通的OOM,而是一场持续增长的内存…...
vscode-drawio扩展依赖更新:安全高效地管理第三方库
vscode-drawio扩展依赖更新:安全高效地管理第三方库 【免费下载链接】vscode-drawio This unofficial extension integrates Draw.io (also known as diagrams.net) into VS Code. 项目地址: https://gitcode.com/gh_mirrors/vs/vscode-drawio vscode-drawio…...
HunyuanVideo-Foley效果展示:为体育直播生成实时观众欢呼/球鞋摩擦/哨声
HunyuanVideo-Foley效果展示:为体育直播生成实时观众欢呼/球鞋摩擦/哨声 1. 惊艳的体育音效生成能力 想象一下,当篮球运动员急停变向时,球鞋与地板摩擦发出的"吱吱"声;当足球射门得分时,全场观众爆发的欢呼…...
ESFT-gate-law-lite:法律文本智能分析新工具
ESFT-gate-law-lite:法律文本智能分析新工具 【免费下载链接】ESFT-gate-law-lite ESFT-gate-law-lite是基于HuggingFace的深度学习模型,专为法律领域定制。源自deepseek-ai团队,继承ESFT-vanilla-lite优势,强大而轻量,…...
首款支持AI渗透的WebShell管理工具,聊个天就能实现免杀|实现高隐蔽内网渗透
0x01 工具介绍 金刚狼首款支持 AI 渗透的 WebShell MCP,也是一款支持多层内网级联的 ASPX、ASHX 高级 WebShell 管理工具。工具采用 AES 加密通信,无需代理即可实现内网穿透,支持内存加载各类渗透工具,做到无文件落地隐蔽渗透目标…...
用MobaXterm替代传统终端的完整指南
Windows远程运维革命:用MobaXterm替代传统终端的完整指南 每次打开 PuTTY 时,你是否会对着那个灰暗的界面叹气?当需要在Xshell中频繁切换标签时,是否感到效率低下?作为Windows系统管理员或开发者,我们长期忍…...
# 发散创新:基于群体智能的Python蚁群算法优化路径规划实战在人工智能快速演进的时代,**群体智能(Swarm Int
发散创新:基于群体智能的Python蚁群算法优化路径规划实战 在人工智能快速演进的时代,群体智能(Swarm Intelligence) 作为一类受自然界生物行为启发的计算范式,正逐渐成为解决复杂优化问题的重要工具。本文聚焦于蚁群算…...
AI绘画新革命:SDXL-Turbo镜像快速上手与实战测评
AI绘画新革命:SDXL-Turbo镜像快速上手与实战测评 想象一下这样的场景:你刚输入完几个单词,屏幕上就立即呈现出对应的图像。没有等待,没有延迟,就像思维直接转化为画面一样流畅。这就是SDXL-Turbo带来的AI绘画新体验—…...
后端架构师转型AI智能体架构师:3个月实战路径,收藏这份落地指南
如果你本身就是后端/全栈/架构师出身,这意味着你已经有了一套非常扎实的“确定性系统”的构建能力——分布式、高并发、数据库事务、系统稳定性,这些都是你的底牌。 而AI智能体恰恰是“不确定性系统”(大模型)与“确定性系统”&am…...
