国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Ruby > 正文

設計模式中的模板方法模式在Ruby中的應用實例兩則

2020-10-29 19:36:15
字體:
來源:轉載
供稿:網友

實例一
今天你還是像往常一樣來上班,一如既往地開始了你的編程工作。
項目經理告訴你,今天想在服務器端增加一個新功能,希望寫一個方法,能對Book對象進行處理,將Book對象的所有字段以XML格式進行包裝,這樣以后可以方便與客戶端進行交互。并且在包裝開始前和結束后要打印日志,這樣方便調試和問題定位。
沒問題!你覺得這個功能簡直是小菜一碟,非常自信地開始寫起代碼。
Book對象代碼如下:

class Book  attr_accessor :book_name, :pages, :price, :author, :isbn end 

然后寫一個類專門用于將Book對象包裝成XML格式:

class Formatter   def format_book(book)   puts "format begins"   result = "<book_name>#{book.book_name}</book_name>/n"   result += "<pages>#{book.pages}</pages>/n"   result += "<price>#{book.price}</price>/n"   result += "<author>#{book.author}</author>/n"   result += "<isbn>#{book.isbn}</isbn>/n"   puts "format finished"   result  end  end 

 
調用代碼如下:

book = Book.new book.book_name = "Programming Ruby" book.pages = 830 book.price = 45 book.author = "Dave Thomas" book.isbn = "9787121038150" formatter = Formatter.new result = formatter.format_book(book) puts result 

你寫好了之后,迫不及待地開始運行,運行結果也完全符合你的期望。

2016316160008180.png (357×125)

項目經理看完后,對你非常滿意,小伙效率很高的嘛!你也非常的得意。
不過兩天之后,項目經理又找到了你,他說之前沒有考慮到需要交互的客戶端還包括手機設備,而手機設備都比較吃流量,用XML格式來傳輸太耗流量了,想最好能改成使用JSON格式傳輸。但是之前的XML格式也要保留,最好可以由客戶端指定使用哪種格式。
你有些不開心,心里低估著,為什么一開始不考慮周全呢,現在又要改遺留代碼。但對方畢竟是領導,你還是要服從命令的,于是你開始修改Formatter類:

class Formatter   def format_book(book, format)   puts "format begins"   result = ""   if format == :xml    result += "<book_name>#{book.book_name}</book_name>/n"    result += "<pages>#{book.pages}</pages>/n"    result += "<price>#{book.price}</price>/n"    result += "<author>#{book.author}</author>/n"    result += "<isbn>#{book.isbn}</isbn>/n"   elsif format == :json    result += "{/n"    result += "/"book_name/" : /"#{book.book_name}/",/n"    result += "/"pages/" : /"#{book.pages}/",/n"    result += "/"price/" : /"#{book.price}/",/n"    result += "/"author/" : /"#{book.author}/",/n"    result += "/"isbn/" : /"#{book.isbn}/",/n"    result += '}'   end   puts "format finished"   result  end  end 

調用代碼如下:

book = Book.new book.book_name = "Programming Ruby" book.pages = 830 book.price = 45 book.author = "Dave Thomas" book.isbn = "9787121038150" formatter = Formatter.new result = formatter.format_book(book, :xml) puts result result = formatter.format_book(book, :json) puts result 

再次運行程序,得到了以下結果。

2016316160047870.png (358×292)

項目經理看到運行結果后開心地說:“太好了,這正是我想要的!”
可是你這次卻沒有那么開心,你覺得代碼已經有些混亂了,XML格式的邏輯和JSON格式的邏輯混淆在一起,非常不利于閱讀,而且如果以后還需要擴展功能也會非常困難。好在傳輸格式一般也就XML和JSON了,應該不會再有什么擴展了,你這樣安慰自己道。
但幻想總會被現實打破,“我最近聽說有個YAML格式挺好玩的.......” 項目經理說道。這個時候你已經有想打人的沖動了!!!

很多時候就是這樣,在公司里寫的代碼亂七八糟,質量極差,很大一部分原因就是因為需求變來變去。我們不斷在原有代碼基礎上補充各種后續加入的情況,在一行行新增的if語句下面,我們的代碼變得不堪入目。當然,我們作為程序員,對于需求這種東西沒有太多的話語權,在這方面我們無能為力。但是我們可以盡量地把程序的架構設計好,讓我們寫出的代碼更具有擴展性,這樣就可以應對各種需求變更了。

下面你將要使用23種設計模式中的模板方法來改進以上程序。
首先要定義專門的子類來處理每種傳輸格式的具體邏輯,這樣不同傳輸格式的邏輯可以從一個方法里分離開,明顯便于閱讀和理解。
定義類XMLFormatter繼承自Formatter,里面加入處理XML格式的具體邏輯:

class XMLFormatter < Formatter   def formating(book)   result = "<book_name>#{book.book_name}</book_name>/n"   result += "<pages>#{book.pages}</pages>/n"   result += "<price>#{book.price}</price>/n"   result += "<author>#{book.author}</author>/n"   result += "<isbn>#{book.isbn}</isbn>/n"  end  end 

定義類JSONFormatter繼承自Formatter,里面加入處理JSON格式的具體邏輯:

class JSONFormatter < Formatter    def formating(book)   result = "{/n"   result += "/"book_name/" : /"#{book.book_name}/",/n"   result += "/"pages/" : /"#{book.pages}/",/n"   result += "/"price/" : /"#{book.price}/",/n"   result += "/"author/" : /"#{book.author}/",/n"   result += "/"isbn/" : /"#{book.isbn}/",/n"   result += '}'  end   end 

然后將Formatter中的代碼進行修改,如下所示:

class Formatter   def format_book(book)   before_format   result = formating(book)   after_format   result  end    def before_format   puts "format begins"  end    def formating(book)   raise "You should override this method in subclass."  end    def after_format   puts "format finished"  end  end 

你會發現format_book方法只有四步,第一步調用before_format,去打印格式轉換前的日志。第二步調用formating,處理具體的轉換邏輯,但是formating方法中只是raise了一個異常,因為具體的轉換的邏輯應該由子類來處理,如果走到了父類的formating方法中,就說明應該出現異常。第三步調用after_format,去打印格式轉換后的日志。第四步返回result。
最后調用代碼如下:

book = Book.new book.book_name = "Programming Ruby" book.pages = 830 book.price = 45 book.author = "Dave Thomas" book.isbn = "9787121038150" xmlFormatter = XMLFormatter.new result = xmlFormatter.format_book(book) puts result jsonFormatter = JSONFormatter.new result = jsonFormatter.format_book(book) puts result 

運行之后,你會發現運行結果和修改前代碼的運行結果完全相同。但是使用模板方法之后,代碼的可讀性有了很大的提高,因為處理格式轉換的代碼都放到了各自的類當中,而不是全部塞進一個方法中。并且在擴展性上也有了很大的提升,比如你開始感興趣項目經理說的YAML格式了。
定義類YAMLFormatter繼承自Formatter,里面加入處理YAML格式的具體邏輯:

class YAMLFormatter < Formatter   def formating(book)   result = "book_name: #{book.book_name}/n"   result += "pages: #{book.pages}/n"   result += "price: #{book.price}/n"   result += "author: #{book.author}/n"   result += "isbn: #{book.isbn}/n"  end  end 

調用代碼只需要加入:

yamlFormatter = YAMLFormatter.new result = yamlFormatter.format_book(book) puts result 

好了,令人頭疼的YAML格式就這樣被支持了,只需要在調用的時候決定是實例化XMLFormatter,JSONFormatter還是YAMLFormatter,就可以按照相應的規格進行格式轉換了。而且整體的代碼很有條理,看起來也很舒心。這個時候,你會輕松地向項目經理調侃一句,還有需要支持的格式嗎?

實例二

需求:

學生抄題目,做題目

初始代碼

# -*- encoding: utf-8 -*-#學生甲的試卷類class TestPaperA  def question1  puts '楊過得到,后來給了郭靖,煉成倚天劍,屠龍刀的玄鐵可能是[] a.球墨鑄鐵 b.馬口鐵 c.高速合金鋼 d.碳塑纖維 '  puts '答案:b' end  def question2  puts '楊過、程英、陸無雙鏟除了情花,造成了[] a.使這種植物不再害人 b.使一種珍稀物種滅絕 c.破壞了那個生物圈的生態平衡 d.造成該地區沙漠化 '  puts '答案:a' end  def question3  puts '藍鳳凰的致使華山師徒、桃谷六仙嘔吐不止,如果你是大夫,會給他們開什么藥[] a.阿司匹林 b.牛黃解毒片 c.氟酸 d.讓他們喝大量的生牛奶 e.以上全不對'  puts '答案:c' endend#學生乙的試卷類class TestPaperB  def question1  puts '楊過得到,后來給了郭靖,煉成倚天劍,屠龍刀的玄鐵可能是[] a.球墨鑄鐵 b.馬口鐵 c.高速合金鋼 d.碳塑纖維 '  puts '答案:d' end  def question2  puts '楊過、程英、陸無雙鏟除了情花,造成了[] a.使這種植物不再害人 b.使一種珍稀物種滅絕 c.破壞了那個生物圈的生態平衡 d.造成該地區沙漠化 '  puts '答案:b' end  def question3  puts '藍鳳凰的致使華山師徒、桃谷六仙嘔吐不止,如果你是大夫,會給他們開什么藥[] a.阿司匹林 b.牛黃解毒片 c.氟酸 d.讓他們喝大量的生牛奶 e.以上全不對'  puts '答案:a' endendputs '學生甲抄的試卷'student1 = TestPaperA.newstudent1.question1student1.question2student1.question3puts '學生乙抄的試卷'student2 = TestPaperB.newstudent2.question1student2.question2student2.question3

存在的問題:

TestPaperA和TestPaperB中的代碼很多相同的地方,不利于維護,如果需要修改題目的話,就要改兩處
改后的代碼

# -*- encoding: utf-8 -*-class TestPaper def question1  puts '楊過得到,后來給了郭靖,煉成倚天劍,屠龍刀的玄鐵可能是[] a.球墨鑄鐵 b.馬口鐵 c.高速合金鋼 d.碳塑纖維 ' end  def question2  puts '楊過、程英、陸無雙鏟除了情花,造成了[] a.使這種植物不再害人 b.使一種珍稀物種滅絕 c.破壞了那個生物圈的生態平衡 d.造成該地區沙漠化 ' end  def question3  puts '藍鳳凰的致使華山師徒、桃谷六仙嘔吐不止,如果你是大夫,會給他們開什么藥[] a.阿司匹林 b.牛黃解毒片 c.氟酸 d.讓他們喝大量的生牛奶 e.以上全不對' endend#學生甲的試卷類class TestPaperA < TestPaper  def question1  super  puts '答案:b' end  def question2  super  puts '答案:a' end  def question3  super  puts '答案:c' endend#學生乙的試卷類class TestPaperB < TestPaper  def question1  super  puts '答案:d' end  def question2  super  puts '答案:b' end  def question3  super  puts '答案:a' endendputs '學生甲抄的試卷'student1 = TestPaperA.newstudent1.question1student1.question2student1.question3puts '學生乙抄的試卷'student2 = TestPaperB.newstudent2.question1student2.question2student2.question3

可以看出,抽取出來一個公共的試卷類,讓甲乙去繼承,公用其中的試題。這時再看TestPaperA和TestPaperB,不同的只有答案a、b、c、d不一樣,其他的都一樣。

# -*- encoding: utf-8 -*-class TestPaper def question1  puts '楊過得到,后來給了郭靖,煉成倚天劍,屠龍刀的玄鐵可能是[] a.球墨鑄鐵 b.馬口鐵 c.高速合金鋼 d.碳塑纖維 '  puts "答案:#{answer1}" end  def question2  puts '楊過、程英、陸無雙鏟除了情花,造成了[] a.使這種植物不再害人 b.使一種珍稀物種滅絕 c.破壞了那個生物圈的生態平衡 d.造成該地區沙漠化 '  puts "答案:#{answer2}" end  def question3  puts '藍鳳凰的致使華山師徒、桃谷六仙嘔吐不止,如果你是大夫,會給他們開什么藥[] a.阿司匹林 b.牛黃解毒片 c.氟酸 d.讓他們喝大量的生牛奶 e.以上全不對'  puts "答案:#{answer3}" end def answer1; end def answer2; end def answer3; endend#學生甲的試卷類class TestPaperA < TestPaper  def answer1  'b' end  def answer2  'a' end  def answer3  'c' endend#學生乙的試卷類class TestPaperB < TestPaper  def answer1  'd' end  def answer2  'b' end  def answer3  'a' endendputs '學生甲抄的試卷'student1 = TestPaperA.newstudent1.question1student1.question2student1.question3puts '學生乙抄的試卷'student2 = TestPaperB.newstudent2.question1student2.question2student2.question3

這里將TestPaperA和TestPaperB中的答案抽離到了父類中,僅僅保存不同的部分。

父類成為子類的模板,所有重復的代碼都應該上升到父類去,而不是讓每個子類都去重復。

當我們要完成在某一細節層次一致的過程或一系列步驟,但其個別步驟在更詳細層次上的實現可能不同時,我們通常考慮使用模板方法模式來處理。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 定西市| 宜丰县| 克拉玛依市| 永平县| 确山县| 保靖县| 永靖县| 兴安县| 金阳县| 额尔古纳市| 分宜县| 张家港市| 洪湖市| 汤阴县| 荥经县| 金湖县| 珠海市| 河北区| 珠海市| 余干县| 托克托县| 石河子市| 孟村| 迁安市| 和林格尔县| 丽江市| 田林县| 长宁区| 北辰区| 奎屯市| 弥渡县| 吴忠市| 凤山县| 甘谷县| 莆田市| 阳山县| 南木林县| 游戏| 大足县| 曲麻莱县| 乐山市|