打赏

策略模式

策略模式

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

 

posted on 2021-08-22 11:52  头大的冯冯  阅读(8)  评论(0编辑  收藏  举报

导航