加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php适配器模式(adapter pattern)

发布时间:2020-12-13 16:08:02 所属栏目:PHP教程 来源:网络整理
导读:下午陪家人和小孩,晚上练起来。 ? php /* The adapter pattern allows the interface of an existing class to be used from anotherinterface,basically,helping two incompatible interfaces to work together byconverting the interface of one class i

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

<?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);
?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读