# Bridge パターン # # Java言語で学ぶデザインパターンと同様の例題をRubyで記述した。 # # coded by Takehiro Kaga # class Display def initialize(impl) @impl = impl end def open @impl.rawOpen end def printf @impl.rawPrint end def close @impl.rawClose end def display open printf close end end class CountDisplay < Display def initialize(impl) super(impl) end def multiDisplay(times) open for i in 1..times printf end close end end class DisplayImpl def rawOpen end def rawPrint end def rawClose end end class StringDisplayImpl < DisplayImpl def initialize(string) @string = string @width = @string.length end def rawOpen printLine end def rawPrint puts("|" + @string + "|") end def rawClose printLine end def printLine print "+" for i in 1..@width print "-" end puts "+" end end # --- Main --- # d1 = Display.new(StringDisplayImpl.new("Hello, Japan.")) d2 = CountDisplay.new(StringDisplayImpl.new("Hello, World.")) d3 = CountDisplay.new(StringDisplayImpl.new("Hello, Universe.")) d1.display d2.display d3.display d3.multiDisplay(5)