Hatena::Groupruby

kdmsnrの日記

 | 

1000-01-04

Strategy

12:02

class Report
  attr_reader :title, :text
  attr_accessor :formatter

  def initialize(formatter)
    @title = 'Monthly Report'
    @text =  [ 'Things are going', 'really, really well.' ]
    @formatter = formatter
  end

  def output_report
    @formatter.output_report( @title, @text )
  end
end

## クライアント
report = Report.new(HTMLFormatter.new)
report.output_report

# 途中で切り替えたり
report.formatter = PlainTextFormatter.new
report.output_report

 |