class Tree def initialize puts "Make a normal tree" end def decorate puts "Make sure the tree won\'t fall" end end class RedBalls < Tree def initialize(tree) @parent = tree; end def decorate @parent.decorate puts "Put on some red balls" end end class BlueBalls < Tree def initialize(tree) @parent = tree; end def decorate @parent.decorate puts "Add blue balls" end end class Angel < Tree def initialize(tree) @parent = tree; end def decorate @parent.decorate puts "An angel on the top" end end tree = Angel.new(BlueBalls.new(RedBalls.new(Tree.new))) tree.decorate
Ruby的装饰器模式实现
最新推荐文章于 2024-04-19 17:29:29 发布