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.”
分享到:
相关推荐
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 ...
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英文版
2. 单例模式(Singleton Pattern):确保一个类只有一个实例,并提供一个全局访问点,常用于配置中心、缓存管理等场景。 3. 建造者模式(Builder Pattern):将复杂对象的构建与其表示分离,使得同样的构建过程可以...
又一本设计模式神书本书共有14章,每章都介绍了几个设计模式,完整地涵盖了四人组版本全部23个设计模式。 ★前言介绍了这本书的用法 ★第1章至第11章陆续介绍了设计模式:Strategy、Observer、Decorator、Abstract ...
design patterns elements of reusable object-oriented software.pdf 经典的书籍,比较完美的版本了
Design Patterns Elements of Reusable Object-Oriented Software 英文版 (chm)
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-design-patterns-master”显然是一个专注于Java设计模式的学习资源,旨在帮助开发者深入理解和应用这些模式。下面我们将详细探讨Java设计模式及其在实际开发中的应用。 1. **单例模式(Singleton...
Design Patterns:Elements of Reusable Object-Oriented Software(美)Erich GammaRichard HelmRalph JohnsonJohn Vlissides
《Design Patterns in Ruby Dec 2007》是关于Ruby编程语言中设计模式的一份珍贵资料,这份2007年发布的PDF文档深入探讨了如何在Ruby语言中应用经典的设计模式。设计模式是软件工程中经过实践证明的有效解决方案模板...
design pattern in C# language
2. **结构型模式**: - 代理模式:为其他对象提供一种代理以控制对这个对象的访问。 - 桥接模式:将抽象部分与实现部分分离,使它们可以独立变化。 - 外观模式:为子系统提供一个统一的接口,使得子系统更容易...