概述
若你从事过面向对象开发,实现给一个类或对象增加行为,使用继承机制,这是所有面向对象语言的一 个基本特性。如果已经存在的一个类缺少某些方法,或者须要给方法添加更多的功能(魅力),你也许会仅仅继承这个类来产生一个新类—这建立在额外的代码上。
通过继承一个现有类可以使得子类在拥有自身方法的同时还拥有父类的方法。但是这种方法是静态的,用户不能控制增加行为的方式和时机。如果 你希望改变一个已经初始化的对象的行为,你怎么办?或者,你希望继承许多类的行为,改怎么办"htmlcode">
class SimpleWriter def initialize(path) @file = File.open(path,"w") end def write_line(line) @file.print(line) @file.print("\n") end #字符数 def pos @file.pos end #它将会将文件指针指向文件的开头 def rewind @file.rewind end def close @file.colse end end sw = SimpleWriter.new("test.txt") sw.write_line("你好") puts sw.pos puts sw.rewind #基类 class WriterDecorator def initialize(real_writer) @real_writer = real_writer end def write_line @real_writer.write_line end def pos @real_writer.pos end def rewind @real_writer.rewind end def close @real_writer.close end end class NumberingWriter < WriterDecorator attr :line_number def initialize(real_writer) super(real_writer) @line_number = 1 end #实际调用的是WriterDecorator的write_line方法,只是在写入的内容前加上了编号(装饰) #所以说NumberingWriter对WriterDecorator的接口wirte_line进行了装饰 # def write_line(line) @real_writer.write_line("#{@line_number}:#{line}") @line_number += 1 end end sw = SimpleWriter.new("numbering_write.txt") nw = NumberingWriter.new(sw) nw.write_line("hello,world") nw.write_line("hello,ruby") puts nw.line_number class CheckSummingWriter < WriterDecorator attr_reader :check_num def initialize(real_writer) super(real_writer) @check_num = 0 end def write_line(line) line.each_byte{|byte| @check_num += byte % 256} @real_writer.write_line(line) end end sw = SimpleWriter.new("check_num_writer.txt") csw = CheckSummingWriter.new(sw) csw.write_line("hello,world") puts csw.check_num class TimeStampingWriter < WriterDecorator def initialize(real_writer) super(real_writer) end def write_line(line) @real_writer.write_line("#{Time.now}: #{line}") end end #倒着看 #5. 实际调用的是SimpleWriter得write_line方法,将内容写入文件 sw = SimpleWriter.new("mix.txt") #4. 实际调用的是NumberingWriter得write_line方法,对在输入的数据前加上了编号 # 然后传给@real_writer,此时的@real_witer为sw nw = NumberingWriter.new(sw) #3. 实际调用的是TimeStampingWriter得write_line方法,对在输入的数据前加上了时间戳 # 然后传给@real_writer,此时的@real_witer为nw tsw = TimeStampingWriter.new(nw) #2. 实际调用的是CheckSummingWriter得write_line方法,对输入的数据进行了字节数的统计 # 然后传给@real_writer,此时的@real_witer为tsw csw = CheckSummingWriter.new(tsw) #1. csw调用write_line csw.write_line("hello,world") puts csw.check_num
两种ruby风格的装饰模式应用
(1)使用extend混入模块
class SimpleWriter def initialize(path) @file = File.open(path,"w") end def write_line(line) @file.print(line) @file.print("\n") end #字符数 def pos @file.pos end #它将会将文件指针指向文件的开头 def rewind @file.rewind end def close @file.colse end end #使用extend方法动态的混入模块,来进行装饰 module TimeStampingWriter def write_line(line) super("#{Time.now}:#{line}") end end module NumberingWriter attr_reader :line_number def write_line(line) @line_number = 1 unless @line_number super("#{@line_number}:#{line}") @line_number += 1 end end
最后被加入的模块,先被调用,然后通过super来调用父类的write_line方法。
例子中先在文本的前面加上时间戳,在加入编号,最后写入文件
sw = SimpleWriter.new("out3.txt") sw.extend(NumberingWriter) sw.extend(TimeStampingWriter) sw.write_line("hello,ruby")
(2)使用alias关键字
class SimpleWriter def initialize(path) @file = File.open(path,"w") end def write_line(line) @file.print(line) @file.print("\n") end #字符数 def pos @file.pos end #它将会将文件指针指向文件的开头 def rewind @file.rewind end def close @file.colse end end ruby实现装饰模式的另一种动态方法 : 修改对象的实例方法, 所以在out1.txt文件中会加入时间戳,而不影响对象sw2,out2.txt中不会加入时间戳 。 sw1 = SimpleWriter.new("out1.txt") class << sw1 alias old_write_line write_line def write_line(line) old_write_line("#{Time.now}:#{line}") end end sw1.write_line("hello,world") sw2 = SimpleWriter.new("out2.txt") sw2.write_line("hello,world")