设计模式 - MVC 模式
MVC 模式代表 Model-View-Controller(模型-视图-控制器) 模式。这种模式用于应用程序的分层开发。
- Model(模型) - 模型代表一个存取数据的对象或 JAVA POJO。它也可以带有逻辑,在数据变化时更新控制器。
- View(视图) - 视图代表模型包含的数据的可视化。
- Controller(控制器) - 控制器作用于模型和视图上。它控制数据流向模型对象,并在数据变化时更新视图。它使视图与模型分离开。
package main import "fmt" type Student struct { RollNo string Name string } func (s *Student) GetRollNo() string { return s.RollNo } func (s *Student) SetRollNo(str string) { s.RollNo = str } func (s *Student) GetName() string { return s.Name } func (s *Student) SetName(name string) { s.Name = name } type StudentView struct{} func (v *StudentView) PrintStudentDetails(student *Student) { fmt.Println("Student[name: ", student.Name, ", RollNo: ", student.RollNo, "]") } type StudentController struct { Model *Student View *StudentView } func NewStudentController(model *Student, view *StudentView) *StudentController { return &StudentController{ Model: model, View: view, } } func (c *StudentController) SetStudentName(name string) { c.Model.SetName(name) } func (c *StudentController) GetStudentName() string { return c.Model.GetName() } func (c *StudentController) SetStudentRollNo(roll string) { c.Model.SetRollNo(roll) } func (c *StudentController) GetStudentRollNo() string { return c.Model.GetRollNo() } func (c *StudentController) UpdateView() { c.View.PrintStudentDetails(c.Model) } func main() { student := &Student{RollNo: "1", Name: "Robert"} view := &StudentView{} controller := NewStudentController(student, view) controller.UpdateView() controller.SetStudentName("John") controller.SetStudentRollNo("2") controller.UpdateView() }
Jan 14, 2023 10:32:03 AM
The MVC design pattern is a software design pattern that divides the application into three major components: the model, the view, and the controller. The MVC pattern is often used in web applications, where the view is the web page and the controller is the code that handles the best CBD extraction methods user input. The model is the data that is being displayed on the web page.