Class ItemsGroup could be called Composite in current example.
class Component attr_reader :data def initialize(data) @data = data end def display raise "Implement me" end end class ItemsGroup < Component def initialize @children = [] end def add_child(child) @children << child end def remove_child(child) @children.delete(child) end def display @children.each do |child| child.display end end end class Item < Component def display puts data.inspect end end group = ItemsGroup.new group1 = ItemsGroup.new group2 = ItemsGroup.new group1.add_child(Item.new('label')) group1.add_child(Item.new('input text')) group2.add_child(Item.new('label')) group2.add_child(Item.new('input password')) group.add_child(Item.new("header")) group.add_child(group1) group.add_child(group2) group.add_child(Item.new("submit button")) group.display
No comments:
Post a Comment