2016-06-11 49 views
-1

我已经为一项任务编写了这段代码,我希望它能够很好地分解。基本上,这是一个简单的老派计算器的一部分,执行加法,减法,乘法,除法(在执行除法时,应始终显示提醒)。我们需要为每个操作分别设置不同的类(加法,减法,乘法,除法,但我已经介绍了一个 - 提醒)。你有什么建议吗?或者你在我对Java泛型概念的理解上看到一些差距?更好地构建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!"); 
     } 
    } 

    } 
} 
+6

http://codereview.stackexchange.com/ – Tom

+0

将'ADD'等改为一个枚举,并在枚举上实现特定于操作符的逻辑作为方法。 –

回答

0

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

由于从视代码审查点:

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

下可能会给你的想法,你会如何重构你的类设计:

  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)); 
        } 
    
    } 
    

所以,你可以通过只实现该接口,因为你需要增加更多的运营商和你只有改变工厂类。其余的不应该改变。

希望它给你一个想法如何实现。