【问题标题】:Optimized way to create a text menu without creating new objects在不创建新对象的情况下创建文本菜单的优化方式
【发布时间】:2017-02-10 19:43:22
【问题描述】:

我有一个带有基于文本的用户菜单的小程序。一个 switch case 正在为我做输入验证,因为用户只输入我验证的整数。

菜单如下所示:

----------------------
(1) Manage articles
(2) Manage customers
(3) Close
----------------------
Please enter your number:

这个菜单是通过一个额外的方法创建的。
当我输入1 时,我调用另一个包含另一个do-while 循环的方法,这次验证文章菜单等等。第二个菜单如下所示:

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);
    }
}

【问题讨论】:

  • 制作方法static.
  • 已经考虑过了,但这不是很糟糕的风格吗?
  • 不一定。您没有显示您的代码,所以我们无法知道。
  • 我添加了一个要点,现在可能更清楚了。
  • 问题中的邮政编码,而不是外部来源。

标签: java user-interface optimization menu


【解决方案1】:

但是要切换回上一个菜单,我总是创建一个新的类实例并调用第一个方法来显示第一个菜单。

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

设计

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

  1. 静态方法(实用程序/处理程序/管理器...)

    public class UserInterface {
    
        public static void main(String[] args) {
    
            UserInterface.showDefaultInterface();
        }
    
        private static void showDefaultInterface() {
    
            // ...
            showArtikelInterface();
            // ...
        }
    
        private static void showArtikelInterface() {
    
            // ...
            showDefaultInterface();
            // ...
        }
    }
    

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

    interface Context {
    
        void show();
    }
    

    class UserInterface {
    
        static void showContext(Context context) { context.show(); }
    } 
    
  2. 实例方法

    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);

showArtikelInterface 方法相同。实例可以访问自身,因此在您的情况下没有理由这样做(实例方法将采用其类型的实例的情况可以在您具有链/链接/节点的数据结构中......)。

循环条件

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

while (scanner.nextInt() != 5); // and the other one

应该是> 5< 1。虽然我猜这只是你当前的测试代码。

共享资源

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

【讨论】:

  • 非常感谢!由于“静态是邪恶的”这句话,我一直认为我应该避免使用静态。
  • @Zumarta 在涉及目标领域时,我倾向于避免使用流行语 :)
  • 你到底是什么意思? :)
  • @Zumarta “静态是邪恶的。”是一个流行语,避免那些。
猜你喜欢
  • 2012-01-31
  • 2023-03-10
  • 2012-04-14
  • 2014-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-15
  • 2018-01-21
相关资源
最近更新 更多