Created
January 25, 2012 00:40
-
-
Save yamano/1673816 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment