MVC on the command line

后端 未结 4 1339
后悔当初
后悔当初 2021-02-05 19:58

I am looking into the MVC from a Command line point of view (not web and no framework).. nice and simple. the only thing that confuses me is the View part of this? (well it may

4条回答
  •  攒了一身酷
    2021-02-05 20:31

    I'm working on re-architecting a trivial CLI program and have asked a similar question to myself.

    In researching this, it's apparent that MVC and its derivatives are commonly misused. To complicate things further, variations of MVC (Web MVC comes to mind) exist that don’t adhere to the almost-original Smalltalk-80 implementation and definition of MVC.

    That said, I like dfa's answer with one caveat: the user input should be placed in the controller, not the view. His answer aligns more closely with the Dolphin Smalltalk MVP pattern than MVC.

    "The Controller is a component which responds to user input such as data entry and commands issued from a keyboard or mouse. Its responsibility is to act as a bridge between the human and the application, allowing the user to interact with the screen and data." - source

    As such, I would remove:

    return br.readLine();
    

    from the view and simply have the controller wait for input as follows:

    public void run() {
        view.prompt("\nmvc demo> ");
        String name = br.readLine(); // assume the BufferedReader object is now in the controller
        model.setName(name);
    }
    

    To be honest, I have a hard time justifying this as necessarily better, but thought I would throw out there.

提交回复
热议问题