Created
October 26, 2012 12:49
-
-
Save caike/3958587 to your computer and use it in GitHub Desktop.
Decorators
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
class Poet | |
def produce_art(inspiration) | |
"#{inspiration} !" | |
end | |
end | |
class ArtistDecorator < Struct.new(:artist) | |
def produce_art(inspiration) | |
artist.produce_art(inspiration) | |
end | |
end | |
class Musician < ArtistDecorator | |
def produce_art(inspiration) | |
@art = artist.produce_art(inspiration) | |
add_music_to_art | |
end | |
private | |
def add_music_to_art | |
"MUSIC! #{@art}" | |
end | |
end | |
class VideoDirector < ArtistDecorator | |
def produce_art(inspiration) | |
@art = artist.produce_art(inspiration) | |
add_video_to_art | |
end | |
private | |
def add_video_to_art | |
"ACTION! #{@art}" | |
end | |
end | |
artist = VideoDirector.new(Musician.new(Poet.new)) | |
p artist.produce_art("Every rose has its thorn...") | |
# "ACTION! MUSIC! Every rose has its thorn..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment