【问题标题】:better structuring the Java code更好地构建 Java 代码
【发布时间】:2016-06-11 12:40:54
【问题描述】:

我已经为一个作业编写了这段代码,我希望它能够被很好地分解。基本上,这是一个简单的老式计算器的一部分,用于执行加法、减法、乘法、除法(执行除法时,应始终显示提示)。我们需要为每个操作(加法、减法、乘法、除法)设置单独的类,但我已经介绍了另外一个 - 提醒)。您有什么建议,或者您是否发现我对 Java 泛型概念的理解存在一些差距?

public class Logic 
       implements LogicInterface {

    private final int ADDITION = 1;
    private final int SUBTRACTION = 2;
    private final int MULTIPLICATION = 3;
    private final int DIVISION = 4;

    /**
     * Reference to the Activity output.
     */
    protected ActivityInterface mOut;

    /**
     * Constructor initializes the field.
     */
    public Logic(ActivityInterface out){
        mOut = out;
    }

    /**
     * Perform the @a operation on @a argumentOne and @a argumentTwo.
     */
    public void process(int argumentOne,
                        int argumentTwo,
                        int operation){

        OperationsInterface operationsInterface =null;

        if(operation==ADDITION)
        {
            operationsInterface = new Add();
        }
        else if(operation==SUBTRACTION)
        {
            operationsInterface = new Subtract();
        }
        else if(operation==MULTIPLICATION)
        {
            operationsInterface = new Multiply();
        }
        else
        {
            operationsInterface = new Divide();
        }

    if(argumentTwo==0 && operation == DIVISION) {
        mOut.print("You cannot divide by zero!");
    }
    else {
        try {
            //get the result
            int result = operationsInterface.process(argumentOne, argumentTwo);
            mOut.print(String.valueOf(result));

            //add the reminder to the output in case we are performing division
            if (operation == DIVISION) {
                operationsInterface = new Reminder();
                mOut.print(result + " R: " + String.valueOf(operationsInterface.process(argumentOne, argumentTwo)));
            }
        }
        catch (Exception exception)
        {
            mOut.print("Something went wrong!");
        }
    }

    }
}

【问题讨论】:

标签: java android generics polymorphism refactoring


【解决方案1】:

我不明白这与泛型有什么关系。

从代码审查的角度来看:

  1. 当您需要扩展某些功能时,您应该始终问问自己更改代码是多么容易。在您的情况下,假设您要添加另一个运算符。您需要添加一个常量并添加另一个 if/else 案例,也许还有其他一些逻辑。我建议将操作符常量映射到操作类,或者为此使用枚举;那么您只需要初始化一次并保存 if/else 情况。
  2. 考虑有不同的Add 类,一个执行简单的加法,另一个打印输出。如果要交换它们,则需要更改new Add() 部分,但是您不能拥有两个计算器,一个带有简单的添加,另一个带有扩展。因此,在某种可以轻松覆盖的工厂方法中使用new 是一种很好的做法,例如protected OperationInterface createAdd() {return new Add();}。然后你可以继承你的计算器并覆盖createAdd()。当然,所有其他运营商也是如此。
  3. 您的OperationInterface 似乎返回了int。我不认为它适用于分裂。至少应为 double
  4. 我会将Reminder 视为Divide 的子类。至少该逻辑仅与除法运算相关,因此应位于Divide 类或其某个子类中。

【讨论】:

    【解决方案2】:

    以下内容可能会让您了解如何重构类设计:

    1. 定义如下接口:

      public interface LogicInterface<T extends Number> {
          T calculate(T operand1, T operand2);
      }
      
    2. 为您的操作实现此接口:

      public class Addition implements LogicInterface<Integer> {
          public Integer calculate(Integer operand1, Integer operand2) {
              return operand1.intValue() + operand2.intValue();
         }
      }
      

      public class Division implements LogicInterface<Integer> {
          public Integer calculate(Integer operand1, Integer operand2) {
              if (operand2 == null) throw new IllegalArgumentException();
      
              return operand1.intValue() / operand2.intValue();
          }
      }
      

      等等

    3. 实现一个工厂:

      public class CalculatorFactory {
          public enum CalculatorType {
              ADD, SUBTRACT, MULTIPLY, DIVIDE, MODULO;  // etc
          }
      
          public static LogicInterface<Integer> getOperator(CalculatorType type) {
              switch (type) {
                  case ADD: return new Addition();
                  case DIVIDE: return new Division();
                  // etc
      
                  default: throw new UnsupportedOperationException("Operation type not supported");
              }
         }
      }
      
    4. 按如下方式使用它:

      public class CalculatorTest {
      
          public static void main(String[] args) {
              LogicInterface<Integer> add =   CalculatorFactory.getOperator(CalculatorType.ADD);
              System.out.println("Sum of 1 and 2: " + add.calculate(14, 16));
          }
      
      }
      

    因此您可以通过根据需要实现接口来添加更多运算符,并且您只需更改工厂类。其余的永远不要改变。

    希望它能让您了解如何实施。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多