Template Method その1
1つめのサンプルを、単純に移植。
module TemplateMethod class AbstractDisplay def disp open 5.times { _print } close end end class CharDisplay < AbstractDisplay def initialize(str) @str = str end def open print "<" end def _print print @str end def close puts ">" end end end TemplateMethod::CharDisplay.new("H").disp => <HHHHH>
親クラスにメソッドを書かなくても動きますね。
書いた方が分かりやすいかも知れないが、気にしない。
このパターンにはブロックが使えそうです。
寧ろ、ブロック全てがTemplateMethod?
まぁ、考えてみましょう。