Ruby设计模式:
模板方法:定义一个操作中的算法的骨架 而将一些步骤的实现延迟到子类中 模板方法使得之类可以不改变算法的结构既可重定义该算法的某些特定步骤。
- #%()用来定义单行字符串 --- 包含“ 并且有字符串插值
- class Report
- def output; puts "#{report_start}#{report_body}#{report_end}"; end
- def report_body
- %(\nbody\n)
- end
- end
- class HtmlReport < Report
- def report_start
- %(<html>)
- end
- def report_end
- %(</html>)
- end
- end
- class TextReport < Report
- def report_start
- %(=start=)
- end
- def report_end
- %(=end=)
- end
- end
- TextReport.new.output
- HtmlReport.new.output
结果:
=start=
body
=end=
<html>
body
</html>
转载于:https://blog.51cto.com/athinkingreed/870550