设计模式九:策略模式

策略模式:

  一个系统有许多类,根据行为区分作用。我们把他们一个个的封装成类,使得可以替换使用。

<?php

/**
 * 方法接口
 */
interface PayInterface{
    public function pay();
}

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

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

/**
 * 业务类
 */
class Pay{

    public function orderPay(PayInterface $pay)
    {
        return $pay->pay();    
    }
}

$pay = new Pay();
//这里可以切换要使用的方法类
$pay->orderPay(new AliPay());

posted @ 2021-08-26 10:58  wish_yang  阅读(33)  评论(0编辑  收藏  举报