Skip to content

Instantly share code, notes, and snippets.

@renato-zannon
Created June 9, 2012 05:33
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 renato-zannon/2899634 to your computer and use it in GitHub Desktop.
Save renato-zannon/2899634 to your computer and use it in GitHub Desktop.
Strategy pattern to follow users
class User < Struct.new(:username, :private)
alias_method :private?, :private
def follow
follow_strategy.call
end
private
def follow_strategy
if private?
PrivateFollow.new(self)
else
NormalFollow.new(self)
end
end
end
class NormalFollow
def initialize(user)
@user = user
end
def call
# actually_follow code
end
end
class PrivateFollow < NormalFollow
def call
super if confirm_follow
end
private
def confirm_follow
# actual code
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment