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

所属分类: 脚本专栏 / ruby专题 阅读数: 1776
收藏 0 赞 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

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

更多精彩内容其他人还在看

Ruby 面向对象知识总结

这篇文章主要介绍了Ruby 面向对象的的相关资料,文中讲解非常详细,实例代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

Ruby中任务构建工具rake的入门学习教程

这篇文章主要介绍了Ruby中任务构建工具rake的入门学习教程,讲解了包括命名空间和默认任务的执行等基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

Ruby程序中发送基于HTTP协议的请求的简单示例

这篇文章主要介绍了Ruby程序中发送基于HTTP协议的请求的简单示例,包括对HTTPS请求的介绍,需要的朋友可以参考下
收藏 0 赞 0 分享

Ruby的字符串与数组求最大值的相关问题讨论

这篇文章主要介绍了Ruby中的字符串与数组求最大值的相关问题,文中还提到了sort排序方法的相关用法,需要的朋友可以参考下
收藏 0 赞 0 分享

Ruby中Time对象的常用函数总结

这篇文章主要介绍了Ruby中Time对象的常用函数总结,包括Ruby中一些实用的时间算法,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Ruby中正则表达式对字符串的匹配和替换操作

这篇文章主要介绍了Ruby中正则表达式对字符串的匹配和替换操作,包括对结果分组和一些特殊全局变量的介绍,需要的朋友可以参考下
收藏 0 赞 0 分享

深入剖析Ruby设计模式编程中对命令模式的相关使用

这篇文章主要介绍了Ruby设计模式编程中对命令模式的相关使用,文中还讲到了关于观察者模式和命令模式的一些概念区别,需要的朋友可以参考下
收藏 0 赞 0 分享

实例解析Ruby设计模式开发中对观察者模式的实现

这篇文章主要介绍了实例解析Ruby设计模式开发中对观察者模式的实现,Ruby中自带的observer类自然是绝佳的使用示例,需要的朋友可以参考下
收藏 0 赞 0 分享

设计模式中的观察者模式在Ruby编程中的运用实例解析

这篇文章主要介绍了设计模式中的观察者模式在Ruby编程中的运用实例解析,观察者模式中主张设立观察者对象来降低对象之间的耦合,需要的朋友可以参考下
收藏 0 赞 0 分享

解析proxy代理模式在Ruby设计模式开发中的运用

这篇文章主要介绍了proxy代理模式在Ruby设计模式开发中的运用,通过代理模式以客户透明的形式可以动态地为目标对象加以行为控制,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多