Example #1
0
        if (isset($this->items[$component->GetName()])) {
            return $this->items[$component->GetName()];
        }
        return null;
    }
    public function GetName() : string
    {
        return $this->name;
    }
    public function SetName(string $value)
    {
        $this->name = $value;
    }
}
$product = new Composite("Product");
$a = new Composite("Part A");
$b = new Composite("Part B");
$c = new Composite("Part C");
$a->Add(new Single("Sub Part A-1"));
$a->Add(new Single("Sub Part A-2"));
$a->Add(new Single("Sub Part A-3"));
$a->Add(new Single("Sub Part A-4"));
$b->Add(new Single("Sub Part B-1"));
$b->Add(new Single("Sub Part B-2"));
$c->Add(new Single("Sub Part C-1"));
$c->Add(new Single("Sub Part C-2"));
$c->Add(new Single("Sub Part C-3"));
$product->Add($a);
$product->Add($b);
$product->Add($c);
echo $product->Display(0);
Example #2
0
 function run_component_proba()
 {
     // 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();
 }
Example #3
0
    public function GetChildren()
    {
        return $this->_children;
    }
}
class Leaf extends Component
{
    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\n";
    }
    public function Remove($index)
    {
        throw new ComponentException("Child not exists");
    }
    public function GetChildren()
    {
        return array();
    }
}
$composite = new Composite();
$composite->Add(new Leaf());
$composite->Operation();