ruby设计模式--装饰器模式
通过装饰器模式以客户透明的形式动态的对目标对象的行为进行增强,并且可以形成一系列的装饰器链,按照用户的想法组合并且增加自己想要的功能点。
?
class Report def report p "standard report" end endclass Decorator attr_accessor :report def initialize report @report = report end def report p "decorate code of before" @report.report p "decorate code of after" end endclass SubDecorator < Decorator def initialize report super(report) end def report p "sub decorate code of before" @report.report p "sub decorate code of after" end endreport = Report.newdecorator = SubDecorator.new(Decorator.new(report))decorator.report
?这里是一个装饰器链,可以按照自己的组合来新增装饰器,然后来组成想要的功能组合。
?