Skip to content

Instantly share code, notes, and snippets.

@kivanio
Created September 20, 2010 19:44
Show Gist options
  • Save kivanio/588528 to your computer and use it in GitHub Desktop.
Save kivanio/588528 to your computer and use it in GitHub Desktop.
Strategy_pattern + extend self
# http://en.wikipedia.org/wiki/Strategy_pattern + extend self
module StrategyA
extend self
def execute
puts 'Doing the task the normal way'
end
end
module StrategyB
def execute
puts 'Doing the task alternatively'
end
end
module StrategyC
def execute
puts 'Doing the task even more alternatively'
end
end
module ProxyExt
extend self
def define_extension(extension)
case extension
when :rghost
StrategyA
when :prawn
StrategyB
else
extension
end
end
end
class Context
include ProxyExt
extend ProxyExt
extend define_extension(:rghost)
def initialize(strategy=nil)
self.class.send :include, define_extension(strategy)
end
end
Context.execute
a = Context.new(:rghost)
a.execute #=> Doing the task the normal way
b = Context.new(StrategyB)
b.execute #=> Doing the task alternatively
c = Context.new(StrategyC)
c.execute #=> Doing the task even more alternatively
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment