[关闭]
@Chiang 2020-01-03T09:56:18.000000Z 字数 3873 阅读 532

结构型--组合模式

设计模式


  • 组件
  • 树枝节点
  • 叶子节点

透明模式

透明模式

组件抽象类

  1. <?php
  2. /**
  3. * Component.php
  4. *
  5. * @package sy-records/design-patterns
  6. * @author Luffy (lufei@swoole.com)
  7. * @date 2019/11/22
  8. * @copyright Swoole, Inc.
  9. */
  10. namespace Luffy\DesignPatterns\Composite\Transparent;
  11. abstract class Component
  12. {
  13. /**
  14. * @var
  15. */
  16. protected $name;
  17. /**
  18. * Component constructor.
  19. *
  20. * @param $name
  21. */
  22. public function __construct($name)
  23. {
  24. $this->name = $name;
  25. }
  26. /**
  27. * @param Component $component
  28. * @return mixed
  29. */
  30. abstract public function add(Component $component);
  31. /**
  32. * @return mixed
  33. */
  34. abstract public function getName();
  35. }

树枝节点类

  1. <?php
  2. /**
  3. * Dir.php
  4. *
  5. * @package sy-records/design-patterns
  6. * @author Luffy (lufei@swoole.com)
  7. * @date 2019/11/22
  8. * @copyright Swoole, Inc.
  9. */
  10. namespace Luffy\DesignPatterns\Composite\Transparent;
  11. class Dir extends Component
  12. {
  13. /**
  14. * @var array
  15. */
  16. protected $children = [];
  17. /**
  18. * @param Component $component
  19. */
  20. public function add(Component $component)
  21. {
  22. $this->children[] = $component;
  23. }
  24. public function getName()
  25. {
  26. $nameStr = $this->name ."\n";
  27. foreach ($this->children as $k => $v) {
  28. $nameStr .= "--" . $v->getName();
  29. }
  30. return $nameStr;
  31. }
  32. }

叶子节点类

  1. <?php
  2. /**
  3. * File.php
  4. *
  5. * @package sy-records/design-patterns
  6. * @author Luffy (lufei@swoole.com)
  7. * @date 2019/11/22
  8. * @copyright Swoole, Inc.
  9. */
  10. namespace Luffy\DesignPatterns\Composite\Transparent;
  11. class File extends Component
  12. {
  13. /**
  14. * @param Component $component
  15. * @throws \Exception
  16. */
  17. public function add(Component $component)
  18. {
  19. throw new \Exception("文件不能添加子节点");
  20. }
  21. /**
  22. * @return string
  23. */
  24. public function getName()
  25. {
  26. return "--" . $this->name . "\n";
  27. }
  28. }

客户端调用

  1. <?php
  2. /**
  3. * Client.php
  4. *
  5. * @package sy-records/design-patterns
  6. * @author Luffy (lufei@swoole.com)
  7. * @date 2019/11/22
  8. * @copyright Swoole, Inc.
  9. */
  10. namespace Luffy\DesignPatterns\Composite\Transparent;
  11. require __DIR__ . '/../../vendor/autoload.php';
  12. class Client
  13. {
  14. public static function run()
  15. {
  16. $composite = new Dir("Composite");
  17. $transparent = new Dir("Transparent");
  18. $composite->add($transparent);
  19. $componentFile = new File("Component.php");
  20. $dirFile = new File("Dir.php");
  21. $fileFile = new File("File.php");
  22. $clientFile = new File("Client.php");
  23. $transparent->add($componentFile);
  24. $transparent->add($dirFile);
  25. $transparent->add($fileFile);
  26. $transparent->add($clientFile);
  27. $testFile = new File("test.php");
  28. $clientFile->add($testFile);
  29. echo $composite->getName();
  30. }
  31. }
  32. Client::run();

安全模式

安全模式

组件抽象类

  1. <?php
  2. /**
  3. * Component.php
  4. *
  5. * @package sy-records/design-patterns
  6. * @author Luffy (lufei@swoole.com)
  7. * @date 2019/11/22
  8. * @copyright Swoole, Inc.
  9. */
  10. namespace Luffy\DesignPatterns\Composite\Safe;
  11. abstract class Component
  12. {
  13. /**
  14. * @var
  15. */
  16. protected $name;
  17. /**
  18. * Component constructor.
  19. *
  20. * @param $name
  21. */
  22. public function __construct($name)
  23. {
  24. $this->name = $name;
  25. }
  26. /**
  27. * @return mixed
  28. */
  29. abstract public function getName();
  30. }

树枝节点类

  1. <?php
  2. /**
  3. * Dir.php
  4. *
  5. * @package sy-records/design-patterns
  6. * @author Luffy (lufei@swoole.com)
  7. * @date 2019/11/22
  8. * @copyright Swoole, Inc.
  9. */
  10. namespace Luffy\DesignPatterns\Composite\Safe;
  11. class Dir extends Component
  12. {
  13. /**
  14. * @var array
  15. */
  16. protected $children = [];
  17. /**
  18. * @param Component $component
  19. */
  20. public function add(Component $component)
  21. {
  22. $this->children[] = $component;
  23. }
  24. public function getName()
  25. {
  26. $nameStr = $this->name ."\n";
  27. foreach ($this->children as $k => $v) {
  28. $nameStr .= "--" . $v->getName();
  29. }
  30. return $nameStr;
  31. }
  32. }

叶子节点类

  1. <?php
  2. /**
  3. * File.php
  4. *
  5. * @package sy-records/design-patterns
  6. * @author Luffy (lufei@swoole.com)
  7. * @date 2019/11/22
  8. * @copyright Swoole, Inc.
  9. */
  10. namespace Luffy\DesignPatterns\Composite\Safe;
  11. class File extends Component
  12. {
  13. /**
  14. * @return string
  15. */
  16. public function getName()
  17. {
  18. return "--" . $this->name . "\n";
  19. }
  20. }

客户端调用类

  1. <?php
  2. /**
  3. * Client.php
  4. *
  5. * @package sy-records/design-patterns
  6. * @author Luffy (lufei@swoole.com)
  7. * @date 2019/11/22
  8. * @copyright Swoole, Inc.
  9. */
  10. namespace Luffy\DesignPatterns\Composite\Safe;
  11. require __DIR__ . '/../../vendor/autoload.php';
  12. class Client
  13. {
  14. public static function run()
  15. {
  16. $composite = new Dir("Composite");
  17. $safe = new Dir("Safe");
  18. $composite->add($safe);
  19. $componentFile = new File("Component.php");
  20. $dirFile = new File("Dir.php");
  21. $fileFile = new File("File.php");
  22. $clientFile = new File("Client.php");
  23. $safe->add($componentFile);
  24. $safe->add($dirFile);
  25. $safe->add($fileFile);
  26. $safe->add($clientFile);
  27. echo $composite->getName();
  28. }
  29. }
  30. Client::run();

参考资料:
swoole微课堂
示例代码

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注