2016-06-11 48 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)); 
        } 
    
    } 
    

所以,你可以通過只實現該接口,因爲你需要增加更多的運營商和你只有改變工廠類。其餘的不應該改變。

希望它給你一個想法如何實現。