Example (MVC)

When software design patterns are presented in object-oriented book, example source code is usually included. This source code is a simplest implementation of the pattern that demonstrate its usage without polluting the code with needless implementation specific details.


Model.java

View.java

 Controller.java

 

public class Model {
    int data;
    public Model(){
    }
    public int getData(){
        return data;
    }
    public void setData(int i){
        data = i;
    }
}

public class View {
    private Model model; 
    public View(Model m){
        model = m;
    }
    public void display(){
        System.out.println(model.getData());
    }
}
public class Controller {
    private Model model;
    public Controller(Model m){
        model = m;
    }
    public void change(int k){
        model.setData(k);
    }
}