首页 > 其他 > 详细

策略模式

时间:2019-05-30 15:28:08      阅读:122      评论:0      收藏:0      [点我收藏+]
<?php
//策略模式
interface Math {
    public function calc($op1,$op2);
}

class MathAdd implements Math{
    public function calc($op1,$op2){
        return $op1 + $op2;
    }
}

class MathSub implements Math{
    public function calc($op1,$op2){
        return $op1 - $op2;
    }
}

class MathMul implements Math{
    public function calc($op1,$op2){
        return $op1 * $op2;
    }
}

class MathDiv implements Math{
    public function calc($op1,$op2){
        return $op1 / $op2;
    }
}

class Cmath {
    protected $calc = null;
    
    public function __construct($type){
        $calc = ‘Math‘ . $type;
        $this->calc = new $calc();
    }
    
    public function calc($op1,$op2){
        return $this->calc->calc($op1,$op2);
    }
}
$type = $_POST[‘op‘];
$cmath = new CMath($type);
echo $cmath->calc((int)$_POST[‘op1‘],(int)$_POST[‘op2‘]);

 

策略模式

原文:https://www.cnblogs.com/nr-zhang/p/10949761.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!