MVC Pattern 이란? - 개념 및 자바 예제


서론

제가 이해한대로 적었기 때문에 이미 개념을 알고계신 분들에게는 다소 쉽게 느껴질 수 있습니다. 처음 개념을 접하시는 분들이 개념을 쉽게 이해하도록 돕는 것이 글의 목적입니다!

tutorials point 사이트의 디자인 패턴 문서를 참고하고 거기에 제 설명을 덧붙여서 적은 글입니다. (영어가 편하신 분들은 이 글도 읽어보세요!)

개념

#Model

Model represents an object or JAVA POJO carrying data. It can also have logic to update controller if its data changes.

모델은 데이터를 가지고 있는 객체 또는 JAVA POJO입니다. 데이터가 바뀌었을 때 컨트롤러의 데이터를 업데이트 하는 로직도 포함합니다.

예를들어 '학생'이라는 데이터 set에 대해 모델을 만든다면 Student 클래스의 멤버 변수와 멤버 함수는 이정도 되겠네요

  • 멤버 변수 : rollNo(학번), name(학생 이름)
  • 멤버 함수 : getRollNo, setRollNo, getName, setName

보통 학번, 학생 이름과 같이 모델에서 다룰만한 데이터 들은 데이터베이스에 저장하죠! 그럴 경우 데이터베이스와 상호작용 하며 CRUD 작업을 하는 것도 모델의 멤버함수(getter, setter 등)들이 처리 합니다.

#View

View represents the visualization of the data that model contains.

모델에 포함된 데이터의 시각화를 담당합니다.

여기서 '모델에 포함된 데이터'란 학번과 학생 이름입니다! 즉 뷰는 학번과 학생 이름이라는 데이터가 어플리케이션상에서 UI에 어떻게 나타날지에 관련된 코드 덩어리 입니다. 웹 어플리케이션이라면 HTML, Android 앱이라면 xml이 이 부분의 표현을 담당하는 언어죠. 즉 프로젝트의 뷰 폴더에는 웹 앱이라면 주로 html, 안드로이드 앱이라면 주로 xml 파일들이 들어 있을 거에요.

#Controller

Controller acts on both model and view. It controls the data flow into model object and updates the view whenever data changes. It keeps view and model separate.

컨트롤러는 모델과 뷰에 전부 영향을 미칩니다. 모델 객체로의 데이터 흐름을 제어하고 데이터가 update 되었을 때 뷰를 갱신합니다. 컨트롤러는 뷰와 모델의 역할을 분리합니다.

빠밤! 설명의 하이라이트! 컨트롤러입니다. 저는 이렇게 생각하니까 명료했어요. 모델과 뷰를 사용하는 건 컨트롤러, 컨트롤러를 사용하는 건 어플리케이션![1]

컨트롤러는 모델과 뷰 객체를 활용해서 멤버 함수를 만듭니다. 컨트롤러의 역할은 어플리케이션에서 사용할 interface 함수[2]들을 제공하는 것이죠.

어플리케이션의 역할은 컨트롤러 객체의 함수들을 잘 사용해서 적절한 데이터를 적절한 UI에 보여주는 프로그램이 되는 것이겠죠. 그리고 컨트롤러를 사용하는 게 어플리케이션이라는 말의 의미는, 어플리케이션에서 데이터와 관련된 작업을 하고 싶다면 모델, 뷰 객체는 건드릴 필요 없이 컨트롤러 객체만 가지고 놀면 된다는 의미입니다.

이 관점에서 계속 본다면, 컨트롤러를 기준으로 추상화가 한단계 생기네요. 컨트롤러 윗 레벨에서는 데이터베이스는 물론이고 모델과 뷰에 대해서도 알 필요가 없고 '컨트롤러 객체의 조작법'만 알면 되는거죠.

이 부분 설명이 이해가 안가셨다면, 바로 아래 그림 보고, 예제 코드 보고 다시 설명 읽어보시면 감이오실거라고 믿어요ㅎㅎ

#전체 구조 그림

그림에서 중요한 점은 뷰, 모델, 컨트롤러, 어플리케이션이 어떻게 상호작용을 하고있나 하는 점입니다!

mvc

예제

#1. 모델

Student.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Student {
private String rollNo;
private String name;

public String getRollNo() {
return rollNo;
}

public void setRollNo(String rollNo) {
this.rollNo = rollNo;
}

public String getName() {
return name;
}

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

#2. 뷰

StudentView.java

1
2
3
4
5
6
7
public class StudentView {
public void printStudentDetails(String studentName, String studentRollNo){
System.out.println("Student: ");
System.out.println("Name: " + studentName);
System.out.println("Roll No: " + studentRollNo);
}
}

#3. 컨트롤러

StudentController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class StudentController {
private Student model;
private StudentView view;

public StudentController(Student model, StudentView view){
this.model = model;
this.view = view;
}

public void setStudentName(String name){
model.setName(name);
}

public String getStudentName(){
return model.getName();
}

public void setStudentRollNo(String rollNo){
model.setRollNo(rollNo);
}

public String getStudentRollNo(){
return model.getRollNo();
}

public void updateView(){
view.printStudentDetails(model.getName(), model.getRollNo());
}
}

#4. 어플리케이션 : 컨트롤러 사용

MVCPatternDemo.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class MVCPatternDemo {
public static void main(String[] args) {

//fetch student record based on his roll no from the database
Student model = retriveStudentFromDatabase();

//Create a view : to write student details on console
StudentView view = new StudentView();

StudentController controller = new StudentController(model, view);

controller.updateView();

//update model data
controller.setStudentName("John");

controller.updateView();
}

private static Student retriveStudentFromDatabase(){
Student student = new Student();
student.setName("Robert");
student.setRollNo("10");
return student;
}
}

#5. output

1
2
3
4
5
6
Student: 
Name: Robert
Roll No: 10
Student:
Name: John
Roll No: 10


  1. 여기서 어플리케이션이라는 단어는 software의 entry point로 제어 흐름을 가지는 코드를 뜻하는 의미로 사용하였습니다!

  2. 사용자가 어플리케이션을 사용할 때의 조작 화면, 어플리케이션과 사용자의 접점을 User Interface 라고 하는 것 처럼, 어플리케이션이 컨트롤러를 조작할 때 사용하는 컨트롤러의 함수들도 일종의 Interface 라고 할 수 있습니다.