[关闭]
@Chiang 2020-01-03T10:42:08.000000Z 字数 2144 阅读 514

结构型--享元模式

设计模式


  • 享元抽象类
  • 共享的具体享元类
  • 不共享的具体享元类
  • 享元工厂

享元模式

享元抽象类

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

共享的具体享元类

  1. <?php
  2. /**
  3. * ConcreteFlyweight.php
  4. *
  5. * @author Luffy <lufei@swoole.com>
  6. * @date 2019/12/9
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Flyweight;
  11. class ConcreteFlyweight extends Flyweight
  12. {
  13. /**
  14. * @param $content
  15. */
  16. public function get($content)
  17. {
  18. echo "共享的:{$content} {$this->name}\n";
  19. }
  20. }

不共享的具体享元类

  1. <?php
  2. /**
  3. * UnsharedConcreteFlyweight.php
  4. *
  5. * @author Luffy <lufei@swoole.com>
  6. * @date 2019/12/9
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Flyweight;
  11. class UnsharedConcreteFlyweight extends Flyweight
  12. {
  13. /**
  14. * @param $content
  15. */
  16. public function get($content)
  17. {
  18. echo "不共享的:{$content} {$this->name}\n";
  19. }
  20. public function del()
  21. {
  22. $this->name = '';
  23. }
  24. }

享元工厂

  1. <?php
  2. /**
  3. * FlyweightFactory.php
  4. *
  5. * @author Luffy <lufei@swoole.com>
  6. * @date 2019/12/9
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Flyweight;
  11. class FlyweightFactory
  12. {
  13. /**
  14. * @var array
  15. */
  16. private $flyweights = [];
  17. /**
  18. * @param $name
  19. * @return mixed
  20. */
  21. public function getFlyweight($name)
  22. {
  23. if (!isset($this->flyweights[$name])) {
  24. $this->flyweights[$name] = new ConcreteFlyweight($name);
  25. }
  26. return $this->flyweights[$name];
  27. }
  28. }

客户端调用

  1. <?php
  2. /**
  3. * Client.php
  4. *
  5. * @author Luffy <lufei@swoole.com>
  6. * @date 2019/12/9
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Flyweight;
  11. include __DIR__ . '/../vendor/autoload.php';
  12. class Client
  13. {
  14. public static function run()
  15. {
  16. $flyweight = new FlyweightFactory();
  17. $obj1 = $flyweight->getFlyweight("⭐️");
  18. $obj1->get('第1个');
  19. $obj2 = $flyweight->getFlyweight("⭐️");
  20. $obj2->get('第50个');
  21. var_dump($obj1 === $obj2);
  22. $obj3 = $flyweight->getFlyweight("🍎");
  23. $obj3->get('第1个');
  24. // 不共享的
  25. $obj4 = new UnsharedConcreteFlyweight("🍍");
  26. $obj4->get("一");
  27. $obj4->del();
  28. $obj4->get("一");
  29. }
  30. }
  31. Client::run();

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

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