当前位置: 首页 > 编程笔记 >

Ruby中使用设计模式中的简单工厂模式和工厂方法模式

陶涵育
2023-03-14
本文向大家介绍Ruby中使用设计模式中的简单工厂模式和工厂方法模式,包括了Ruby中使用设计模式中的简单工厂模式和工厂方法模式的使用技巧和注意事项,需要的朋友参考一下

之前有看过《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

相比于简单工厂模式,这里的变化是移除了工厂类,取而代之的是具体的运算工厂,分别是加法工厂、减法工厂、乘法工厂和除法工厂。

 类似资料:
  • 我正在学习新的设计模式 我编写了一个简单的工厂类,如下所示 我们创建Factory类,如下所示: 现在,当客户端想要添加名为IceCream的新项目时,他们只需创建名为IceCreamFactory的新工厂并从中创建IceCream,如下所示: 我的理解正确吗?我们在这里满足了开闭原则,但对于每个产品(项目),我们都需要一个工厂类,这不是一个可管理的噩梦吗? 注:我指的是一篇文章https://w

  • 本文向大家介绍Java设计模式之工厂模式分析【简单工厂、工厂方法、抽象工厂】,包括了Java设计模式之工厂模式分析【简单工厂、工厂方法、抽象工厂】的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Java设计模式之工厂模式。分享给大家供大家参考,具体如下: 一、 简单工厂 先来思考一个问题。我们平时写程序时,会有这种情况,A对象里面需要调用B对象的方法,这时我们使用的一般是new关键字来创建

  • 简单工厂(Simple Factory) Intent 在创建一个对象时不向客户暴露内部细节,并提供一个创建对象的通用接口。 Class Diagram 简单工厂把实例化的操作单独放到一个类中,这个类就成为简单工厂类,让简单工厂类来决定应该用哪个具体子类来实例化。 这样做能把客户类和具体子类的实现解耦,客户类不再需要知道有哪些子类以及应当实例化哪个子类。客户类往往有多个,如果不使用简单工厂,那么所

  • 本文向大家介绍Java设计模式编程中的工厂方法模式和抽象工厂模式,包括了Java设计模式编程中的工厂方法模式和抽象工厂模式的使用技巧和注意事项,需要的朋友参考一下 工厂方法模式 动机 创建一个对象往往需要复杂的过程,所以不适合包含在一个复合工厂中,当有新的产品时,需要修改这个复合的工厂,不利于扩展。 而且,有些对象的创建可以需要用到复合工厂访问不到的信息,所以,定义一个工厂接口,通过实现这个接口来

  • 本文向大家介绍java设计模式之简单工厂模式,包括了java设计模式之简单工厂模式的使用技巧和注意事项,需要的朋友参考一下 在编写一个计算器程序时,可以将业务逻辑和显示分离,业务逻辑封装为一个类(封装);如果要新添加一种运算,可以先创建一个Operation的基类,然后各种运算从Operation类继承,并实现GetResult()虚函数,这时添加新的运算只需要派生一个新的类,即不需要之前的运算参

  • 工厂方法(Factory Method) Intent 定义了一个创建对象的接口,但由子类决定要实例化哪个类。工厂方法把实例化操作推迟到子类。 Class Diagram 在简单工厂中,创建对象的是另一个类,而在工厂方法中,是由子类来创建对象。 下图中,Factory 有一个 doSomething() 方法,这个方法需要用到一个产品对象,这个产品对象由 factoryMethod() 方法创建。