For a programmer who knows nothing about design patterns, maintenance costs are a deadly problem, so from now on, I'm going to start working hard, designing patterns, and documenting my learning outcomes.
Production class
- Abstract class Oper Abstraction classes
- Class Add Plus
- Class Sub minus
- Class Mul Multiply
- Class Div in addition to
- Class Operate Calculation
abstract class Oper { public $num 1 = 0; public $num 2 = 0; abstract public function GetResult ();}
class extends Oper { publicfunction getresult () { $result$this- >NUM1 +$this-num2; return $result ; }}
。
。
。
class extends Oper { publicfunction getresult () { if($this Num2==0) { throw_exception (' denominator cannot be zero '); } $result $this->num1/$this, num2; return $result ; }}
classOperate { Public functionOper$operate){ Switch($operate){ Case' + ':return NewAdd (); Break; Case‘-‘:return NewSub (); Break; Case‘*‘:return NewMul (); Break; Case‘/‘:return NewDiv (); Break; default:Throw_exception (' Symbol not supported '); Break; } }}
"Simple Factory mode" a simple calculator