public function __construct() { $this->root = new Composite("Root"); $n1 = new Composite("Composite 1"); $n1->add(new Leaf("Composite1 -> Leaf1")); $n1->add(new Leaf("Composite1 -> Leaf2")); $this->root->add($n1); $n2 = new Composite("Composite2"); $n2->add(new Leaf("Composite2 -> Leaf1")); $n2->add(new Leaf("Composite2 -> Leaf2")); $n2->add(new Leaf("Composite2 -> Leaf3")); $this->root->add($n2); $this->root->add(new Leaf("Root -> Leaf1")); $this->root->operation(); }
public function testComposite() { $root = new Composite(); $root->doSomething(); $branch = new Composite(); $leaf = new Leaf(); $root->add($branch); $branch->add($leaf); $this->display($root); }
public static function main() { $leaf1 = new Leaf('first leaf'); $leaf2 = new Leaf('second leaf'); $composite = new Composite(); $composite->add($leaf1); $composite->add($leaf2); $composite->operate(); $composite->remove($leaf2); $composite->operate(); $composite->remove($leaf1); $leaf1 = null; $leaf2 = null; $composite = null; }
// определяет поведение примитивных объектов в композиции; class Leaf extends Component { public function add(Component $c) { print "Cannot add to a leaf"; } public function remove(Component $c) { print "Cannot remove from a leaf"; } public function display() { print_r($this->name); } } // Create a tree structure $root = new Composite("root"); $root->add(new Leaf("Leaf A")); $root->add(new Leaf("Leaf B")); $comp = new Composite("Composite X"); $comp->add(new Leaf("Leaf XA")); $comp->add(new Leaf("Leaf XB")); $root->add($comp); $root->add(new Leaf("Leaf C")); // Add and remove a leaf $leaf = new Leaf("Leaf D"); $root->add($leaf); $root->remove($leaf); // Recursively display tree $root->display();
class ConcreteComponent implements ComponentInterface { protected $name; public function __construct($name) { $this->name = $name; } public function operation() { echo '[' . $this->name . '] operation done'; } } class Composite implements ComponentInterface { protected $components = array(); public function add(ComponentInterface $component) { $this->components[] = $component; } public function operation() { foreach ($this->components as $component) { $component->operation(); } } } $composite = new Composite(); $composite->add(new ConcreteComponent('FirstComponent')); $composite->add(new ConcreteComponent('SecondComponent')); $composite->add(new ConcreteComponent('ThirdComponent')); $composite->operation();
{ return $this->name; } public function add(Component $Component) { throw new ComponentException("I can't append child to myself"); } public function getChild($index) { throw new ComponentException("Child not exists"); } public function operation() { print "I am leaf '{$this->name}' <br >"; } public function remove($index) { throw new ComponentException("Child not exists"); } public function getChildren() { return array(); } } $composite = new Composite(); $composite->add(new Leaf('First')); $composite->add(new Leaf('Second')); $composite->add(new Leaf('Third')); $composite->add(new Leaf('Fourth')); $composite->add(new Leaf('Fifth')); $composite->operation();