例如:
class Order < ActiveRecord::Base
def pay
base_handler.init_with(self)
end
end
class BaseHandler
def initialize
#
end
def init_with order
# 根据 order 的类型来决定使用 way_one 还是 way_two
end
def pay
end
end
class WayOne < BaseHandler
def pay
# 流程 1
end
end
class WayTwo < BaseHandler
def pay
# 流程 2
end
end
参考文章: Rails' delegates are even more useful than I knew delegation-in-ruby