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

所属分类: 脚本专栏 / ruby专题 阅读数: 1784
收藏 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里面有4种比较方法,equal?, eql?, ==, ===,而且在不同的类里面表现的很不一样。在使用的时候也特别容易搞糊涂。 本文先给大家讲述一下==号的用法及使用中应该注意的地方
收藏 0 赞 0 分享

Ruby里4种比较函数(equal?, eql?, ==, ===)详解

本文给大家详细介绍了Ruby中的4种比较函数(equal?, eql?, ==, ===)的用法,并用具体示例进行了讲解,希望对大家学习ruby能够有所帮助。
收藏 0 赞 0 分享

Ruby on Rails在Ping ++ 平台实现支付

本文给大家分享的是使用Ruby on Rails在Ping ++ 平台实现支付功能的代码,非常的实用,有需要的小伙伴可以参考下。
收藏 0 赞 0 分享

Ruby on Rails基础之新建项目

Ruby on Rails 是一个可以使你开发,部署,维护 web 应用程序变得简单的框架。下面我们就来看看如何简单便捷的使用这一框架,本系列文章将一一为大家揭秘
收藏 0 赞 0 分享

Ruby语法笔记

本文给大家记录的是本人学习ruby之后所记录下来的部分语法知识,分享给有需要的小伙伴,希望对大家能够有所帮助。
收藏 0 赞 0 分享

Ruby的安装与运行

本文给大家分享的是ruby的基础知识,是学习ruby必须掌握的ruby的安装和运行以及ruby的文档,非常实用,有需要的小伙伴可以参考下。
收藏 0 赞 0 分享

CentOS7下搭建ruby on rails开发环境

听说rails是一个比较流行的快速开发框架,对于我这个web不熟悉的人来说,那是极好的!可以快速上手,又能真正了解服务器端的各种,所以rails搞起来。不过一个完整的开发环境搭建过程完成后,真的只能用各种坑来形容~
收藏 0 赞 0 分享

Windows下安装配置Ruby的debug工具ruby-debug-base19

这篇文章主要介绍了Windows下安装配置Ruby的debug工具ruby-debug-base19的方法,同时讲解了Ruby的IDE RubyMine中的相关配置方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Windows下Ruby+Watir自动化测试的环境搭建及数据读取

这篇文章主要介绍了Windows下Ruby+Watir自动化测试的环境搭建及数据读取,Watir是一个使用Ruby实现的开源Web自动化测试框架,需要的朋友可以参考下
收藏 0 赞 0 分享

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

这篇文章主要介绍了Ruby中使用设计模式中的简单工厂模式和工厂方法模式的示例,这两种模式经常被用于Ruby on Rails开发的结构设计中,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多