PHP基于工厂模式实现的计算器实例
本文实例讲述了PHP基于工厂模式实现的计算器。分享给大家供大家参考。具体如下:
abstractclassCalculator { private$number1; private$number2; public$result; /** *@returnthe$number2 */ publicfunctiongetNumber2(){ return$this->number2; } /** *@paramfield_type$number2 */ publicfunctionsetNumber2($number2){ $this->number2=$number2; } /** *@returnthe$number1 */ publicfunctiongetNumber1(){ return$this->number1; } /** *@paramfield_type$number1 */ publicfunctionsetNumber1($number1){ $this->number1=$number1; } abstractfunctionget_result(); } classAddextendsCalculator { publicfunctionget_result($number1,$number2) { return$number1+$number2; } } classSubextendsCalculator { publicfunctionget_result($number1,$number2) { return$number1-$number2; } } classMulextendsCalculator { publicfunctionget_result($number1,$number2) { return$number1*$number2; } } classDivextendsCalculator { publicfunctionget_result($number1,$number2) { return$number1/$number2; } } classFactory { publicfunctionBuilding($notes) { if($notes=="+") { $add=newAdd(); return$add; } elseif($notes=="-") { $sub=newSub(); return$sub; } elseif($notes=="*") { $mul=newMul(); return$mul; } else { $div=newDiv(); return$div; } } }
希望本文所述对大家的php程序设计有所帮助。