When we have a complex algorithm including several steps to build something, which would be vary in the middle.
Now you need Template method.
It comes up with an example:
class Report
def initialize
@title = 'Monthly Report'
@text = [ 'Things are going', 'really, really well.' ]
end
def output_report
puts('<html>')
puts(' <head>')
puts(" <title>#{@title}</title>")
puts(' </head>')
puts(' <body>')
@text.each do |line|
puts(" <p>#{line}</p>" )
end
puts(' </body>')
puts('</html>')
end
end
The usage of the class is as below:
report = Report.new
report.output_report
When they need something more? It will support plain text or RTF or PostScript.
The code will mess up with many if ... elsif ... else conditions
So, let's do the seperate thing:
Separate the Things That Stay the Same
class Report
def initialize
@title = 'Monthly Report'
@text = ['Things are going', 'really, really well.']
end
def output_report
output_start
output_head
output_body_start
output_body
output_body_end
output_end
end
def output_body
@text.each do |line|
output_line(line)
end
end
def output_start
raise 'Called abstract method: output_start'
end
def output_head
raise 'Called abstract method: output_head'
end
def output_body_start
raise 'Called abstract method: output_body_start'
end
def output_line(line)
raise 'Called abstract method: output_line'
end
def output_body_end
raise 'Called abstract method: output_body_end'
end
def output_end
raise 'Called abstract method: output_end'
end
end
class HTMLReport < Report
def output_start
puts('<html>')
end
def output_head
puts(' <head>')
puts(" <title>#{@title}</title>")
puts(' </head>')
end
def output_body_start
puts('<body>')
end
def output_line(line)
puts(" <p>#{line}</p>")
end
def output_body_end
puts('</body>')
end
def output_end
puts('</html>')
end
end
class PlainTextReport < Report
def output_start
end
def output_head
puts("**** #{@title} ****")
puts
end
def output_body_start
end
def output_line(line)
puts(line)
end
def output_body_end
end
def output_end
end
end
Then the usage is really straight:
report = HTMLReport.new
report.output_report
report = PlainTextReport.new
report.output_report
Ruby doesn't support abstract methods and abstract classes, then we can use the methods which raise exception meet our demand.
"In the Template Method pattern, the abstract base class controls the higher-level processing through the template method; the subclasses simply fill in the details."
Then it goes to hook methods. but really, I don't agree with the writer's opinion about the example
Report with the default method implementations. Maybe the default implementations will brought in some wrong format if the coder obmit to implement some methods, such as missing the output_start or output_end or something context related.
So maybe we should implement the must be right thing for the common abstract class.
Lazziness must be secondary in front of correctness
Then we go to duck typing issues:
If it looks like a duck and quacks like a duck, then it is a duck~
The statically typed languages are working like aristocracies, they always ask about your genealogy.
The dynamically typed language are working like meritocracies, they only concern about what you have rather than where do you get the methods from.
The writer wrote it at the end of the part~
Dynamically typed languages rarely ask about an object’s ancestry; instead, they simply say, “I don’t care who you are related to, Mac. All I want to know is what you can do.”
分享到:
相关推荐
Design Patterns in Ruby, Addison-wesley (2008). The Addison-Wesley Professional Ruby Series provides readers with practical, people-oriented, and in-depth information about applying the Ruby platform ...
Design Patterns in C# A Hands-on Guide with Real-World Examples的例子源码 注意: 所有例子没有经过上机运行验证。
Apply design patterns to modern C++ programming Use creational patterns of builder, factories, prototype and singleton Implement structural patterns such as adapter, bridge, decorator, facade and ...
Design Patterns in Modern C++.pdf
The topic of Design Patterns sounds dry, academically constipated and, in all honesty, done to death in almost every programming language imaginable—including programming languages such as JavaScript...
Addison.Wesley.Design.Patterns.in.Ruby.Dec.2007 高清PDF英文版
关于设计模式的英文教材,包含详细例子,值得一读
design patterns elements of reusable object-oriented software.pdf 经典的书籍,比较完美的版本了
Design Patterns in Ruby Dec 2007.rar
Design Patterns Elements of Reusable Object-Oriented Software 英文版 (chm)
Design Patterns: Elements of Reusable Object-Oriented Software.doc (无水印)
Practical-Python-Design-Patterns-Pythonic-Solutions-to-Common-Problems.pdf
Implement the design patterns in real-world applications Select an alternative to these patterns by comparing their pros and cons Use Visual Studio Community Edition 2017 to write code and generate...
Module 2, Go Design Patterns, will provide readers with a reference point to software design patterns and CSP concurrency design patterns to help them build applications in a more idiomatic, robust,...
GOF-设计模式-Design Patterns-英文原版-高清-有目录-有页码
• Design Patterns in Smalltalk MVC • Describing Design Patterns • The Catalog of Design Patterns • Organizing the Catalog • How Design Patterns Solve Design Problems • How to Select a ...
Design_Patterns Elements of Reusable Object-Oriented Software
Learning Python Design Patterns A practical and fast-paced guide exploring Python design patterns 作者:Gennadiy Zlobin 包含 pdf, mobi, epub 三种格式和代码
Java codes to study patterns