In Ruby implementation base class Strategy could be removed.
class Strategy def output(data) raise "Implement me" end end class TxtOutput < Strategy def output(data) puts data.inspect end end class ConsoleTxtOutput < Strategy def output(data) puts ">> #{data.inspect}" end end class SomeLogic attr_accessor :output_formatter def initialize(output_formatter) @output_formatter = output_formatter end def do_some_job @data = { "some" => "data", "here" => 10 } end def print_somewhere @output_formatter.output(@data) end end txt_output = TxtOutput.new console_txt_outut = ConsoleTxtOutput.new # Specify formatter dynamically logic = SomeLogic.new(txt_output) logic.do_some_job logic.print_somewhere # Specify formatter dynamically logic.output_formatter = console_txt_outut logic.print_somewhere
No comments:
Post a Comment