Skip to content

Instantly share code, notes, and snippets.

@caike
Created October 26, 2012 12:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caike/3958587 to your computer and use it in GitHub Desktop.
Save caike/3958587 to your computer and use it in GitHub Desktop.
Decorators
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