Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
module RunStrategies
class RunStrategyInterface
def self.run(name)
raise "Run method has not been implemented!"
end
end
class Jog < RunStrategyInterface
def self.run(name)
puts "#{name} is now jogging at a comfortable pace."
end
end
class Sprint < RunStrategyInterface
def self.run(name)
puts "#{name} is now sprinting full speeed!"
end
end
class Marathon < RunStrategyInterface
def self.run(name)
puts "#{name} is now running at a steady pace."
end
end
end
class Runner
attr_reader :name, :strategy
attr_writer :strategy
def initialize(name, strategy)
@name = name
@strategy = strategy
end
def run
@strategy.run(@name)
end
end
class Race
attr_reader :runners
def initialize(runners)
@runners = runners
end
def start
@runners.each { |runner| runner.run }
end
end
alice_ruby = Runner.new("Alice Ruby", RunStrategies::Jog)
florence_joyner = Runner.new("Florence Joyner", RunStrategies::Sprint)
eliud_kipchoge = Runner.new("Eliud Kipchoge", RunStrategies::Marathon)
race = Race.new([alice_ruby, florence_joyner, eliud_kipchoge])
race.start
# Alice Ruby is now jogging at a comfortable pace.
# Florence Joyner is now sprinting full speeed!
# Eliud Kipchoge is now running at a steady pace.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.