Home >Backend Development >PHP Tutorial >Combination mode of PHP design pattern - processing tree structure data

Combination mode of PHP design pattern - processing tree structure data

齐天大圣
齐天大圣Original
2020-08-05 16:39:491849browse

Regarding the combination mode, do not take it literally. It has nothing to do with the combination relationship we understand. It is used to process data in a tree structure. Because it can only process data in a tree structure, it is not very commonly used in daily life. However, if the tree structure is satisfied, it can be processed very well using this mode, which can greatly reduce the amount of code and make the code written concise and clear. .

Definition

Composition pattern is a structural design pattern, you can use it to combine objects into a tree structure, and can be like Use them like independent objects. Its core lies in recursion, through which tree-structured data is processed sequentially.

Scenario

The file directory tree is a typical tree-structured data. The following shows a class used to calculate the size of a file or directory. It has two main methods, adding a child node (subtree) addNode and counting the size of a file or directory fileSize.

class File
{
    private $path = '';
    private $nodes = [];
    private $fileSize = 0;
    
    public function __construct(string $path, int $fileSize)
    {
        $this->path = $path;
        $this->fileSize = $fileSize;
    }
    
    public function addNode (File $node)
    {
        $this->nodes[] = $node;
    }
    
    public function fileSize ()
    {
        $size = 0;
        foreach ($this->nodes as $node) {
            $size += $node->fileSize();
        }
        return $size;
    }
}

The following directory tree is simulated below

/app

/app/1.txt
/app/a
/app/b

/app/a/a1.txt
/app/b/b1.txt
/app/b/b2.txt

The test code is as follows:

$node0 = new File('/app', 0);

$node1 = new File('/app/1.txt', 1000);
$node2 = new File('/app/a', 0);
$node3 = new File('/app/b', 0);

$node21 = new File('/app/a1.txt', 1000);
$node31 = new File('/app/b1.txt', 1000);
$node32 = new File('/app/b2.txt', 1000);

$node2->addNode($node21);
$node3->addNode($node31);
$node3->addNode($node32);
$node0->addNode($node1);
$node0->addNode($node2);
$node0->addNode($node3);

// 计算目录/app/b大小
echo $node3->fileSize() . 'B' . PHP_EOL; // 2000
// 计算/app目录大小
echo $node0->fileSize() . 'B' . PHP_EOL; // 4000

Summary

Combined mode organizes a group of objects into a tree structure, and then treats the objects as nodes of the tree. Utilize a tree-shaped data structure and use recursion to process each subtree, simplifying code implementation in turn. Because this mode has strict requirements for data, it is not used much in daily life. If you want to use this mode, you need to have a good understanding of the business scenario and then be able to abstract the data into a tree structure. Generally, what we commonly see include file directory trees, Infinitus classification processing, etc.

The above is the detailed content of Combination mode of PHP design pattern - processing tree structure data. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn