PHP design pattern combination pattern

不言
Release: 2023-03-24 10:04:02
Original
1491 people have browsed it

This article introduces the combination mode of PHP design pattern, which has a certain reference value. Now I share it with you. Friends in need can refer to it

Combinator (Composite) Pattern is one of the structural patterns in design patterns. Its main purpose is to combine multiple objects into a tree-like structure to represent the "whole-part" relationship.
Example: We use a tree diagram to represent Jiangsu Province->Nanjing City->Qinhuai District and Jianye District.
Among them, Jiangsu Province is the first level, Nanjing City belongs to Jiangsu Province and is the second level, while Jianye District and Qinhuai District belong to Nanjing City and are the third level.
You will get the structure as shown:


-->江苏省          1级
-->-->南京市       2级
-->-->-->秦淮区    3级
-->-->-->建邺区    3级
Copy after login



The role of the combiner mode is that the client combines objects through the same method and distinguishes the master-slave level, improving flexibility and scalability.

<?php  
         
      
/** 抽象一个总组合器类     
 *  abstract IComposite          
 */   
abstract Class IComposite  
{  
   protected $name;  
   function __construct($name)  
   {  
      $this->name = $name;  
   }  
   abstract function Add(IComposite $place);  
  
   abstract function Display($level);  
}  
  
/** 组合器类 供客户端调用 
 *  Composite          
 */  
Class Composite extends IComposite  
{  
   private $places = array();  
  
   function __construct($name)  
   {   
      parent::__construct($name);  
   }  
  
   function Add(IComposite $place)  
   {  
      $this->places[] = $place;  
   }  
  
/** 显示方法 
 *  Display  
 *  $level 级别 默认江苏省为 1级       
 */  
   function Display($level = "1")  
   {  
      $pre = "";  
  
      for ($i=0; $i < $level; $i++) {   
          $pre.= "-->";  
      }  
      $pre.=$this->name."<br/>";  
      echo $pre;  
  
      foreach ($this->places as $v) {  
          // 往后南京市 级别加1 秦淮区在南京市基础上再加1  
         $v->display($level+1);   
      }  
  
   }  
}
Copy after login
<?php  
  
// 组合器模式  index.php  
header("Content-Type:text/html;charset=utf-8");  
  
require_once "Composite.php"; // Composite.php  
  
// 先处理江苏省  
$jiangsu = new Composite("江苏省");  
  
// 再处理南京市  
$nanjing = new Composite("南京市");  
  
// 最后处理秦淮区和建邺区  
$qinhuai = new Composite("秦淮区");   
$jianye = new Composite("建邺区");  
  
// 把南京添加到江苏省下  
$jiangsu->Add($nanjing);  
  
// 把秦淮区和建邺区添加到南京市下  
$nanjing->Add($qinhuai);  
$nanjing->Add($jianye);  
  
$jiangsu->Display(); // 显示
Copy after login

Related recommendations:

PHP Design Pattern Bridge Mode

PHP Design Pattern Adapter Mode

PHP Design Pattern Builder Pattern



The above is the detailed content of PHP design pattern combination pattern. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template