[关闭]
@Chiang 2019-12-30T17:03:09.000000Z 字数 2007 阅读 424

结构型--装饰器模式

设计模式


装饰器模式

饮品接口

  1. <?php
  2. /**
  3. * Drinks.php
  4. *
  5. * @author Luffy (lufei@swoole.com)
  6. * @date 2019/11/15
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Decorator;
  11. interface Drinks
  12. {
  13. public function name();
  14. public function price();
  15. }

茶类 实现饮品接口

  1. <?php
  2. /**
  3. * Tea.php
  4. *
  5. * @author Luffy (lufei@swoole.com)
  6. * @date 2019/11/15
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Decorator;
  11. class Tea implements Drinks
  12. {
  13. public function name()
  14. {
  15. return '茶';
  16. }
  17. public function price()
  18. {
  19. return 10;
  20. }
  21. }

装饰器抽象类 实现饮品接口 茶类注入

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

红茶 继承装饰器

  1. <?php
  2. /**
  3. * BlackTea.php 红茶
  4. *
  5. * @author Luffy (lufei@swoole.com)
  6. * @date 2019/11/15
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Decorator;
  11. class BlackTea extends Decorator
  12. {
  13. public function name()
  14. {
  15. return '柠檬红'.$this->drinks->name();
  16. }
  17. public function price()
  18. {
  19. return $this->drinks->price() + 5;
  20. }
  21. }

奶茶 继承装饰器

  1. <?php
  2. /**
  3. * MilkTea.php 奶茶
  4. *
  5. * @author Luffy (lufei@swoole.com)
  6. * @date 2019/11/15
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Decorator;
  11. class MilkTea extends Decorator
  12. {
  13. public function name()
  14. {
  15. return '珍珠奶'.$this->drinks->name();
  16. }
  17. public function price()
  18. {
  19. return $this->drinks->price() + 8;
  20. }
  21. }

客户端 对象注入

  1. <?php
  2. /**
  3. * Client.php
  4. *
  5. * @author Luffy (lufei@swoole.com)
  6. * @date 2019/11/15
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Decorator;
  11. require __DIR__ . '/../vendor/autoload.php';
  12. class Client
  13. {
  14. public static function run()
  15. {
  16. $tea = new Tea();
  17. echo $tea->name();
  18. echo $tea->price()."元\n";
  19. $blackTea = new BlackTea($tea);
  20. echo $blackTea->name();
  21. echo $blackTea->price()."元\n";
  22. $milkTea = new MilkTea($tea);
  23. echo $milkTea->name();
  24. echo $milkTea->price()."元\n";
  25. }
  26. }
  27. Client::run();

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

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