2006-10-30

A implementation of delegate pattern (1)

关键字: ruby
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
评论
发表评论

您还没有登录,请登录后发表评论

默海飞瀑
搜索本博客
博客分类
最近加入圈子
最新评论
评论排行榜