Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建文本菜单而不创建新对象的优化方法_Java_User Interface_Optimization_Menu - Fatal编程技术网

Java 创建文本菜单而不创建新对象的优化方法

Java 创建文本菜单而不创建新对象的优化方法,java,user-interface,optimization,menu,Java,User Interface,Optimization,Menu,我有一个基于文本的用户菜单的小程序。一个开关案例正在为我进行输入验证,因为用户只输入我验证的整数 菜单如下所示: ---------------------- (1) Manage articles (2) Manage customers (3) Close ---------------------- Please enter your number: Manage articles ---------------------- (1) Show (2) Add (3) Change (4

我有一个基于文本的用户菜单的小程序。一个开关案例正在为我进行输入验证,因为用户只输入我验证的整数

菜单如下所示:

----------------------
(1) Manage articles
(2) Manage customers
(3) Close
----------------------
Please enter your number:
Manage articles
----------------------
(1) Show
(2) Add
(3) Change
(4) Delete
(5) Back
----------------------
Please enter your number:
此菜单是通过额外的方法创建的。
当我输入
1
时,我调用另一个包含另一个do while循环的方法,这次是验证文章菜单,以此类推。第二个菜单如下所示:

----------------------
(1) Manage articles
(2) Manage customers
(3) Close
----------------------
Please enter your number:
Manage articles
----------------------
(1) Show
(2) Add
(3) Change
(4) Delete
(5) Back
----------------------
Please enter your number:
但是为了切换回上一个菜单,我总是创建一个类的新实例,并调用第一个方法来显示第一个菜单

结果是:当我在菜单之间多次切换时,我总是创建一个新的程序实例

我认为有更好的方法来进行这样的多层阅读,我想知道如何在这里表现得更好

更新:

package articlemanagement.menu;

import articlemanagement.model.Artikel;

import articlemanagement.verwaltung.ArticleManagement;

import java.util.LinkedList;
import java.util.Random;
import java.util.Scanner;

public class UserInterface {

    private ArticleManagement articleManagement;

    private UserInterface() {
        this.articleManagement = new ArticleManagement();
    }


    public static void main(String[] args) {
        UserInterface userInterface = new UserInterface();

        userInterface.showDefaultInterface(userInterface);

        System.out.println("See you next time!");
    }

    private void showDefaultInterface(UserInterface userInterface) {
        Scanner scanner = new Scanner(System.in);
        int input = 0;

        do {
            System.out.println("Welcome");
            System.out.println("----------------------");
            System.out.println("(1) Articles");
            System.out.println("(2) Customers");
            System.out.println("(3) Shop");
            System.out.println("(4) End");
            System.out.println("----------------------");
            System.out.print("Please enter a number:");

            input = scanner.nextInt();

            switch (input) {
                case 1:
                    userInterface.showArtikelInterface(userInterface);
                    break;
                case 2:
                    System.out.println("todo");
                    break;
                case 3:
                    System.out.println("todo");
                    break;
                case 4:
                    System.out.println("Bye");
                    break;
                default:
                    break;
            }

        } while (input != 4);

        System.exit(1);
    }

    /**
     * Show overview
     */
    private void showArtikelInterface(UserInterface userInterface) {
        Scanner scanner = new Scanner(System.in);

        do {
            System.out.println("Manage your articles");
            System.out.println("----------------------");
            System.out.println("(1) Show");
            System.out.println("(2) Add");
            System.out.println("(3) Modify");
            System.out.println("(4) Delete");
            System.out.println("(5) Go Back");
            System.out.println("----------------------");
            System.out.print("Bitte geben Sie eine Zahl für Ihr Menü ein:");

            int input = scanner.nextInt();

            switch (input) {
                case 1: // show
                    zeigeArtikel();
                    break;
                case 2: // add
                    System.out.println("todo");
                    break;
                case 3: // modify
                    System.out.println("todo");
                    break;
                case 4: // delete
                    System.out.println("todo");
                    break;
                case 5: // back
                    System.out.println("Going back...");
                    // bad style?
                    userInterface.showDefaultInterface(userInterface);
                    break;
                default:
                    break;
            }

        } while (scanner.nextInt() != 5);
    }
}
但是为了切换回上一个菜单,我总是创建一个类的新实例,并调用第一个方法来显示第一个菜单

您并不是每次都创建一个新实例。您在
main
中只创建了一个实例,就是它。话虽如此,您还是做出了一些奇怪的设计选择

设计 您有一个
UserInterface
类,它能够根据上下文打印一些文本。两种一般设计是:

  • 静态方法(实用程序/处理程序/管理器…)

    或者,如果需要更大的灵活性(似乎不是这样),可以创建一个类型来表示上下文:

  • 实例法

    public class UserInterface {
    
        public static void main(String[] args) {
    
            UserInterface userInterface = new UserInterface();
            userInterface.showDefaultInterface();
        }
    
        private void showDefaultInterface() {
    
            // ...
            showArtikelInterface();
            // ...
        }
    
        private void showArtikelInterface() {
    
            // ...
            showDefaultInterface();
            // ...
        }
    }
    
  • 但是,您有一个将实例传递给自身的组合,这是没有意义的:

    UserInterface userInterface = new UserInterface();
    userInterface.showDefaultInterface(userInterface);
    
    showarticelinterface
    方法相同。实例可以访问自身,因此没有理由在您的案例中这样做(实例方法将获取其类型的实例的案例可以在您具有链/链接/节点的数据结构中…)

    循环条件 另一件奇怪的事情是你的
    while
    条件

    while (scanner.nextInt() != 5); // and the other one
    
    应该是
    >5
    <1
    的位置。虽然我猜这只是您当前的测试代码

    共享资源
    最后一点是,您可以在方法之间共享
    Scanner
    对象,而不是每次创建一个。将其升级到一个字段(
    static
    或不取决于您的设计选择)。

    使用方法
    static
    。已经考虑过这一点,但这不是不好的风格吗?不一定。你没有显示你的代码,所以我们无法知道。我添加了一个要点,也许现在更清楚了。在问题中发布代码,而不是作为外部来源。非常感谢!我一直认为我应该避免静电,因为“静电是邪恶的。”@Zumarta我倾向于避免在客观领域使用流行语:)你到底是什么意思?:)@Zumarta“静态是邪恶的。”是一句流行语,避免使用这些。
    while (scanner.nextInt() != 5); // and the other one