濮阳杆衣贸易有限公司

主頁 > 知識庫 > Ruby中使用設(shè)計(jì)模式中的簡單工廠模式和工廠方法模式

Ruby中使用設(shè)計(jì)模式中的簡單工廠模式和工廠方法模式

熱門標(biāo)簽:百度地圖標(biāo)注自定義圖片 常德電銷平臺外呼系統(tǒng)軟件價格 滴滴外呼系統(tǒng) 電銷機(jī)器人廠商代理 地圖標(biāo)注賺錢項(xiàng)目注冊 徐州網(wǎng)絡(luò)外呼系統(tǒng)哪個好 白銀外呼paas系統(tǒng) 高德地圖標(biāo)注客服 湖州u友防封電銷卡

之前有看過《ruby設(shè)計(jì)模式》,不過漸漸的都忘記了?,F(xiàn)在買了一個大話設(shè)計(jì)模式,看起來不是那么枯燥,順便將代碼用ruby實(shí)現(xià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

這樣寫的好處是降低耦合。
比如增加一個開根號運(yù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


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

相比于簡單工廠模式,這里的變化是移除了工廠類,取而代之的是具體的運(yùn)算工廠,分別是加法工廠、減法工廠、乘法工廠和除法工廠。

您可能感興趣的文章:
  • 設(shè)計(jì)模式中的觀察者模式在Ruby編程中的運(yùn)用實(shí)例解析
  • 實(shí)例解析Ruby設(shè)計(jì)模式開發(fā)中對觀察者模式的實(shí)現(xiàn)
  • 深入剖析Ruby設(shè)計(jì)模式編程中對命令模式的相關(guān)使用
  • Ruby設(shè)計(jì)模式編程中對外觀模式的應(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ì)模式編程中對單例模式的運(yùn)用
  • Ruby設(shè)計(jì)模式編程之適配器模式實(shí)戰(zhàn)攻略
  • Ruby使用設(shè)計(jì)模式中的代理模式與裝飾模式的代碼實(shí)例
  • 解析proxy代理模式在Ruby設(shè)計(jì)模式開發(fā)中的運(yùn)用

標(biāo)簽:梧州 三沙 永州 遼寧 荊門 公主嶺 張家界 普洱

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Ruby中使用設(shè)計(jì)模式中的簡單工廠模式和工廠方法模式》,本文關(guān)鍵詞  Ruby,中,使用,設(shè)計(jì)模式,中的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Ruby中使用設(shè)計(jì)模式中的簡單工廠模式和工廠方法模式》相關(guān)的同類信息!
  • 本頁收集關(guān)于Ruby中使用設(shè)計(jì)模式中的簡單工廠模式和工廠方法模式的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    永定县| 疏附县| 射洪县| 吕梁市| 岢岚县| 上饶县| 常熟市| 突泉县| 抚顺县| 皋兰县| 兴宁市| 寿光市| 镇坪县| 深圳市| 彰武县| 东乡县| 巴青县| 民县| 北流市| 手机| 朝阳区| 南乐县| 马山县| 永川市| 华容县| 碌曲县| 河津市| 奉贤区| 凌海市| 梅河口市| 通河县| 潞西市| 登封市| 时尚| 洛阳市| 和政县| 遂川县| 瑞丽市| 广宗县| 临汾市| 屯留县|