Ruby策略模式2

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

 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