Ruby策略模式2

2015-09-04 0 611
Ruby策略模式2
 class Formatter
     def output_report title, text
         raise 'can not call Abstract method'
     end
 end

 class HTMLFormatter < Formatter
     def output_report title, text
         puts '<html>'
        puts '    <head>'
        puts '        <title>' + title + '</title>'
        puts '    </head>'
         puts '    <body>'
         text.each do |line|
             puts "<p>#{line}</p>"
        end
         puts '    </body>'
         puts '</html>'
    end
 end

 class PlainTextFormatter < Formatter
    def output_report title, text
         puts '******** ' + title + ' ********'
        text.each do |line|
             puts line
         end
     end
 end

 class Reporter
    attr_reader :title, :text
     attr_accessor :formater

     def initialize formater
        @title = 'My Report'
         @text = ['This is my report', 'Please see the report', 'It is ok']
        @formater = formater
    end

     def output_report
         @formater.output_report @title, @text
     end
 end

Reporter.new(HTMLFormatter.new).output_report
Reporter.new(PlainTextFormatter.new).output_report

遇见资源网 ruby Ruby策略模式2 http://www.ox520.com/16443.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务