有没有办法让这个java调用更小?

时间:2014-01-29 19:37:51

标签: java swing methods refactoring

我有一个类,在类中我有三个方法做同样的事情,但提供不同的输入,所以我想知道是否有任何方法可以使这个称为更小。

我的代码;

    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。

2 个答案:

答案 0 :(得分: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”,您需要修改的只是将这些情况添加到交换机。

答案 1 :(得分:0)

我知道他们现在都在同一个班级,但根据他们的调用方式,你可以重构并使用Template Method Design Pattern