php适配器模式(adapter pattern)

Posted aguncn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php适配器模式(adapter pattern)相关的知识,希望对你有一定的参考价值。

下午陪家人和小孩,晚上练起来。

<?php
/*
The adapter pattern allows the interface of an existing class to be used from another
interface, basically, helping two incompatible interfaces to work together by
converting the interface of one class into an interface expected by another class.
*/

class Stripe 
    public function capturePayment($amount) 
        echo $amount . " Stripe_capturePayment<br/>";
    
    
    public function authorizeOnlyPayment($amount) 
        echo $amount . " Stripe_authorizeOnlyPayment<br/>";
    
    
    public function cancelAmount($amount) 
        echo $amount . " Stripe_cancelAmount<br/>";
    


interface PaymentService 
    public function capture($amount);
    public function authorize($amount);
    public function cancel($amount);


class StripePaymentServiceAdapter implements PaymentService 
    private $stripe;
    
    public function __construct(Stripe $stripe) 
        $this->stripe = $stripe;
    
    
    public function capture($amount) 
        $this->stripe->capturePayment($amount);
    
    public function authorize($amount)
        $this->stripe->authorizeOnlyPayment($amount);
    
    public function cancel($amount) 
        $this->stripe->cancelAmount($amount);
    


$stripe = new StripePaymentServiceAdapter(new Stripe());
$stripe->authorize(49.99);
$stripe->capture(19.99);
$stripe->cancel(9.99);
?>

技术图片

以上是关于php适配器模式(adapter pattern)的主要内容,如果未能解决你的问题,请参考以下文章

适配器模式(Adapter Pattern)

适配器模式(Adapter Pattern)

设计模式Adapter Pattern适配器模式

适配器模式(Adapter Pattern)

Java设计模式—适配器模式(adapter pattern)

适配器模式(Adapter Pattern)