[关闭]
@Chiang 2020-01-03T10:17:57.000000Z 字数 1409 阅读 1404

结构型--外观模式

设计模式


  • 外观对象
  • 子系统

外观模式

外观对象

  1. <?php
  2. /**
  3. * Facade
  4. *
  5. * @author Luffy <lufei@swoole.com>
  6. * @date 2019/11/29
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Facade;
  11. class Facade
  12. {
  13. /**
  14. * @var People
  15. */
  16. private $people;
  17. /**
  18. * @var Package
  19. */
  20. private $package;
  21. /**
  22. * Facade constructor.
  23. */
  24. public function __construct()
  25. {
  26. $this->people = new People();
  27. $this->package = new Package();
  28. }
  29. /**
  30. * 寄快递
  31. */
  32. public function send()
  33. {
  34. $this->people->say();
  35. $this->package->pickup();
  36. $this->package->inspection();
  37. $this->package->bale();
  38. $this->package->sendOut();
  39. }
  40. }

子系统->人

  1. <?php
  2. /**
  3. * People
  4. *
  5. * @author Luffy <lufei@swoole.com>
  6. * @date 2019/11/29
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Facade;
  11. class People
  12. {
  13. public function say()
  14. {
  15. echo "下单寄快递\n";
  16. }
  17. }

子系统->快递服务

  1. <?php
  2. /**
  3. * Package
  4. *
  5. * @author Luffy <lufei@swoole.com>
  6. * @date 2019/11/29
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Facade;
  11. class Package
  12. {
  13. public function pickup()
  14. {
  15. echo "上门取件\n";
  16. }
  17. public function inspection()
  18. {
  19. echo "验视\n";
  20. }
  21. public function bale()
  22. {
  23. echo "打包\n";
  24. }
  25. public function sendOut()
  26. {
  27. echo "寄出\n";
  28. }
  29. }

客户端调用

  1. <?php
  2. /**
  3. * Client
  4. *
  5. * @author Luffy <lufei@swoole.com>
  6. * @date 2019/11/29
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Facade;
  11. include __DIR__ . '/../vendor/autoload.php';
  12. class Client
  13. {
  14. public static function run()
  15. {
  16. $facade = new Facade();
  17. $facade->send();
  18. }
  19. }
  20. Client::run();

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

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