Ruby/Design Patterns/Composite
Материал из Wiki.crossplatform.ru
Содержание |
An Array as a Composite
class Job < Array attr_reader :name def initialize(name) @name = name end def get_time_required 0.0 end end class SubJob1 < Job def initialize super("sub job 1") end def get_time_required 1.0 # 1 minute to add flour and sugar end end class SubJob2 < Job def initialize super("sub job 2") end def get_time_required 3.0 # Mix for 3 minutes end end class CompositeJob < Job def initialize(name) super(name) @sub_jobs = [] end def add_sub_job(job) @sub_jobs << job end def remove_sub_job(job) @sub_jobs.delete(job) end def get_time_required time=0.0 @sub_jobs.each {|job| time += job.get_time_required} time end def <<(job) @sub_jobs << job end def [](index) @sub_jobs[index] end def []=(index, new_value) @sub_jobs[index] = new_value end end composite = CompositeJob.new("example") composite << SubJob2.new puts(composite[0].get_time_required) composite[1] = SubJob1.new
Array Iterator
class ArrayIterator def initialize(array) @array = array @index = 0 end def has_next? @index < @array.length end def item @array[@index] end def next_item value = @array[@index] @index += 1 value end end array = ["red", "green", "blue"] i = ArrayIterator.new(array) while i.has_next? puts("item: #{i.next_item}") end # Running this code will give us the output we expect: # item: red # item: green # item: blue
Composite with Push and index Operators
class Job attr_reader :name def initialize(name) @name = name end def get_time_required 0.0 end end class SubJob1 < Job def initialize super("sub job 1") end def get_time_required 1.0 # 1 minute to add flour and sugar end end class SubJob2 < Job def initialize super("sub job 2") end def get_time_required 3.0 # Mix for 3 minutes end end class CompositeJob < Job def initialize(name) super(name) @sub_jobs = [] end def add_sub_job(job) @sub_jobs << job end def remove_sub_job(job) @sub_jobs.delete(job) end def get_time_required time=0.0 @sub_jobs.each {|job| time += job.get_time_required} time end def <<(job) @sub_jobs << job end def [](index) @sub_jobs[index] end def []=(index, new_value) @sub_jobs[index] = new_value end end composite = CompositeJob.new("example") composite << SubJob2.new puts(composite[0].get_time_required) composite[1] = SubJob1.new
Creating Composites
class Job attr_reader :name def initialize(name) @name = name end def get_time_required 0.0 end end class SubJob1 < Job def initialize super("sub job 1") end def get_time_required 1.0 # 1 minute to add flour and sugar end end class SubJob2 < Job def initialize super("sub job 2") end def get_time_required 3.0 # Mix for 3 minutes end end class CompositeJob < Job def initialize(name) super(name) @sub_jobs = [] end def add_sub_job(job) @sub_jobs << job end def remove_sub_job(job) @sub_jobs.delete(job) end def get_time_required time=0.0 @sub_jobs.each {|job| time += job.get_time_required} time end end class MakeCakeJob < CompositeJob def initialize super("Make cake") add_sub_job( SubJob1.new ) add_sub_job( SubJob2.new ) end end
Sprucing Up the Composite with Operators
class Job attr_reader :name def initialize(name) @name = name end def get_time_required 0.0 end end class SubJob1 < Job def initialize super("sub job 1") end def get_time_required 1.0 # 1 minute to add flour and sugar end end class SubJob2 < Job def initialize super("sub job 2") end def get_time_required 3.0 # Mix for 3 minutes end end class CompositeJob < Job def initialize(name) super(name) @sub_jobs = [] end def add_sub_job(job) @sub_jobs << job end def remove_sub_job(job) @sub_jobs.delete(job) end def get_time_required time=0.0 @sub_jobs.each {|job| time += job.get_time_required} time end def <<(job) @sub_jobs << job end end composite = CompositeJob.new("example") composite << SubJob2.new