[关闭]
@Chiang 2019-12-30T16:41:54.000000Z 字数 2972 阅读 476

结构型--桥接模式

设计模式


桥接模式

颜色抽象类

  1. <?php
  2. /**
  3. * Color.php 颜色抽象类
  4. *
  5. * @author Luffy (lufei@swoole.com)
  6. * @date 2019/11/11
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Bridge;
  11. abstract class Color
  12. {
  13. abstract public function output();
  14. }

红色类

  1. <?php
  2. /**
  3. * RedColor.php 红色
  4. *
  5. * @author Luffy (lufei@swoole.com)
  6. * @date 2019/11/11
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Bridge;
  11. class RedColor extends Color
  12. {
  13. public function output()
  14. {
  15. return "红色";
  16. }
  17. }

绿色类

  1. <?php
  2. /**
  3. * GreenColor.php 绿色
  4. *
  5. * @author Luffy (lufei@swoole.com)
  6. * @date 2019/11/11
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Bridge;
  11. class GreenColor extends Color
  12. {
  13. public function output()
  14. {
  15. return "绿色";
  16. }
  17. }

黄色类

  1. <?php
  2. /**
  3. * YellowColor.php 黄色
  4. *
  5. * @author Luffy (lufei@swoole.com)
  6. * @date 2019/11/11
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Bridge;
  11. class YellowColor extends Color
  12. {
  13. public function output()
  14. {
  15. return "黄色";
  16. }
  17. }

形状抽象类 注入颜色类

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

圆形

  1. <?php
  2. /**
  3. * Circle.php 圆形
  4. *
  5. * @author Luffy (lufei@swoole.com)
  6. * @date 2019/11/11
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Bridge;
  11. class Circle extends Shape
  12. {
  13. public function run()
  14. {
  15. echo "{$this->color->output()} 圆形\n";
  16. }
  17. }

正方形

  1. <?php
  2. /**
  3. * Square.php 正方形
  4. *
  5. * @author Luffy (lufei@swoole.com)
  6. * @date 2019/11/11
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Bridge;
  11. class Square extends Shape
  12. {
  13. public function run()
  14. {
  15. echo "{$this->color->output()} 正方形\n";
  16. }
  17. }

三角形

  1. <?php
  2. /**
  3. * Triangle.php 三角形
  4. *
  5. * @author Luffy (lufei@swoole.com)
  6. * @date 2019/11/11
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Bridge;
  11. class Triangle extends Shape
  12. {
  13. public function run()
  14. {
  15. echo "{$this->color->output()} 三角形\n";
  16. }
  17. }

客户端调用

  1. <?php
  2. /**
  3. * Client.php
  4. *
  5. * @author Luffy (lufei@swoole.com)
  6. * @date 2019/11/11
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Bridge;
  11. require __DIR__ . '/../vendor/autoload.php';
  12. class Client
  13. {
  14. public function run()
  15. {
  16. $red = new RedColor();
  17. $yellow = new YellowColor();
  18. $green = new GreenColor();
  19. // 红色的正方形
  20. $redSquare = new Square($red);
  21. $redSquare->run();
  22. // 黄色的正方形
  23. $yellowSquare = new Square($yellow);
  24. $yellowSquare->run();
  25. // 绿色的正方形
  26. $greenSquare = new Square($green);
  27. $greenSquare->run();
  28. // 红色的三角形
  29. $redTriangle = new Triangle($red);
  30. $redTriangle->run();
  31. // 黄色的三角形
  32. $yellowTriangle = new Triangle($yellow);
  33. $yellowTriangle->run();
  34. // 绿色的三角形
  35. $greenTriangle = new Triangle($green);
  36. $greenTriangle->run();
  37. // 红色的圆形
  38. $redCircle = new Circle($red);
  39. $redCircle->run();
  40. // 黄色的圆形
  41. $yellowCircle = new Circle($yellow);
  42. $yellowCircle->run();
  43. // 绿色的圆形
  44. $greenCircle = new Circle($green);
  45. $greenCircle->run();
  46. }
  47. }
  48. $client = new Client();
  49. $client->run();

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

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