java - 有什么办法可以让这个java调用变小吗?

标签 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,所有这些都要求用户提供相同的输入,但每个方法都会调用不同类的不同方法。

提前致谢,我希望我已经清楚地解释了自己。

编辑:我之前忘了提及,我的主类中有三个按钮,它们分别调用这三个方法。例如我的按钮A调用方法类A,按钮B调用方法类B,按钮C调用方法类C。

最佳答案

您可以只在方法中提供一个输入开关,因此它会类似于

 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”,您只需修改将这些大小写添加到开关即可。

关于java - 有什么办法可以让这个java调用变小吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21440902/

相关文章:

java - 接口(interface)类型的变量

java - 在 Java 中实现定时器事件的最简单方法

java - 在 Java 中,一段代码在一个新的操作之后做了什么?

java - 如何在mouseDragged事件下的DRAG_LAYER上添加JLabel

Java Swing 定时器倒计时

c++ - 使用 C++ 类方法作为函数回调

java - 如何在Avro模式中避免Map of Map

java - 尝试传递二维数组时出现不兼容类型错误

python - 在 Python 中更改了对象

java - 使用 servlet 接收音频文件