feat : Decorator basic

This commit is contained in:
2025-09-05 15:00:28 +08:00
parent d43d38d820
commit 5e8cc5c61e
15 changed files with 264 additions and 10 deletions

View File

@@ -4,13 +4,14 @@ declare(strict_types=1);
namespace App\Controller\Test; namespace App\Controller\Test;
use App\Controller\AbstractController;
use App\Service\Test\Adapter\CacheService; use App\Service\Test\Adapter\CacheService;
use App\Service\Test\Adapter\PayService; use App\Service\Test\Adapter\PayService;
use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\RequestMapping;
#[Controller(prefix: 'adapter/test')] #[Controller(prefix: 'adapter/test')]
class AdapterTestController class AdapterTestController extends AbstractController
{ {
/** /**
* pay 适配器 * pay 适配器

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace App\Controller\Test;
use App\Controller\AbstractController;
use App\Service\Test\Decorator\AopService;
use App\Service\Test\Decorator\BasicService;
use App\Service\Test\Decorator\ContainerService;
use App\Service\Test\Decorator\HttpService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
#[Controller(prefix: 'decorator/test')]
class DecoratorController extends AbstractController
{
/**
* basic 装饰器
* @return array
*/
#[RequestMapping(path: 'basic', methods: 'GET')]
public function basic()
{
return (new BasicService)->handle();
}
public function container()
{
return (new ContainerService)->handle();
}
public function aop()
{
return (new AopService)->handle();
}
public function http()
{
return (new HttpService)->handle();
}
}

View File

@@ -2,7 +2,7 @@
namespace App\Interface\Test\Adapter; namespace App\Interface\Test\Adapter;
interface PaymentGateway interface PaymentGatewayInterface
{ {
public function pay(float $amount): array; public function pay(float $amount): array;
} }

View File

@@ -0,0 +1,12 @@
<?php
namespace App\Interface\Test\Decorator;
interface ComponentInterface
{
/**
* 组件接口
* @return string
*/
public function operation(): string;
}

View File

@@ -11,9 +11,9 @@ declare(strict_types=1);
namespace App\Service\Test\Adapter\Pay; namespace App\Service\Test\Adapter\Pay;
use App\Interface\Test\Adapter\PaymentGateway; use App\Interface\Test\Adapter\PaymentGatewayInterface;
class AlipayAdapter implements PaymentGateway class AlipayAdapter implements PaymentGatewayInterface
{ {
/** /**
* @var AlipayService * @var AlipayService

View File

@@ -11,9 +11,9 @@ declare(strict_types=1);
namespace App\Service\Test\Adapter\Pay; namespace App\Service\Test\Adapter\Pay;
use App\Interface\Test\Adapter\PaymentGateway; use App\Interface\Test\Adapter\PaymentGatewayInterface;
class WechatPayAdapter implements PaymentGateway class WechatPayAdapter implements PaymentGatewayInterface
{ {
/** /**
* @var WechatPayService * @var WechatPayService

View File

@@ -9,13 +9,13 @@
declare(strict_types=1); declare(strict_types=1);
namespace App\Service\Test; namespace App\Service\Test\Decorator;
use App\Service\BaseService; use App\Service\Test\TestBaseService;
class AdapterTestService extends TestBaseService class AopService extends TestBaseService
{ {
public function handle(): array public function handle()
{ {
return $this->return->success(); return $this->return->success();
} }

View File

@@ -0,0 +1,40 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
* @web_site https://ctexthuang.com
*/
declare(strict_types=1);
namespace App\Service\Test\Decorator\Basic;
use App\Interface\Test\Decorator\ComponentInterface;
abstract class BasicDecorator implements ComponentInterface
{
/**
* @var ComponentInterface $component
*/
protected ComponentInterface $component;
/**
* 构造函数
* @param ComponentInterface $component
*/
public function __construct(ComponentInterface $component)
{
$this->component = $component;
}
/**
* 装饰器基类
* @return string
*/
public function operation(): string
{
return $this->component->operation();
}
}

View File

@@ -0,0 +1,26 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
* @web_site https://ctexthuang.com
*/
declare(strict_types=1);
namespace App\Service\Test\Decorator\Basic;
use App\Interface\Test\Decorator\ComponentInterface;
class ConcreteComponentService implements ComponentInterface
{
/**
* 组件
* @return string
*/
public function operation(): string
{
return 'ConcreteComponentService';
}
}

View File

@@ -0,0 +1,24 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
* @web_site https://ctexthuang.com
*/
declare(strict_types=1);
namespace App\Service\Test\Decorator\Basic;
class ConcreteDecoratorAService extends BasicDecorator
{
/**
* A 类装饰器组件逻辑
* @return string
*/
public function operation(): string
{
return 'ConcreteDecoratorAService(' . parent::operation() . ')';
}
}

View File

@@ -0,0 +1,24 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
* @web_site https://ctexthuang.com
*/
declare(strict_types=1);
namespace App\Service\Test\Decorator\Basic;
class ConcreteDecoratorBService extends BasicDecorator
{
/**
* B 类装饰器组件逻辑
* @return string
*/
public function operation(): string
{
return 'ConcreteDecoratorBService(' . parent::operation() . ')';
}
}

View File

@@ -0,0 +1,36 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
* @web_site https://ctexthuang.com
*/
declare(strict_types=1);
namespace App\Service\Test\Decorator;
use App\Service\Test\Decorator\Basic\ConcreteComponentService;
use App\Service\Test\Decorator\Basic\ConcreteDecoratorAService;
use App\Service\Test\Decorator\Basic\ConcreteDecoratorBService;
use App\Service\Test\TestBaseService;
class BasicService extends TestBaseService
{
/**
* @return array
*/
public function handle(): array
{
$simple = new ConcreteComponentService();
$decorator1 = new ConcreteDecoratorAService($simple);
$decorator2 = new ConcreteDecoratorBService($decorator1);
return $this->return->success('success',[
'simple' => $simple->operation(),
'decorator1' => $decorator1->operation(),
'decorator2' => $decorator2->operation()
]);
}
}

View File

@@ -0,0 +1,22 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
* @web_site https://ctexthuang.com
*/
declare(strict_types=1);
namespace App\Service\Test\Decorator;
use App\Service\Test\TestBaseService;
class ContainerService extends TestBaseService
{
public function handle()
{
return $this->return->success();
}
}

View File

@@ -0,0 +1,22 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
* @web_site https://ctexthuang.com
*/
declare(strict_types=1);
namespace App\Service\Test\Decorator;
use App\Service\Test\TestBaseService;
class HttpService extends TestBaseService
{
public function handle()
{
return $this->return->success();
}
}

View File

@@ -11,3 +11,8 @@ account=13632877014&password=123456
### Adapter cache test ### Adapter cache test
GET {{host}}/adapter/test/cache GET {{host}}/adapter/test/cache
Content-Type: application/x-www-form-urlencoded Content-Type: application/x-www-form-urlencoded
### Decorator basic test
GET {{host}}/decorator/test/basic
Content-Type: application/x-www-form-urlencoded