Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC on the command line

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 not be the only but its the main one)

from the IBM site the view has the following definition

The view provides the presentation of the model. It is the look of the application. The view can access the model getters, but it has no knowledge of the setters. In addition, it knows nothing about the controller. The view should be notified when changes to the model occur.

So if i have the following:

Model

  • Person

View

  • EditPersonDetails

Controller

  • PersonController

My person controller could pass a person object to the EditPeronDetails view, however how does my view edit the person if it cannot access its setters, how can i edit the person?

Also does the view class only do one thing? ie can I have a Person View, which has methods for Create, View, Delete, edit the person

many thanks

bones

like image 310
dbones Avatar asked Dec 14 '09 22:12

dbones


1 Answers

define an abstract yet simple MVC program as:

interface Model {
    public void setName(String name);
}

interface View {
    public String prompt(String prompt);
}

class Controller {

    private final Model model;
    private final View view;

    public Controller(Model model, View view) {
        this.model = model;
        this.view = view;
    }

    public void run() {
        String name;

        while ((name = view.prompt("\nmvc demo> ")) != null) {
            model.setName(name);
        }
    }
}

then use the Observer pattern (built-in since JDK 1.0, see here) in order to fill the concrete classes:

class Person extends Observable implements Model {

    private String name;

    public Person() {
    }

    public String getName() {
        return name;
    }

    public void setName(String newName) {
        this.name = newName;
        setChanged();
        notifyObservers(newName);
    }
}

class TUI implements Observer, View { // textual UI

    private final BufferedReader br;

    public TUI(Reader reader) {
        this.br = new BufferedReader(reader);
    }

    public void update(Observable o, Object arg) {
        System.out.println("\n => person updated to " + arg);
    }

    public String prompt(String prompt) {
        try {
            System.out.print(prompt);
            return br.readLine();
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }
}

The main class, that is only responsible to build and connect together the components:

TUI view = new TUI(new StringReader("David\nDamian\nBob\n"));
Person model = new Person();
model.addObserver(view);
Controller controller = new Controller(model, view);
controller.run();

the ouput of this program is:

mvc demo> 
 => person updated to David

mvc demo> 
 => person updated to Damian

...
like image 119
dfa Avatar answered Sep 28 '22 05:09

dfa