Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save afbroman/3762373 to your computer and use it in GitHub Desktop.
Save afbroman/3762373 to your computer and use it in GitHub Desktop.
Understanding the BOOS, "Don't ask tell" from a talk at GORUCO 2012
##
# Growing Object Oriented Software
# Tell, don't ask approach
# Tested and learned the principle
class SomeProgram
def self.runit
# Printer is the main purpose
# Later I might want to Log, IM, Email or Ping something about the printing result.
# So with this approach I can just add or remove any of either Logger, InstantMessenger, Mailer or Pinger
printer = Printer.new(Mailer.new(Pinger.new(self)))
printer.print!
end
def self.print_successful
puts "#{self} finally got the message that print! was a success\n\n"
end
end
class Printer < Struct.new(:listener)
def print!
puts "#{self.class} is printing something... \n\n"
sleep 1
puts "#{self.class} is now telling #{self.listener.class} that print! was successful\n\n"
sleep 1
listener.print_successful
end
end
class Logger < Struct.new(:listener)
def print_successful
puts "#{self.class} was told to logg that print! was a success\n\n"
sleep 1
puts "#{self.class} is now telling #{self.listener.class} that print! was a success\n\n"
sleep 1
listener.print_successful
end
end
class Mailer < Struct.new(:listener)
def print_successful
puts "#{self.class} was told to email that print! was a success\n\n"
sleep 1
puts "#{self.class} is now telling #{self.listener.class} that print! was a success\n\n"
sleep 1
listener.print_successful
end
end
class InstantMessenger < Struct.new(:listener)
def print_successful
puts "#{self.class} was told to IM that print! was a success\n\n"
sleep 1
puts "#{self.class} is now telling #{self.listener.class} that print! was a success\n\n"
sleep 1
listener.print_successful
end
end
class Pinger < Struct.new(:listener)
def print_successful
puts "#{self.class} was told to ping that print! was a success\n\n"
sleep 1
puts "#{self.class} is now telling #{self.listener.class} that print! was a success\n\n"
sleep 1
listener.print_successful
end
end
SomeProgram.runit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment