面向对象
封装
实现一个简单的计算器功能
class Program { public function calculate() { $number_a = 12; $number_b = 5; $operate = '+'; $result = 0; switch ($operate) { case '+': $result = $number_a + $number_b; break; case '-': $result = $number_a - $number_b; break; case '*': $result = $number_a * $number_b; break; case '/': $result = $number_a / $number_b; break; default: # code... break; } echo $number_a . $operate . $number_b .'='. $result; } } $program = new Program(); $program->calculate(); /** 输出结果: 12+5=17 **/
虽然能够满足需求,但假设另外一个地方也需要用到计算,那么你这部分的代码无法直接被其他人调用,这个时候我们需要对代码进行封装
class Program { public function calculate() { $number_a = 12; $number_b = 5; $operate = '+'; Operation::operate($number_a, $number_b, $operate); } } class Operation { public static function operate($number_a, $number_b, $operate) { $result = 0; switch ($operate) { case '+': $result = $number_a + $number_b; break; case '-': $result = $number_a - $number_b; break; case '*': $result = $number_a * $number_b; break; case '/': $result = $number_a / $number_b; break; default: # code... break; } echo $number_a . $operate . $number_b .'='. $result; } } $program = new Program(); $program->calculate(); /** 输出结果: 12+5=17 **/
对代码进行封装后,不单单自己能调用,其他人也能调用 Operation 类的 operate 方法
而不用去管 Program 类的 calculate 方法里面具体实现了什么方法
Operation::operate(1, 2, '*');
在实际开发中,也需要注意自己当前开发的需求,部分业务逻辑代码能否封装供其他人调用
继承和多态
每个 case 其实都是在做一件事情就是计算,实际开发中每个 case 里面的业务代码肯定不仅仅是一行代码
所以应该再把每个操作进行解耦,这样 Program 类的 calculate 方法只需要调用和传参,并不需要关注每个方法里面具体做了什么操作
通过接口,规范子类实现统一的方法,在类外调用参数修改,而最后的结果也会得到更改来实现多态。
也实现了一个简单的工厂模式,用来解决对象的创建问题
class OperationFathe { public $numberA; public $numberB; } interface Operation { public function getResult(); public function log(); } class OperationAdd extends OperationFathe implements Operation { public function __construct($number_a, $number_b) { $this->numberA = $number_a; $this->numberB = $number_b; } public function getResult() { return $this->numberA + $this->numberB; } public function log() { echo '调用了加法:'; } } class OperationSub extends OperationFathe implements Operation { public function __construct($number_a, $number_b) { $this->numberA = $number_a; $this->numberB = $number_b; } public function getResult() { return $this->numberA - $this->numberB; } public function log() { echo '调用了减法:'; } } class OperationMul extends OperationFathe implements Operation { public function __construct($number_a, $number_b) { $this->numberA = $number_a; $this->numberB = $number_b; } public function getResult() { return $this->numberA * $this->numberB; } public function log() { echo '调用了乘法:'; } } class OperationDiv extends OperationFathe implements Operation { public function __construct($number_a, $number_b) { $this->numberA = $number_a; $this->numberB = $number_b; } public function getResult() { return $this->numberA / $this->numberB; } public function log() { echo '调用了除法:'; } } class Program { public function factory($number_a, $number_b, $operate) { switch ($operate) { case '+': $obj = new OperationAdd($number_a, $number_b); break; case '-': $obj = new OperationSub($number_a, $number_b); break; case '*': $obj = new OperationMul($number_a, $number_b); break; case '/': $obj = new OperationDiv($number_a, $number_b); break; default: # code... break; } return $obj; } } $number_a = 12; $number_b = 5; $operate = '+'; $program = new Program(); $obj = $program->factory($number_a, $number_b, $operate); $obj->log(); echo $obj->getResult(); /** 输出结果: 调用了加法:17 **/