在OOP PHP中编写计算器类的更简单方法

前端之家收集整理的这篇文章主要介绍了在OOP PHP中编写计算器类的更简单方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我创建了一个名为Calculator的类,它带有加,减,乘和除功能.计算器仅限于添加两个数字并返回结果.
我对OOP比较陌生,想在课堂上得到一些输入,我是否采取了漫长的路线,如果我做的是有另一种简化课程的方法.

这是代码

  1. class Calculator {
  2. private $_val1,$_val2;
  3.  
  4. public function __construct($val1,$val2){
  5. $this->_val1 = $val1;
  6. $this->_val2 = $val2;
  7. }
  8.  
  9. public function add(){
  10. return $this->_val1 + $this->_val2;
  11. }
  12.  
  13. public function subtract(){
  14. return $this->_val1 - $this->_val2;
  15. }
  16.  
  17. public function multiply (){
  18. return $this->_val1 * $this->_val2;
  19. }
  20.  
  21. public function divide () {
  22. return $this->_val1 / $this->_val2;
  23. }
  24. }
  25.  
  26. $calc = new Calculator(3,4);
  27. echo "<p>3 + 4 = ".$calc->add(). "</p>";
  28.  
  29. $calc = new Calculator (15,12);
  30. echo "<p>15 - 12 = ".$calc->subtract(). "</p>";
  31.  
  32. $calc = new Calculator (20,2);
  33. echo "<p> 20 * 2 = ".$calc->multiply(). "</p>";
  34.  
  35. $calc = new Calculator (20,2);
  36. echo "<p> 20 / 2 = ".$calc ->divide(). "</p>";
恕我直言,你应该使用多态性.
This Video可以帮助您理解这一原则

这是我的思维方式.

首先,为您需要的任何操作定义接口

  1. interface OperationInterface
  2. {
  3. public function evaluate(array $operands = array());
  4. }

然后,创建计算器支架

  1. class Calculator
  2. {
  3. protected $operands = array();
  4.  
  5. public function setOperands(array $operands = array())
  6. {
  7. $this->operands = $operands;
  8. }
  9.  
  10. public function addOperand($operand)
  11. {
  12. $this->operands[] = $operand;
  13. }
  14.  
  15. /**
  16. * You need any operation that implement the given interface
  17. */
  18. public function setOperation(OperationInterface $operation)
  19. {
  20. $this->operation = $operation;
  21. }
  22.  
  23. public function process()
  24. {
  25. return $this->operation->evaluate($this->operands);
  26. }
  27. }

然后,您可以定义操作,例如,添加

  1. class Addition implements OperationInterface
  2. {
  3. public function evaluate(array $operands = array())
  4. {
  5. return array_sum($operands);
  6. }
  7. }

你会像以下一样使用它:

  1. $calculator = new Calculator;
  2. $calculator->setOperands(array(4,2));
  3. $calculator->setOperation(new Addition);
  4.  
  5. echo $calculator->process(); // 6

有了这一点,如果要添加任何新行为或修改现有行为,只需创建或编辑一个类.

例如,假设您需要模数运算

  1. class Modulus implements OperationInterface
  2. {
  3. public function evaluate(array $operands = array())
  4. {
  5. $equals = array_shift($operands);
  6.  
  7. foreach ($operands as $value) {
  8. $equals = $equals % $value;
  9. }
  10.  
  11. return $equals;
  12. }
  13. }

然后,

  1. $calculator = new Calculator;
  2. $calculator->setOperands(array(4,2));
  3. $calculator->setOperation(new Addition); // 4 + 2
  4.  
  5. echo $calculator->process(); // 6
  6.  
  7. $calculator->setOperation(new Modulus); // 4 % 2
  8.  
  9. echo $calculator->process(); // 0
  10.  
  11. $calculator->setOperands(array(55,10)); // 55 % 10
  12.  
  13. echo $calculator->process(); // 5

解决方案允许您的代码成为第三方库

如果您计划重用此代码或将其作为库提供,则用户不会修改您的源代码.
但是,如果他想要一个未定义的Substraction或BackwardSubstraction方法呢?

他只需要在他的项目中创建自己的Substraction类,它实现OperationInterface以便与你的库一起工作.

它更容易阅读

在查看项目架构时,更容易看到这样的文件

  1. - app/
  2. - lib/
  3. - Calculator/
  4. - Operation/
  5. - Addition.PHP
  6. - Modulus.PHP
  7. - Substraction.PHP
  8. - OperationInterface.PHP
  9. - Calculator.PHP

并立即知道哪个文件包含所需的行为.

猜你在找的PHP相关文章