[关闭]
@Chiang 2019-12-11T10:18:33.000000Z 字数 2088 阅读 572

注册树模式

设计模式


把对象的实例注册到全局的对象树上

实现原理

  1. <?php
  2. /**
  3. * 注册树模式
  4. *
  5. * @author Luffy (lufei@swoole.com)
  6. * @date 2019/9/20
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\RegistryTree;
  11. /**
  12. * Class RegistryTree
  13. * @package Luffy\DesignPatterns\RegistryTree
  14. */
  15. class RegistryTree
  16. {
  17. private static $objects;
  18. public static function set($alias, $object)
  19. {
  20. self::$objects[$alias] = $object;
  21. }
  22. public static function get($alias)
  23. {
  24. if (!isset(self::$objects[$alias])) {
  25. return false;
  26. }
  27. return self::$objects[$alias];
  28. }
  29. public static function _unset($alias)
  30. {
  31. if(isset(self::$objects[$alias])){
  32. unset(self::$objects[$alias]);
  33. return true;
  34. }
  35. return false;
  36. }
  37. }
  1. <?php
  2. /**
  3. * 注册树测试文件
  4. *
  5. * @author Luffy (lufei@swoole.com)
  6. * @date 2019/9/20
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. include __DIR__ . '/../vendor/autoload.php';
  11. // 注册树
  12. use Luffy\DesignPatterns\RegistryTree\RegistryTree;
  13. class Demo
  14. {
  15. public function index()
  16. {
  17. echo "红红火火恍恍惚惚\n";
  18. }
  19. }
  20. $demo = new Demo();
  21. var_dump($demo);
  22. $demo->index();
  23. // 添加进对象树中
  24. RegistryTree::set("demo", $demo);
  25. // 获取对象
  26. $demo2 = RegistryTree::get("demo");
  27. var_dump($demo2);
  28. $demo2->index();
  29. // 删除对象
  30. RegistryTree::_unset("demo");
  31. // 再次获取
  32. $demo_obj = RegistryTree::get("demo");
  33. var_dump($demo_obj);
  34. $demo_obj->index();

注册树,单例,工厂一起使用

  1. <?php
  2. /**
  3. * 注册树、单例、工厂一起使用
  4. *
  5. * @author Luffy (lufei@swoole.com)
  6. * @date 2019/9/20
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. include __DIR__ . '/../vendor/autoload.php';
  11. // 注册树
  12. use Luffy\DesignPatterns\RegistryTree\RegistryTree;
  13. /**
  14. * 单例
  15. * Class SingletonDemo
  16. */
  17. class SingletonDemo
  18. {
  19. /**
  20. * @var
  21. */
  22. private static $instance;
  23. private $content;
  24. public static function getInstance()
  25. {
  26. if (!self::$instance instanceof self) {
  27. static::$instance = new self();
  28. }
  29. return static::$instance;
  30. }
  31. /**
  32. * SingletonDemo constructor.
  33. */
  34. private function __construct()
  35. {
  36. $this->content = rand(1, 9999);
  37. }
  38. private function __clone()
  39. {
  40. }
  41. private function __wakeup()
  42. {
  43. // TODO: Implement __wakeup() method.
  44. }
  45. public function test()
  46. {
  47. return $this->content;
  48. }
  49. }
  50. /**
  51. * 工厂
  52. * Class Factory
  53. */
  54. class Factory
  55. {
  56. public static function create()
  57. {
  58. // 返回对象
  59. return SingletonDemo::getInstance();
  60. }
  61. }
  62. RegistryTree::set("singleton", Factory::create());
  63. $obj = RegistryTree::get("singleton");
  64. var_dump($obj);
  65. echo $obj->test();

参考资料:
swoole微课堂
GitHub

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