Created
June 3, 2014 13:29
-
-
Save anonymous/c4627692cdeb2842dd6b 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
class Artist | |
attr_reader :subscribers, :name, :label | |
def initialize(name, label) | |
@name = name | |
@label = label | |
@subscribers = [] | |
end | |
def add_subscriber(subscriber) | |
@subscribers << subscriber | |
end | |
def name=(name) | |
@name = name | |
update | |
end | |
def label=(label) | |
@label = label | |
update | |
end | |
private | |
def update | |
subscribers.each do |subscriber| | |
subscriber.notify(self) | |
end | |
end | |
end | |
class Album | |
attr_reader :artist, :name | |
def initialize(name, artist) | |
@name = name | |
@artist = artist | |
end | |
def notify(observed) | |
@artist = observed | |
end | |
end | |
class Genre | |
attr_reader :artist, :name | |
def initialize(name, artist) | |
@name = name | |
@artist = artist | |
end | |
def notify(observed) | |
@artist = observed | |
end | |
end | |
snoop = Artist.new('Snoop Dogg', "RCA Records") | |
tdf = Album.new("Tha Doggfather", snoop) | |
west_side = Genre.new("West Coast Rap", snoop) | |
snoop.add_subscriber(tdf) | |
snoop.add_subscriber(west_side) | |
p "Before any changes" | |
p tdf.artist.name | |
p tdf.artist.label | |
snoop.name = "Snoop Lion" | |
p "After name change" | |
p tdf.artist.name | |
p tdf.artist.label | |
snoop.label = "Aftermath" | |
p "After label change" | |
p west_side.artist.name | |
p west_side.artist.label |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment