脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Ruby - Ruby中使用设计模式中的简单工厂模式和工厂方法模式

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

2020-05-07 11:07范孝鹏 Ruby

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

之前有看过《ruby设计模式》,不过渐渐的都忘记了。现在买了一个大话设计模式,看起来不是那么枯燥,顺便将代码用ruby实现了一下。

简单工厂模式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- 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

这样写的好处是降低耦合。
比如增加一个开根号运算的时候,只需要在工厂类中添加一个分支,并新建一个开根号类,不会去动到其他的类。

工厂方法模式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- 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 on Rails进行高效的单元测试的教程

    对Ruby on Rails进行高效的单元测试的教程

    这篇文章主要介绍了在Ruby on Rails中进行高效的单元测试的教程,使用到了Ruby的RSpec和Factory Girl框架,需要的朋友可以参考下 ...

    李冠德3562020-04-26
  • RubyRuby中对一元操作符重载实例

    Ruby中对一元操作符重载实例

    这篇文章主要介绍了Ruby中对一元操作符重载实例,实例说明如何对一元操作符进行重载,需要的朋友可以参考下 ...

    junjie1852020-04-14
  • RubyRuby编程中关于中断和返回的用法教程

    Ruby编程中关于中断和返回的用法教程

    这篇文章主要介绍了Ruby编程中关于中断和返回的用法教程,作者用代码举例讲解了其中需要注意的问题,需要的朋友可以参考下 ...

    李哲3142020-04-27
  • RubyRuby on Rails所构建的应用程序基本目录结构总结

    Ruby on Rails所构建的应用程序基本目录结构总结

    Ruby on Rails是Ruby世界中一家独大的Web开发框架,要掌握Rails程序的构建,对其目录结构的了解十分必要,下面就来看一下Ruby on Rails所构建的应用程序基本目录结...

    kevinhua4002020-05-09
  • Rubyruby实现修改ubuntu下的hosts

    ruby实现修改ubuntu下的hosts

    本文给大家分享的是通过ruby获取github上的hosts文件内容,修改到本地Ubuntu中,十分的实用,具体你懂得,有需要的小伙伴可以参考下。 ...

    脚本之家3882020-05-03
  • RubyWindows下ruby语言安装教程

    Windows下ruby语言安装教程

    这篇文章主要介绍了Windows下ruby语言安装教程,本文使用rubyinstaller提供的安装包安装,并给出图文说明,非常简单,需要的朋友可以参考下 ...

    脚本之家5522020-04-22
  • RubyRuby学习笔记之gem 命令详解

    Ruby学习笔记之gem 命令详解

    gem是一种文件组织的包,一般的ruby的很多插件都有由这种各种的包提供。我们来看看gem的用法 ...

    hebedich8662020-04-16
  • Ruby深入讲解Ruby中Block代码块的用法

    深入讲解Ruby中Block代码块的用法

    这篇文章主要介绍了深入讲解Ruby中Block代码块的用法,block是Ruby学习进阶当中的重要知识,需要的朋友可以参考下 ...

    pringwq4582020-04-27