• PHP策略模式demo


    <?php
    //策略模式就是你有很多的方法,选择一种适合自己的,
    // 单例模式就是只有一个实例对象,不需要每个文件都要加载,比如连接数据库,
    // 工厂模式就是
    //策略模式
    interface chargeStrategy{//抽象策略类
    public function charge();
    }
    class Apicharge implements chargeStrategy{//具体策略类
    public function charge(){
    echo "支付宝支付";
    }
    }
    class Wxcharge implements chargeStrategy{
    public function charge(){
    echo "微信支付";
    }
    }

    class pzhang{
    private $charge;
    // public function __construct(ChargeStrategy $chargeStrategy){
    // $this->charge = $chargeStrategy;
    // }
    public function payment($channel){
    if($channel == 'Apicharge'){
    $this->charge = new Apicharge();
    }else if($channel == 'Wxcharge'){
    $this->charge = new Wxcharge();
    }else{
    $this->charge = null;
    }
    }
    public function charge(){
    if(is_null($this->charge)){
    exit('00');
    }
    $this->charge->charge();
    }
    }
    //$channel = trim($_GET['channel']);
    $obj = new pzhang();
    $obj->payment('Apicharge');
    $obj->charge();
    ?>
  • 相关阅读:
    日期类型存储方法
    Log4j2的一些记录
    【Maven】学习记录
    HTML 图片加载问题
    浏览器的组成
    javascript数组的实例属性(方法)
    javascript数组的内置对象Array
    javascript之this
    css的position,float属性的理解
    简单介绍帧动画
  • 原文地址:https://www.cnblogs.com/isuansuan/p/9760419.html
Copyright © 2020-2023  润新知