Created
September 20, 2010 19:44
-
-
Save kivanio/588528 to your computer and use it in GitHub Desktop.
Strategy_pattern + extend self
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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