[关闭]
@Chiang 2019-12-30T16:09:29.000000Z 字数 2768 阅读 605

结构型--适配器模式

设计模式


适配器模式是将类或者对象结合在一起形成复杂的、功能更为强大的结构
用适配器模式来包装其他一些不兼容的类或对象

应用场景

  • 支付
  • 数据库
  • 复杂业务逻辑接口使用适配器接口对外提供数据

类适配器

数据结构

接口:

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

适配者类

  1. <?php
  2. /**
  3. * 适配者类
  4. *
  5. * @author Luffy (lufei@swoole.com)
  6. * @date 2019/11/1
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Adapter\ClassAdapter;
  11. class DatabaseAdaptee
  12. {
  13. protected $driver = "mysql";
  14. public function set()
  15. {
  16. echo "use {$this->driver}.\n";
  17. }
  18. }

适配器类 继承适配者类实现接口

  1. <?php
  2. /**
  3. * 适配器类
  4. *
  5. * @author Luffy (lufei@swoole.com)
  6. * @date 2019/11/1
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Adapter\ClassAdapter;
  11. class DatabaseAdapter extends DatabaseAdaptee implements DatabaseTarget
  12. {
  13. public function __construct()
  14. {
  15. $this->driver = "pdo";
  16. }
  17. public function get()
  18. {
  19. echo "used {$this->driver}.\n";
  20. }
  21. }

客户类

  1. <?php
  2. /**
  3. * 客户类
  4. *
  5. * @author Luffy (lufei@swoole.com)
  6. * @date 2019/11/1
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Adapter\ClassAdapter;
  11. require __DIR__ . '/../../vendor/autoload.php';
  12. class Client
  13. {
  14. public static function run()
  15. {
  16. // 适配者
  17. $adaptee = new DatabaseAdaptee();
  18. $adaptee->set();
  19. // 适配器
  20. $adapter = new DatabaseAdapter();
  21. $adapter->set();
  22. $adapter->get();
  23. }
  24. }
  25. Client::run();

对象适配器 (耦合度更低)

对象适配器

接口

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

适配者类

  1. <?php
  2. /**
  3. * 适配者类
  4. *
  5. * @author Luffy (lufei@swoole.com)
  6. * @date 2019/11/1
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Adapter\ObjectAdapter;
  11. class DatabaseAdaptee
  12. {
  13. public $driver = "mysql";
  14. public function set()
  15. {
  16. echo "use {$this->driver}.\n";
  17. }
  18. }

适配器类 实现接口

  1. <?php
  2. /**
  3. * 适配器类
  4. *
  5. * @author Luffy (lufei@swoole.com)
  6. * @date 2019/11/1
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Adapter\ObjectAdapter;
  11. class DatabaseAdapter implements DatabaseTarget
  12. {
  13. protected $adaptee;
  14. public function __construct(DatabaseAdaptee $adaptee)
  15. {
  16. $this->adaptee = $adaptee;
  17. $adaptee->driver = "mysqli";
  18. }
  19. public function set()
  20. {
  21. echo "use {$this->adaptee->driver}.\n";
  22. }
  23. public function get()
  24. {
  25. echo "used {$this->adaptee->driver}.\n";
  26. }
  27. }

客户类 对象注入

  1. <?php
  2. /**
  3. * 客户类
  4. *
  5. * @author Luffy (lufei@swoole.com)
  6. * @date 2019/11/1
  7. * @copyright Swoole, Inc.
  8. * @package sy-records/design-patterns
  9. */
  10. namespace Luffy\DesignPatterns\Adapter\ObjectAdapter;
  11. require __DIR__ . '/../../vendor/autoload.php';
  12. class Client
  13. {
  14. public static function run()
  15. {
  16. $adaptee = new DatabaseAdaptee();
  17. $adaptee->set();
  18. // 适配器
  19. $adapter = new DatabaseAdapter($adaptee);
  20. $adapter->set();
  21. $adapter->get();
  22. }
  23. }
  24. Client::run();

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

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