interface Math{ public function calc($op1,$op2); } class MathAdd implements Math{ public function calc($op1, $op2) { return $op1+$op2; } } class MathMul implements Math{ public function calc($op1, $op2) { return $op1*$op2; } } class MathSub 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); } } $cmath = new CMath('Add'); echo $cmath->calc(1,2);
策略模式
最新推荐文章于 2024-08-23 17:30:51 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)