Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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