6、php策略模式

2018-07-14  本文已影响11人  水电梁师傅

有这么一段代码

        <form>
            <input  name="para1"  />
            <select name='cal'>
                <option value="+">+</option>
                <option value="-">-</option>
                <option value="*">*</option>
                <option value="/">/</option>
            </select>
            <input name='para2' />
            <input type="submit" />
        </form>

策略的计算加减乘除

interface Math
{
    public function  cal($a,$b);
}
class addMath implements  Math
{
    public function cal($a,$b)
    {
        echo $a+$b;
    }
}
class subMath implements  Math
{
    public function cal($a,$b)
    {
        echo $a-$b;
    }
}
class divMath implements  Math
{
    public function cal($a,$b)
    {
        echo $a/$b;
    }
}
class mulMath implements  Math
{
    public function cal($a,$b)
    {
        echo $a*$b;
    }
}


class  Cmath
{
    protected $cal =null;
    public function __construct($type)
    {
        $caltype = $type.'Math';
        $this->cal = new $caltype;
//      return 
    }
    public function cal($a,$b)
    {
        $this->cal->cal($a,$b);
    }
    
}
$para1 = $_POST['para1'];
$para2 = $_POST['para2'];

$type = $_POST['cal'];
$cmath = new Cmath($type);
$cmath->cal($para1, $para2);
上一篇 下一篇

猜你喜欢

热点阅读