1.1.8 Model-View-Controller Pattern
1.1.8 MVC 패턴 (Model-View-Controller)
Last updated
1.1.8 MVC 패턴 (Model-View-Controller)
Last updated
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Student {
private String rollNo;
private String name;
}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);
}
}@RequiredArgsConstructor
public class StudentController {
private Student model;
private StudentView 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());
}
}public class MVCPatternDemo {
public static void main(String[] args) {
// 모델 객체 생성
Student model = retrieveStudentFromDatabase();
// 뷰 객체 생성
StudentView view = new StudentView();
// 컨트롤러 객체 생성
StudentController controller = new StudentController(model, view);
// 뷰 업데이트
controller.updateView();
// 모델 데이터 변경
controller.setStudentName("John Doe");
// 변경된 데이터로 뷰 업데이트
controller.updateView();
}
private static Student retrieveStudentFromDatabase() {
Student student = new Student();
student.setName("Robert");
student.setRollNo("10");
return student;
}
}