Ruby简单工厂模式与工厂方法模式使用详解
时间:2026-08-17 | 作者:电竞小硕 | 阅读:0之前读过《Ruby设计模式》,不过时间久了细节有些模糊。最近入手了《大话设计模式》,发现这种轻松的风格确实更容易消化,于是顺手用Ruby把其中的示例实现了下。
简单工厂模式
# -*- encoding: utf-8 -*-
#运算类
class Operation
attr_accessor :number_a,:number_b
def initialize(number_a = nil, number_b = nil)
@number_a = number_a
@number_b = number_b
end
def result
0
end
end
#加法类
class OperationAdd < Operation
def result
number_a + number_b
end
end
#减法类
class OperationSub < Operation
def result
number_a - number_b
end
end
#乘法类
class OperationMul < Operation
def result
number_a * number_b
end
end
#除法类
class OperationDiv < Operation
def result
raise '除数不能为0' if number_b == 0
number_a / number_b
end
end
#工厂类
class OperationFactory
def self.create_operate(operate)
case operate
when '+'
OperationAdd.new()
when '-'
OperationSub.new()
when '*'
OperationMul.new()
when '/'
OperationDiv.new()
end
end
end
oper = OperationFactory.create_operate('/')
oper.number_a = 1
oper.number_b = 2
p oper.result
这种设计的好处在于显著降低模块间的耦合。举个例子,如果需要增加一个开根号运算,只需在工厂类中添加一个分支,并新建一个开根号的运算类,已有的代码完全不受影响——这才是真正的“开闭原则”体现。你不必去修改任何一个现有的运算类,只需扩展新功能即可。
工厂方法模式
# -*- encoding: utf-8 -*- #运算类 class Operation attr_accessor :number_a,:number_b def initialize(number_a = nil, number_b = nil) @number_a = number_a @number_b = number_b end def result 0 end end #加法类 class OperationAdd < Operation def result number_a + number_b end end #减法类 class OperationSub < Operation def result number_a - number_b end end #乘法类 class OperationMul < Operation def result number_a * number_b end end #除法类 class OperationDiv < Operation def result raise '除数不能为0' if number_b == 0 number_a / number_b end end module FactoryModule def create_operation end end #加法工厂 class AddFactory include FactoryModule def create_operation OperationAdd.new end end #减法工厂 class SubFactory include FactoryModule def create_operation OperationSub.new end end #乘法工厂 class MulFactory include FactoryModule def create_operation OperationMul.new end end #除法工厂 class DivFactory include FactoryModule def create_operation OperationDiv.new end end factory = AddFactory.new oper = factory.create_operation oper.number_a = 1 oper.number_b = 2 p oper.result
相比于简单工厂模式,这里的变化是移除了统一的工厂类,取而代之的是多个具体的运算工厂:加法工厂、减法工厂、乘法工厂、除法工厂,每个工厂只负责生产一种运算对象。这样一来,新增一种运算就不再需要修改任何已有代码——只需增加对应的运算类和工厂类。从“改分支”进化到“加文件”,结构更清晰,也更符合单一职责原则。当然,如果项目规模不大,简单工厂模式的简洁性反而更占优势;而当业务复杂度上升,工厂方法模式的扩展性价值就会凸显出来。
免责声明:文中图文均来自网络,如有侵权请联系删除,心愿游戏发布此文仅为传递信息,不代表心愿游戏认同其观点或证实其描述。
相关文章
更多-
- 使用RVM切换Ruby与Rails版本的实现方法
- 时间:2026-08-18
-
- Ruby语言是什么及入门使用方法
- 时间:2026-08-18
-
- Ruby on Rails网站项目搭建入门指南
- 时间:2026-08-18
-
- Ruby插入排序算法实现与二路插入排序代码示例
- 时间:2026-08-18
-
- Ruby图片滤镜算法实现代码与核心原理
- 时间:2026-08-18
-
- Ruby中Hash哈希结构基本操作方法详解
- 时间:2026-08-18
-
- Ruby面向对象编程:类方法与类扩展详解
- 时间:2026-08-18
-
- Ruby正则表达式语法详解与常用示例代码
- 时间:2026-08-18
精选合集
更多大家都在玩
大家都在看
更多-
- 糖尿病完全不能吃糖吗
- 时间:2026-09-15
-
- 蚂蚁庄园小课堂2026年9月16日最新题目答案
- 时间:2026-09-15
-
- 小鸡答题今天的答案是什么2026年9月16日
- 时间:2026-09-15
-
- 蚂蚁庄园每日答题答案2026年9月16日
- 时间:2026-09-15
-
- 以下哪种粮食是酿造绍兴黄酒的主要原料 蚂蚁庄园今日答案9月16日
- 时间:2026-09-15
-
- 劝学名句“及时当勉励,岁月不待人”出自哪位诗人 蚂蚁庄园今日答案9.16
- 时间:2026-09-15
-
- 蚂蚁庄园今天答题答案2026年9月16日
- 时间:2026-09-15
-
- 蚂蚁庄园答题今日答案2026年9月16日
- 时间:2026-09-15
