Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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