2014-01-29 174 views
0

我有一個類,在類中,我有三種方法做同樣的事情,但提供不同的輸入,所以我想知道是否有任何方法可以使這個更小。有沒有什麼辦法可以讓這個java更小?

我的代碼;

import javax.swing.JFileChooser; 
import javax.swing.JOptionPane; 
import javax.swing.JTextField; 
import javax.swing.filechooser.FileNameExtensionFilter; 

public class test { 

    public void methodclassA() { 

     int result = JOptionPane 
       .showOptionDialog(
         null, 
         "How would you like you insert your data, manually or from a file? ", 
         "Inserting data", JOptionPane.YES_NO_OPTION, 
         JOptionPane.QUESTION_MESSAGE, null, new String[] { 
           "Manual", "From a File" }, 
         JOptionPane.NO_OPTION); 
     if (result == JOptionPane.YES_OPTION) { 

      // Going to call methodA from another class 
     } 

     if (result == JOptionPane.NO_OPTION) { 

      JTextField NameField = new JTextField(); 
      Object[] message = { "Path location:", NameField }; 

      int result2 = JOptionPane.showOptionDialog(null, message, 
        "Inserting data", JOptionPane.YES_NO_OPTION, 
        JOptionPane.QUESTION_MESSAGE, null, new String[] { "Ok", 
          "Locate the file" }, JOptionPane.NO_OPTION); 
     } 
    } 

    public void methodclassB() { 

     int result = JOptionPane 
       .showOptionDialog(
         null, 
         "How would you like you insert your data, manually or from a file? ", 
         "Inserting data", JOptionPane.YES_NO_OPTION, 
         JOptionPane.QUESTION_MESSAGE, null, new String[] { 
           "Manual", "From a File" }, 
         JOptionPane.NO_OPTION); 
     if (result == JOptionPane.YES_OPTION) { 

      // Going to call methodB from another class 
     } 

     if (result == JOptionPane.NO_OPTION) { 

      JTextField NameField = new JTextField(); 
      Object[] message = { "Path location:", NameField }; 

      int result2 = JOptionPane.showOptionDialog(null, message, 
        "Inserting data", JOptionPane.YES_NO_OPTION, 
        JOptionPane.QUESTION_MESSAGE, null, new String[] { "Ok", 
          "Locate the file" }, JOptionPane.NO_OPTION); 
     } 
    } 

    public void methodclassC() { 

     int result = JOptionPane 
       .showOptionDialog(
         null, 
         "How would you like you insert your data, manually or from a file? ", 
         "Inserting data", JOptionPane.YES_NO_OPTION, 
         JOptionPane.QUESTION_MESSAGE, null, new String[] { 
           "Manual", "From a File" }, 
         JOptionPane.NO_OPTION); 
     if (result == JOptionPane.YES_OPTION) { 

      // Going to call methodB from another class 
     } 

     if (result == JOptionPane.NO_OPTION) { 

      JTextField NameField = new JTextField(); 
      Object[] message = { "Path location:", NameField }; 

      int result2 = JOptionPane.showOptionDialog(null, message, 
        "Inserting data", JOptionPane.YES_NO_OPTION, 
        JOptionPane.QUESTION_MESSAGE, null, new String[] { "Ok", 
          "Locate the file" }, JOptionPane.NO_OPTION); 
     } 
    } 

} 

例如,我在課堂上的三種方法是; methodclassA,methodclassB,methodclassC,所有這些都要求用戶輸入相同的內容,但是每個方法都會調用另一個類的不同方法。

在此先感謝,我希望我已經清楚地解釋了我自己。

編輯:我忘了提及之前,我有三個按鈕在我的主類中調用這三種方法。例如我的buttonA調用methodclassA,buttonB調用methodclassB,buttonC調用methodclassC。

+0

請問您能先格式化您的代碼嗎? – alex

+0

格式化代碼 – user3248466

回答

3

你可以只提供一個輸入切換到方法,因此將像

public void methodCaller(char aSwitcher) { 
     int result = JOptionPane.showOptionDialog(null, "How would you like you insert your data, manually or from a file? ", "Inserting data", 
       JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] { "Manual", "From a File" }, JOptionPane.NO_OPTION); 
     if (result == JOptionPane.YES_OPTION) { 
      switch(aSwitcher) 
      { 
       case 'A': 
        //Going to call methodA from another class 
        break; 
       case 'B': 
        //Going to call methodB from another class 
        break; 
       case 'C': 
        //Going to call methodC from another class 
        break; 
       default: 
        throw new IllegalArgumentExcpetion("No method defined for option" + aSwitcher); 
      } 


     } 
     else if (result == JOptionPane.NO_OPTION) { 
      JTextField NameField = new JTextField(); 
      Object[] message = {"Path location:", NameField}; 
      int result2 = JOptionPane.showOptionDialog(null, message, "Inserting data", 
        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] { "Ok", "Locate the file" }, JOptionPane.NO_OPTION); 
     } 
} 

可能是一個更好的辦法來做到這一點,但這至少要保存所有的代碼重複。

然後只是調用使這些替代

methodclassA(); -> methodCaller('A'); 
methodclassB(); -> methodCaller('B'); 
methodclassC(); -> methodCaller('C'); 

也是有利的,這是你可以添加「d」,「E」,「F」,所有你必須修改是這些案件增加開關。

+0

嗨,我忘記提到之前,我有三個按鈕在我的主類調用這三種方法的每一個。這會影響你提供的代碼的行爲嗎? – user3248466

+0

然後,您可以創建一個新的函數,如@JavaDevil,然後使用該選項從三個方法中調用它。 – Trenin

+0

@ user3248466不,請參閱我的編輯以瞭解調用所需方法的更改。 –

0

我知道他們現在都在同一個班,但根據他們的調用方式,您可以重構並使用Template Method Design Pattern

+0

嗨,我現在非常困惑。基本上,我在我的主類中有三個按鈕。 buttonA,buttonB和buttonC。 buttonA調用methodA,buttonB調用methodB和buttonC調用methodC。但我不知道我是如何用建議java devil來做到這一點,我喜歡它,並且更容易理解。 – user3248466

+0

您是否看到@JavaDevil添加的編輯? – mdewitt

相關問題