SoFunction
Updated on 2025-03-05

PHP structure mode combination mode

What is Composite Pattern

Combination pattern is a structural pattern that allows you to combine objects into a tree structure to represent a hierarchical relationship of "part-total". Combination allows clients to handle individual objects and object combinations in a consistent manner.

Advantages of combination mode

  • Combination mode allows clients to handle individual objects and object combinations in a consistent manner, thus simplifying client code;
  • Combination mode allows us to add new components more easily, thereby increasing the flexibility and scalability of the system;
  • Combination mode allows us to manage complex object structures more easily, thus reducing the maintenance cost of the system.

Implementation of combination mode

In PHP, we can implement the combination mode using the following methods:

<?php
// Abstract componentsabstract class Component
{
    protected $name;
    public function __construct($name)
    {
        $this->name = $name;
    }
    abstract public function add(Component $component);
    abstract public function remove(Component $component);
    abstract public function display($depth);
}
// Leaf componentclass Leaf extends Component
{
    public function add(Component $component)
    {
        echo "Cannot add to a leaf.";
    }
    public function remove(Component $component)
    {
        echo "Cannot remove from a leaf.";
    }
    public function display($depth)
    {
        echo str_repeat("-", $depth) . $this->name . "\n";
    }
}
// Container componentclass Composite extends Component
{
    private $children = array();
    public function add(Component $component)
    {
        array_push($this->children, $component);
    }
    public function remove(Component $component)
    {
        $key = array_search($component, $this->children, true);
        if ($key !== false) {
            unset($this->children[$key]);
        }
    }
    public function display($depth)
    {
        echo str_repeat("-", $depth) . $this->name . "\n";
        foreach ($this->children as $component) {
            $component->display($depth + 2);
        }
    }
}
// Client code$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"));
$leaf = new Leaf("Leaf D");
$root->add($leaf);
$root->remove($leaf);
$root->display(1);

In the above implementation, we first define an abstract component, and define leaf component and container component. Next, we define an array in the container component to store subcomponents and implement the method of adding and deleting subcomponents to the container component. Finally, we instantiate a root component in the client code and add leaf component, container component and leaf component to it, and call the root component'sdisplayMethod to display the entire component tree.

Use of combination modes

<?php
$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"));
$leaf = new Leaf("Leaf D");
$root->add($leaf);
$root->remove($leaf);
$root->display(1);

In the above use, we instantiate a root component and add leaf component, container component and leaf component to it, and call the root component'sdisplayMethod to display the entire component tree.

Summarize

Combination pattern is a very common structure pattern that allows us to combine objects into a tree structure to represent a hierarchical relationship of "part-total". In actual development, we can choose different combination methods to manage complex object structures according to specific needs, thereby improving the flexibility and scalability of the system.

This is the article about the combination mode of PHP structured mode. For more related content on PHP combination mode, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!