Skip to content

Instantly share code, notes, and snippets.

Created June 3, 2014 13:29
Show Gist options
  • Save anonymous/c4627692cdeb2842dd6b to your computer and use it in GitHub Desktop.
Save anonymous/c4627692cdeb2842dd6b to your computer and use it in GitHub Desktop.
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