策略模式

策略模式

<?php

interface PayInterface
{
    public function pay();
}

class AliPay implements PayInterface
{
    public function pay()
    {
        echo '支付宝支付';
    }
}

class TenPay implements PayInterface
{
    public function pay()
    {
        echo '腾讯支付';
    }
}

class Pay
{
    protected $pay;

    public function __construct(PayInterface $pay)
    {
        $this->pay = $pay;
    }

    public function getPay()
    {
        return $this->pay;
    }
}

$pay = new pay(new AliPay());
$pay->getPay()->pay();

// $pay = new pay(new TenPay());
// $pay->getPay()->pay();

 

上一篇:多态


下一篇:PHP开发之微信公众号中进行支付宝支付(二)