校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > Ruby中使用設計模式中的簡單工廠模式和工廠方法模式

Ruby中使用設計模式中的簡單工廠模式和工廠方法模式

熱門標簽:百度地圖標注自定義圖片 常德電銷平臺外呼系統軟件價格 滴滴外呼系統 電銷機器人廠商代理 地圖標注賺錢項目注冊 徐州網絡外呼系統哪個好 白銀外呼paas系統 高德地圖標注客服 湖州u友防封電銷卡

之前有看過《ruby設計模式》,不過漸漸的都忘記了?,F在買了一個大話設計模式,看起來不是那么枯燥,順便將代碼用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設計模式開發中對觀察者模式的實現
  • 深入剖析Ruby設計模式編程中對命令模式的相關使用
  • Ruby設計模式編程中對外觀模式的應用實例分析
  • 詳解組合模式的結構及其在Ruby設計模式編程中的運用
  • 設計模式中的模板方法模式在Ruby中的應用實例兩則
  • 實例解析Ruby設計模式編程中Strategy策略模式的使用
  • 實例講解Ruby使用設計模式中的裝飾器模式的方法
  • Ruby設計模式編程中使用Builder建造者模式的實例
  • 詳解Ruby設計模式編程中對單例模式的運用
  • Ruby設計模式編程之適配器模式實戰攻略
  • Ruby使用設計模式中的代理模式與裝飾模式的代碼實例
  • 解析proxy代理模式在Ruby設計模式開發中的運用

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

巨人網絡通訊聲明:本文標題《Ruby中使用設計模式中的簡單工廠模式和工廠方法模式》,本文關鍵詞  Ruby,中,使用,設計模式,中的,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Ruby中使用設計模式中的簡單工廠模式和工廠方法模式》相關的同類信息!
  • 本頁收集關于Ruby中使用設計模式中的簡單工廠模式和工廠方法模式的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 阿巴嘎旗| 白河县| 全南县| 山东| 云梦县| 得荣县| 永善县| 吉林市| 漾濞| 疏附县| 临颍县| 吉安县| 德保县| 蓝田县| 高唐县| 榆社县| 石首市| 长治市| 菏泽市| 寿光市| 江城| 读书| 琼中| 通许县| 新田县| 垫江县| 舞阳县| 大石桥市| 湘阴县| 武乡县| 三门峡市| 奈曼旗| 金山区| 江孜县| 鄂尔多斯市| 白城市| 新宾| 唐山市| 正阳县| 曲阜市| 扬州市|