Skip to content

Instantly share code, notes, and snippets.

@hastinbe
Created September 13, 2019 01:48
Show Gist options
  • Save hastinbe/f2ca22cad2751ec6ce47293647b34de8 to your computer and use it in GitHub Desktop.
Save hastinbe/f2ca22cad2751ec6ce47293647b34de8 to your computer and use it in GitHub Desktop.
Composite Pattern Example #php #design-patterns #composite-pattern
<?php
abstract class Component
{
protected $_parent;
protected $_children;
public function __construct()
{
$this->_children = new SplDoublyLinkedList();
}
public function __destruct()
{
echo "Destroying " . $this->toString() . "\n";
}
public function addChild(Component $child)
{
$child->setParent($this);
$this->_children->push($child);
}
public function removeChild($index)
{
$this->getChild($index)->setParent(null);
$this->_children->offsetUnset($index);
}
public function setParent(Component $parent=null)
{
$this->_parent = $parent;
}
public function getParent()
{
return $this->_parent;
}
public function getChild($index)
{
return $this->_children->offsetGet($index);
}
public function output()
{
foreach ($this->_children as $child)
$child->output();
}
public function toString()
{
return "Component";
}
}
class Leaf1 extends Component
{
public function addChild(Component $child)
{
throw new Exception("Leaf1 cannot have children.");
}
public function removeChild($index)
{
throw new Exception("Leaf1 cannot have children.");
}
public function getChild($index)
{
throw new Exception("Leaf1 cannot have children.");
}
public function output()
{
echo "Leaf1\n";
}
public function toString()
{
return "Leaf1";
}
}
class Leaf2 extends Component
{
public function addChild(Component $child)
{
throw new Exception("Leaf2 cannot have children.");
}
public function removeChild($index)
{
throw new Exception("Leaf2 cannot have children.");
}
public function getChild($index)
{
throw new Exception("Leaf2 cannot have children.");
}
public function output()
{
echo "Leaf2\n";
}
public function toString()
{
return "Leaf2";
}
}
class CompositeComponent extends Component
{
public function toString()
{
return "CompositeComponent";
}
}
$composite1 = new CompositeComponent();
$composite2 = new CompositeComponent();
$composite2->addChild(new Leaf1());
$composite2->addChild(new Leaf2());
$composite1->addChild($composite2);
$composite1->addChild(new Leaf1());
$composite1->output();
$child = $composite1->getChild(1);
echo "Parent of child: " . ($child->getParent() == null ? "(null)" : $child->getParent()->toString()) . "\n";
echo "Removing child from composite...\n";
$composite1->removeChild(1);
echo "Parent of child: " . ($child->getParent() == null ? "(null)" : $child->getParent()->toString()) . "\n";
$composite1->removeChild(0);
$composite1->addChild($child);
echo "Parent of child: " . ($child->getParent() == null ? "(null)" : $child->getParent()->toString()) . "\n";
//$composite2->destroyChildren();
//$composite1->detachChildren();
unset($composite1);
echo "Parent of child: " . ($child->getParent() == null ? "(null)" : $child->getParent()->toString()) . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment