策略模式

策略模式

<?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();
原文地址:https://www.cnblogs.com/clubs/p/15171836.html