Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ruby中的委托人_Ruby_Delegates_Delegation - Fatal编程技术网

ruby中的委托人

ruby中的委托人,ruby,delegates,delegation,Ruby,Delegates,Delegation,我知道委托方法调用有三种方式 单纯形连接器 使用DelegateClass 使用抽象类委托器 我知道什么是授权以及它是如何工作的。我知道如何使用SimpleDelegator委派方法调用。但我不确定什么时候我会使用它们。谁能告诉我什么时候使用它们吗。以及如何使用DelegateClass完成委派 SimpleDelegator只是Delegator的基本实现DelegateClass实际上是一个定义delegator类的方法。当Delegator通过method\u instance委托时,De

我知道委托方法调用有三种方式

  • 单纯形连接器
  • 使用DelegateClass
  • 使用抽象类委托器

  • 我知道什么是授权以及它是如何工作的。我知道如何使用SimpleDelegator委派方法调用。但我不确定什么时候我会使用它们。谁能告诉我什么时候使用它们吗。以及如何使用DelegateClass完成委派

    SimpleDelegator
    只是
    Delegator
    的基本实现
    DelegateClass
    实际上是一个定义delegator类的方法。当
    Delegator
    通过
    method\u instance
    委托时,
    DelegateClass
    创建了一个新类,该类实现了与给定给它的超类相同的API

    因此,当您不需要任何特殊的东西时,可以使用
    SimpleDelegator

    class AuthorDecorator < SimpleDelegator
      def hi!
        "Hi, #{name}!"
      end
    end
    
    # assuming author is an object that responds to :name method
    AuthorDecorator.new(author).hi! # => "Hi, Rohan!"
    
    class AuthorDecorator“嗨,罗汉!”
    
    如果您想要实现更复杂的功能,例如,您想要完全控制
    \uuuuu setobj\uuuu
    \uuuu getobj\uuuuu
    方法(例如,对于类型强制),您可以使用
    Delegator

    如上所述,
    DelegateClass
    使用预定义的方法准备一个委托类。这里的区别是,当您使用
    SimpleDelegator
    Delegator
    时,它取决于
    method\u missing
    ,因此如果您的Delegator没有请求的方法,它将通过
    method\u missing
    代理给委托对象。对于准备好的带有
    DelegateClass
    的基类,将有一个已定义的方法,该方法将对委托对象调用相同的方法。因此,
    DelegateClass
    提供了一种更有效的委托方式