50 lines
1.4 KiB
PHP
50 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller\Test;
|
|
|
|
use App\Service\Test\Adapter\CacheService;
|
|
use App\Service\Test\Adapter\PayService;
|
|
use Hyperf\HttpServer\Annotation\Controller;
|
|
use Hyperf\HttpServer\Annotation\RequestMapping;
|
|
|
|
#[Controller(prefix: 'adapter/test')]
|
|
class AdapterTestController
|
|
{
|
|
/**
|
|
* pay 适配器
|
|
* @return array
|
|
*/
|
|
#[RequestMapping(path: 'pay', methods: 'GET')]
|
|
public function pay(): array
|
|
{
|
|
return (new PayService)->handle();
|
|
}
|
|
|
|
/**
|
|
* cache 适配器 => dependencies.php
|
|
* @return array
|
|
*/
|
|
#[RequestMapping(path: 'cache', methods: 'GET')]
|
|
public function cache(): array
|
|
{
|
|
return (new CacheService)->handle();
|
|
}
|
|
|
|
/**
|
|
* 适配器模式(Adapter Pattern)是一种结构型设计模式,它允许不兼容的接口之间能够协同工作。
|
|
* 实现适配器模式的关键步骤:
|
|
*
|
|
* 定义目标接口(客户端期望的接口)
|
|
* 创建被适配的类(需要被适配的现有类)
|
|
* 创建适配器类,实现目标接口并包装被适配的类
|
|
* 通过依赖注入容器管理适配器的创建和绑定
|
|
* 适配器模式在以下场景特别有用:
|
|
*
|
|
* 需要使用现有类,但其接口与你的代码不兼容
|
|
* 想要复用一些已经存在的子类,但这些子类缺少一些公共功能
|
|
* 需要为多个不同的子系统提供统一的接口
|
|
*/
|
|
}
|