Skip to content

Instantly share code, notes, and snippets.

@dekellum
Created September 25, 2009 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dekellum/193769 to your computer and use it in GitHub Desktop.
Save dekellum/193769 to your computer and use it in GitHub Desktop.
# GoKart becomes a module
module GoKart
# GoKart.new becomes a factory method for strategy classes
def self.new( strategy = nil )
( strategy == :reverse ) ? ReverseStrategy.new : ForwardStrategy.new
end
class ForwardStrategy
def drive
puts "Forward"
end
def stop
puts "Stop"
end
end
class ReverseStrategy < ForwardStrategy
def drive
puts "Reverse"
end
end
end
# No interface change for consumer:
k = GoKart.new
k.drive
k.stop
k = GoKart.new( :reverse )
k.drive
k.stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment