From 33cd767477fd25d3702e5997d49ba6f3e51417db Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Sun, 7 Sep 2025 18:22:41 +0800 Subject: [PATCH] feat : Bridge pay --- app/Controller/Test/BridgeController.php | 23 ++++++++ .../Test/Bridge/PaymentInterface.php | 18 ++++++ .../Test/Bridge/Pay/Lib/AlipayPayment.php | 45 +++++++++++++++ .../Test/Bridge/Pay/Lib/CreditCardPayment.php | 45 +++++++++++++++ .../Test/Bridge/Pay/PaymentGateway.php | 53 ++++++++++++++++++ .../Test/Bridge/Pay/PremiumPaymentService.php | 37 +++++++++++++ .../Bridge/Pay/StandardPaymentService.php | 35 ++++++++++++ app/Service/Test/Bridge/PayService.php | 55 +++++++++++++++++++ config/autoload/dependencies.php | 17 ++++++ document/bridge.md | 36 ++++++++++++ request/test.http | 5 ++ 11 files changed, 369 insertions(+) create mode 100644 app/Controller/Test/BridgeController.php create mode 100644 app/Interface/Test/Bridge/PaymentInterface.php create mode 100644 app/Service/Test/Bridge/Pay/Lib/AlipayPayment.php create mode 100644 app/Service/Test/Bridge/Pay/Lib/CreditCardPayment.php create mode 100644 app/Service/Test/Bridge/Pay/PaymentGateway.php create mode 100644 app/Service/Test/Bridge/Pay/PremiumPaymentService.php create mode 100644 app/Service/Test/Bridge/Pay/StandardPaymentService.php create mode 100644 app/Service/Test/Bridge/PayService.php create mode 100644 document/bridge.md diff --git a/app/Controller/Test/BridgeController.php b/app/Controller/Test/BridgeController.php new file mode 100644 index 0000000..5869d02 --- /dev/null +++ b/app/Controller/Test/BridgeController.php @@ -0,0 +1,23 @@ +handle(); + } +} diff --git a/app/Interface/Test/Bridge/PaymentInterface.php b/app/Interface/Test/Bridge/PaymentInterface.php new file mode 100644 index 0000000..7b951ea --- /dev/null +++ b/app/Interface/Test/Bridge/PaymentInterface.php @@ -0,0 +1,18 @@ + 'pay', + 'price' => $amount, + 'pay_type' => 'alipay', + 'msg' => 'Processing credit card payment of ' . $amount . PHP_EOL, + ]; + } + + /** + * @param float $amount + * @return array + */ + public function refund(float $amount): array + { + return [ + 'type' => 'refund', + 'price' => $amount, + 'pay_type' => 'alipay', + 'msg' => 'Processing credit card refund of ' . $amount . PHP_EOL, + ]; + } +} \ No newline at end of file diff --git a/app/Service/Test/Bridge/Pay/Lib/CreditCardPayment.php b/app/Service/Test/Bridge/Pay/Lib/CreditCardPayment.php new file mode 100644 index 0000000..c947a4d --- /dev/null +++ b/app/Service/Test/Bridge/Pay/Lib/CreditCardPayment.php @@ -0,0 +1,45 @@ + 'pay', + 'price' => $amount, + 'pay_type' => 'credit_card', + 'msg' => 'Processing credit card payment of ' . $amount . PHP_EOL, + ]; + } + + /** + * @param float $amount + * @return array + */ + public function refund(float $amount): array + { + return [ + 'type' => 'refund', + 'price' => $amount, + 'pay_type' => 'credit_card', + 'msg' => 'Processing credit card refund of ' . $amount . PHP_EOL, + ]; + } +} \ No newline at end of file diff --git a/app/Service/Test/Bridge/Pay/PaymentGateway.php b/app/Service/Test/Bridge/Pay/PaymentGateway.php new file mode 100644 index 0000000..514d408 --- /dev/null +++ b/app/Service/Test/Bridge/Pay/PaymentGateway.php @@ -0,0 +1,53 @@ +payment = $payment; + $this->logger = $logger; + } + + /** + * 抽象支付类 + * @param float $amount + * @return array + */ + abstract public function processPayment(float $amount): array; + + /** + * 抽象退款类 + * @param float $amount + * @return array + */ + abstract public function processRefund(float $amount): array; +} \ No newline at end of file diff --git a/app/Service/Test/Bridge/Pay/PremiumPaymentService.php b/app/Service/Test/Bridge/Pay/PremiumPaymentService.php new file mode 100644 index 0000000..23ec102 --- /dev/null +++ b/app/Service/Test/Bridge/Pay/PremiumPaymentService.php @@ -0,0 +1,37 @@ +logger->debug('Premium payment processing with fee: ' . $fee); + return $this->payment->pay($amount + $fee); + } + + /** + * @param float $amount + * @return array + */ + public function processRefund(float $amount): array + { + $fee = $amount * 0.01; // 1%手续费 + $this->logger->debug('Premium payment processing with refund: ' . $fee); + return $this->payment->refund($amount - $fee); + } +} \ No newline at end of file diff --git a/app/Service/Test/Bridge/Pay/StandardPaymentService.php b/app/Service/Test/Bridge/Pay/StandardPaymentService.php new file mode 100644 index 0000000..8e333d7 --- /dev/null +++ b/app/Service/Test/Bridge/Pay/StandardPaymentService.php @@ -0,0 +1,35 @@ +logger->debug('Standard payment processing'); + return $this->payment->pay($amount); + } + + /** + * @param float $amount + * @return array + */ + public function processRefund(float $amount): array + { + $this->logger->debug('Standard refund processing'); + return $this->payment->refund($amount); + } +} \ No newline at end of file diff --git a/app/Service/Test/Bridge/PayService.php b/app/Service/Test/Bridge/PayService.php new file mode 100644 index 0000000..f90e8b1 --- /dev/null +++ b/app/Service/Test/Bridge/PayService.php @@ -0,0 +1,55 @@ +request->input('amount',100); + $type = $this->request->input('type','standard'); + + $payment = $type === 'premium' ? $this->premiumPaymentGateway : $this->standardPaymentGateway; + + $payRes = $payment->processPayment($amount); + $refundRes = $payment->processRefund($amount); + + $refundPreRes = $this->premiumPaymentGateway->processRefund($amount); + $payPreRes = $this->premiumPaymentGateway->processPayment($amount); + + return $this->return->success('success',[ + 'pay_res' => $payRes, + 'refund_res' => $refundRes, + 'refund_pre_res' => $refundPreRes, + 'pay_pre_res' => $payPreRes, + ]); + } +} \ No newline at end of file diff --git a/config/autoload/dependencies.php b/config/autoload/dependencies.php index ef8801c..4d0d12e 100644 --- a/config/autoload/dependencies.php +++ b/config/autoload/dependencies.php @@ -11,11 +11,16 @@ declare(strict_types=1); */ use App\Interface\Test\Adapter\CacheInterface; +use App\Interface\Test\Bridge\PaymentInterface; use App\Interface\Test\Decorator\HttpClientInterface; use App\Interface\Test\Decorator\LoggerInterface; use App\Interface\Test\Proxy\UserInfoInterface; use App\Lib\Request\GuzzleHttpClient; use App\Service\Test\Adapter\Cache\RedisCacheService; +use App\Service\Test\Bridge\Pay\Lib\AlipayPayment; +use App\Service\Test\Bridge\Pay\Lib\CreditCardPayment; +use App\Service\Test\Bridge\Pay\PremiumPaymentService; +use App\Service\Test\Bridge\Pay\StandardPaymentService; use App\Service\Test\Decorator\Container\BasicFileLogger; use App\Service\Test\Decorator\Container\CriticalLoggerDecorator; use App\Service\Test\Decorator\Container\IpLoggerDecorator; @@ -47,5 +52,17 @@ return [ $logger = make(BasicFileLogger::class); $cache = make(Cache::class); return make(CacheUserInfoService::class, ['userInfoService' => $service,'logger' => $logger , 'cache' => $cache]); + }, +// PaymentInterface::class => AlipayPayment::class, + PaymentInterface::class => CreditCardPayment::class, + 'standard_payment' => function () { + $paymentInterface = make(PaymentInterface::class); + $logger = make(BasicFileLogger::class); + return new StandardPaymentService($paymentInterface,$logger); + }, + 'premium_payment' => function () { + $paymentInterface = make(PaymentInterface::class); + $logger = make(BasicFileLogger::class); + return new PremiumPaymentService($paymentInterface,$logger); } ]; diff --git a/document/bridge.md b/document/bridge.md new file mode 100644 index 0000000..923800a --- /dev/null +++ b/document/bridge.md @@ -0,0 +1,36 @@ + + +### 桥接模式 (Bridge Pattern) 详解 + +> 桥接模式是一种结构型设计模式,它通过将抽象部分与实现部分分离,使它们可以独立变化而不互相影响。这种模式使用组合关系代替继承关系,从而降低了抽象和实现之间的耦合度。 + +#### 核心概念 +用组合代替继承,将多维度的变化拆分为多个正交的维度,每个维度独立变化。 + +#### 结构 +- 抽象部分(Abstraction): 定义高层的抽象接口/包含一个对实现部分的引用 +- 精确抽象(Refined Abstraction): 扩展抽象部分的变体 +- 实现接口(Implementor): 定义实现类的接口 +- 具体实现(Concrete Implementor): 实现Implementor接口的具体类 + +#### 优缺点 + +##### 优点 +- 分离抽象和实现:可以独立扩展两者 +- 提高可扩展性:可以独立添加新的抽象或实现 +- 避免继承爆炸:通过组合代替多层继承 +- 符合开闭原则:新增维度不影响现有代码 + +##### 缺点 +- 增加系统复杂度:需要正确识别抽象和实现两个维度 +- 对高内聚类不适用:如果抽象和实现本身高度耦合,强行分离反而不好 + +#### 桥接模式 vs 其他模式 + +| 模式 | 区别 | +|--------|-----------------------| +| 适配器模式 | 适配器是事后补救,桥接是事先设计 | +| 抽象工厂模式 | 抽象工厂关注产品族,桥接关注分离抽象与实现 | +| 策略模式 | 策略模式改变行为,桥接模式分离抽象与实现 | + +桥接模式通过这种分离设计,让系统获得更好的扩展性和维护性,是多维度变化场景下的优秀解决方案。 \ No newline at end of file diff --git a/request/test.http b/request/test.http index 4726470..6ffce18 100644 --- a/request/test.http +++ b/request/test.http @@ -50,4 +50,9 @@ Content-Type: application/x-www-form-urlencoded ### Proxy get_user_info test GET {{host}}/proxy/test/get_user_info?user_id=2 +Content-Type: application/x-www-form-urlencoded + + +### Bridge pay test +GET {{host}}/bridge/test/pay Content-Type: application/x-www-form-urlencoded \ No newline at end of file