码迷,mamicode.com
首页 > Web开发 > 详细

php适配器模式(adapter pattern)

时间:2019-07-13 19:55:57      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:span   ace   ica   适配   ble   new   cap   rip   rom   

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

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

标签:span   ace   ica   适配   ble   new   cap   rip   rom   

原文地址:https://www.cnblogs.com/aguncn/p/11181723.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!