`

代理模式(Proxy)

阅读更多
名字: 代理模式(Proxy)
别名: 替身(surrogate)
意图: 为其他对象提供一种代理以控制对这个对象的访问.
动机: 按需创建; 替代对象.

class Company
  def register
    raise "Abstract method"
  end
end

class RealCompany < Company
  def register
    puts "真实的公司申请注册"
  end
end

class ProxyCompany < Company
  attr_accessor :real_company

  def register
    @real_company ||= RealCompany.new
    @real_company.register
  end
end

proxy_company = ProxyCompany.new
proxy_company.register




  • 大小: 29.1 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics