MVC(Model-View-Controller) 패턴


MVC 패턴에 대한 내용입니다.
아래 내용은 http://en.wikipedia.org/wiki/Model-view-controller 에서 정의한 내용입니다.

Model
The domain-specific representation of the information on which the application operates. Domain logic adds meaning to raw data (e.g., calculating if today is the user’s birthday, or the totals, taxes, and shipping charges for shopping cart items).
Many applications use a persistent storage mechanism (such as a database ) to store data. MVC does not specifically mention the data access layer because it is understood to be underneath or encapsulated by the Model.
View
Renders the model into a form suitable for interaction, typically a user interface element. Multiple views can exist for a single model for different purposes.
Controller
Processes and responds to events, typically user actions, and may invoke changes on the model.

예제코드

Model.java

import java.util.*;

public class Model {
    private int x;
    private int y;
    private ArrayList<Model> arrList = new ArrayList<Model>();
   
    public Model(int i, int j) {
        this.x = i;
        this.y = j;
    }
   
    public void setModel(int x) {
        for(int i=0; i < x; i++) {   
            arrList.add(new Model(i, i+1));
        }
    }
   
    public ArrayList<Model> getArrayList() {
        return this.arrList;
    }
   
    public int getX(){
        return this.x;
    }
   
    public int getY(){
        return this.y;
    }
}

View.java

import java.util.*;

public class View {
    private static Controller con = null;
   
    // x, y 축 보기
    public static void viewList() {       
        ArrayList<Model> arrayList = View.con.getModelList();
        for(int i = 0; i < arrayList.size(); i++ ) {
            System.out.println(arrayList.get(i).getX() + “, ” + arrayList.get(i).getY());
        }
    }
   
    // x축 보기
    public static void viewLine(){
        ArrayList<Model> arrayList = View.con.getModelList(20);
       
        for(int i = 0; i < arrayList.size(); i++ ) {
            System.out.println(arrayList.get(i).getX());
        }
    }
   
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       
        con = new Controller();
        viewList();
        viewLine();   
    }
}

Controller.java

import java.util.*;

public class Controller {
    private Model model= null;
   
    public Controller() {
        this.model = new Model(1,1);
    }
   
    public ArrayList<Model> getModelList() {
        model.setModel(10);
        return model.getArrayList();
    }
   
    public ArrayList<Model> getModelList(int count) {
        model.setModel(count);
        return model.getArrayList();
    }   
}

답글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다.