A implementation of delegate pattern (1)

First of all, use SimpleDelegator class
1.first new a class
2.new another class extends SimpleDelegator,and donot forget require "delegate" at top of file
3.in this subclass ,super(obj) should be added in end of method initialize
4.let's look at code

require 'delegate'
class Hello
    def helloworld()
        "hello world"
    end
end
class MyDelegate < SimpleDelegator
    def initialize
        @hello = Hello.new
        super(@hello)
    end
    def setObj
        __setobj__(@hello);
    end
end
hello =  MyDelegate.new
hello.setObj
puts hello.helloworld


and then output "hello world" string

你可能感兴趣的:(Ruby)