Ruby中使用設(shè)計(jì)模式中的簡(jiǎn)單工廠模式和工廠方法模式
之前有看過(guò)《ruby設(shè)計(jì)模式》,不過(guò)漸漸的都忘記了?,F(xiàn)在買了一個(gè)大話設(shè)計(jì)模式,看起來(lái)不是那么枯燥,順便將代碼用ruby實(shí)現(xiàn)了一下。
簡(jiǎn)單工廠模式:
# -*- encoding: utf-8 -*- #運(yùn)算類 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 '除數(shù)不能為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
這樣寫的好處是降低耦合。
比如增加一個(gè)開(kāi)根號(hào)運(yùn)算的時(shí)候,只需要在工廠類中添加一個(gè)分支,并新建一個(gè)開(kāi)根號(hào)類,不會(huì)去動(dòng)到其他的類。
工廠方法模式:
# -*- encoding: utf-8 -*- #運(yùn)算類 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 '除數(shù)不能為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
相比于簡(jiǎn)單工廠模式,這里的變化是移除了工廠類,取而代之的是具體的運(yùn)算工廠,分別是加法工廠、減法工廠、乘法工廠和除法工廠。
- 設(shè)計(jì)模式中的觀察者模式在Ruby編程中的運(yùn)用實(shí)例解析
- 實(shí)例解析Ruby設(shè)計(jì)模式開(kāi)發(fā)中對(duì)觀察者模式的實(shí)現(xiàn)
- 深入剖析Ruby設(shè)計(jì)模式編程中對(duì)命令模式的相關(guān)使用
- Ruby設(shè)計(jì)模式編程中對(duì)外觀模式的應(yīng)用實(shí)例分析
- 詳解組合模式的結(jié)構(gòu)及其在Ruby設(shè)計(jì)模式編程中的運(yùn)用
- 設(shè)計(jì)模式中的模板方法模式在Ruby中的應(yīng)用實(shí)例兩則
- 實(shí)例解析Ruby設(shè)計(jì)模式編程中Strategy策略模式的使用
- 實(shí)例講解Ruby使用設(shè)計(jì)模式中的裝飾器模式的方法
- Ruby設(shè)計(jì)模式編程中使用Builder建造者模式的實(shí)例
- 詳解Ruby設(shè)計(jì)模式編程中對(duì)單例模式的運(yùn)用
- Ruby設(shè)計(jì)模式編程之適配器模式實(shí)戰(zhàn)攻略
- Ruby使用設(shè)計(jì)模式中的代理模式與裝飾模式的代碼實(shí)例
- 解析proxy代理模式在Ruby設(shè)計(jì)模式開(kāi)發(fā)中的運(yùn)用
相關(guān)文章
CentOS7下搭建ruby on rails開(kāi)發(fā)環(huán)境
聽(tīng)說(shuō)rails是一個(gè)比較流行的快速開(kāi)發(fā)框架,對(duì)于我這個(gè)web不熟悉的人來(lái)說(shuō),那是極好的!可以快速上手,又能真正了解服務(wù)器端的各種,所以rails搞起來(lái)。不過(guò)一個(gè)完整的開(kāi)發(fā)環(huán)境搭建過(guò)程完成后,真的只能用各種坑來(lái)形容~2016-02-02使用Ruby on Rails快速開(kāi)發(fā)web應(yīng)用的教程實(shí)例
這篇文章主要介紹了使用Ruby on Rails快速開(kāi)發(fā)web應(yīng)用的教程實(shí)例,本文來(lái)自于IBM官方技術(shù)文檔,需要的朋友可以參考下2015-04-04ruby寫掃描當(dāng)前網(wǎng)頁(yè)所有url的腳本
用ruby寫的掃描當(dāng)前網(wǎng)頁(yè)所有url的腳本,對(duì)于學(xué)習(xí)ruby的朋友是個(gè)不錯(cuò)的實(shí)例2008-06-06深入剖析Ruby設(shè)計(jì)模式編程中對(duì)命令模式的相關(guān)使用
這篇文章主要介紹了Ruby設(shè)計(jì)模式編程中對(duì)命令模式的相關(guān)使用,文中還講到了關(guān)于觀察者模式和命令模式的一些概念區(qū)別,需要的朋友可以參考下2016-04-04冒泡排序算法及Ruby版的簡(jiǎn)單實(shí)現(xiàn)
冒泡排序?yàn)樽罨镜呐判蛩惴ㄖ?其時(shí)間復(fù)雜度為O(n^2),這里我們就來(lái)簡(jiǎn)單看一下冒泡排序算法及Ruby版的簡(jiǎn)單實(shí)現(xiàn),首先還是先來(lái)了解算法原理:2016-05-05