@Chiang
2020-01-03T01:56:18.000000Z
字数 3873
阅读 730
设计模式
- 组件
- 树枝节点
- 叶子节点

<?php/*** Component.php** @package sy-records/design-patterns* @author Luffy (lufei@swoole.com)* @date 2019/11/22* @copyright Swoole, Inc.*/namespace Luffy\DesignPatterns\Composite\Transparent;abstract class Component{/*** @var*/protected $name;/*** Component constructor.** @param $name*/public function __construct($name){$this->name = $name;}/*** @param Component $component* @return mixed*/abstract public function add(Component $component);/*** @return mixed*/abstract public function getName();}
<?php/*** Dir.php** @package sy-records/design-patterns* @author Luffy (lufei@swoole.com)* @date 2019/11/22* @copyright Swoole, Inc.*/namespace Luffy\DesignPatterns\Composite\Transparent;class Dir extends Component{/*** @var array*/protected $children = [];/*** @param Component $component*/public function add(Component $component){$this->children[] = $component;}public function getName(){$nameStr = $this->name ."\n";foreach ($this->children as $k => $v) {$nameStr .= "--" . $v->getName();}return $nameStr;}}
<?php/*** File.php** @package sy-records/design-patterns* @author Luffy (lufei@swoole.com)* @date 2019/11/22* @copyright Swoole, Inc.*/namespace Luffy\DesignPatterns\Composite\Transparent;class File extends Component{/*** @param Component $component* @throws \Exception*/public function add(Component $component){throw new \Exception("文件不能添加子节点");}/*** @return string*/public function getName(){return "--" . $this->name . "\n";}}
<?php/*** Client.php** @package sy-records/design-patterns* @author Luffy (lufei@swoole.com)* @date 2019/11/22* @copyright Swoole, Inc.*/namespace Luffy\DesignPatterns\Composite\Transparent;require __DIR__ . '/../../vendor/autoload.php';class Client{public static function run(){$composite = new Dir("Composite");$transparent = new Dir("Transparent");$composite->add($transparent);$componentFile = new File("Component.php");$dirFile = new File("Dir.php");$fileFile = new File("File.php");$clientFile = new File("Client.php");$transparent->add($componentFile);$transparent->add($dirFile);$transparent->add($fileFile);$transparent->add($clientFile);$testFile = new File("test.php");$clientFile->add($testFile);echo $composite->getName();}}Client::run();

<?php/*** Component.php** @package sy-records/design-patterns* @author Luffy (lufei@swoole.com)* @date 2019/11/22* @copyright Swoole, Inc.*/namespace Luffy\DesignPatterns\Composite\Safe;abstract class Component{/*** @var*/protected $name;/*** Component constructor.** @param $name*/public function __construct($name){$this->name = $name;}/*** @return mixed*/abstract public function getName();}
<?php/*** Dir.php** @package sy-records/design-patterns* @author Luffy (lufei@swoole.com)* @date 2019/11/22* @copyright Swoole, Inc.*/namespace Luffy\DesignPatterns\Composite\Safe;class Dir extends Component{/*** @var array*/protected $children = [];/*** @param Component $component*/public function add(Component $component){$this->children[] = $component;}public function getName(){$nameStr = $this->name ."\n";foreach ($this->children as $k => $v) {$nameStr .= "--" . $v->getName();}return $nameStr;}}
<?php/*** File.php** @package sy-records/design-patterns* @author Luffy (lufei@swoole.com)* @date 2019/11/22* @copyright Swoole, Inc.*/namespace Luffy\DesignPatterns\Composite\Safe;class File extends Component{/*** @return string*/public function getName(){return "--" . $this->name . "\n";}}
<?php/*** Client.php** @package sy-records/design-patterns* @author Luffy (lufei@swoole.com)* @date 2019/11/22* @copyright Swoole, Inc.*/namespace Luffy\DesignPatterns\Composite\Safe;require __DIR__ . '/../../vendor/autoload.php';class Client{public static function run(){$composite = new Dir("Composite");$safe = new Dir("Safe");$composite->add($safe);$componentFile = new File("Component.php");$dirFile = new File("Dir.php");$fileFile = new File("File.php");$clientFile = new File("Client.php");$safe->add($componentFile);$safe->add($dirFile);$safe->add($fileFile);$safe->add($clientFile);echo $composite->getName();}}Client::run();
