MVC模式
MVC 模式代表 Model-View-Controller(模型-视图-控制器) 模式。这种模式用于应用程序的分层开发。
- Model(模型) - 模型代表一个存取数据的对象或 JAVA POJO。它也可以带有逻辑,在数据变化时更新控制器。
- View(视图) - 视图代表模型包含的数据的可视化。
- Controller(控制器) - 控制器作用于模型和视图上。它控制数据流向模型对象,并在数据变化时更新视图。它使视图与模型分离开。
- 实现:
- 我们将创建一个作为模型的 Student 对象。StudentView 是一个把学生详细信息输出到控制台的视图类,StudentController 是负责存储数据到 Student 对象中的控制器类,并相应地更新视图 StudentView。
-
MVCPatternDemo,我们的演示类使用 StudentController 来演示 MVC 模式的用法。
思维导图如上
步骤一:创建模型
#region 模块信息
// **********************************************************************
// Copyright (C) 2018 Blazors
// Please contact me if you have any questions
// File Name: Main
// Author: romantic123fly
// WeChat||QQ: at853394528 || 853394528
// **********************************************************************
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MVCMode
{
//模型M
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;
}
}
}
步骤二:创建视图
#region 模块信息
// **********************************************************************
// Copyright (C) 2018 Blazors
// Please contact me if you have any questions
// File Name: Main
// Author: romantic123fly
// WeChat||QQ: at853394528 || 853394528
// **********************************************************************
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace MVCMode
{
class StudentView
{
public void printStudentDetails(string studentName, string studentRollNo)
{
Debug.Log ("Student: ");
Debug.Log("Name: " + studentName);
Debug.Log("Roll No: " + studentRollNo);
}
}
}
步骤三:创建控制器
#region 模块信息
// **********************************************************************
// Copyright (C) 2018 Blazors
// Please contact me if you have any questions
// File Name: Main
// Author: romantic123fly
// WeChat||QQ: at853394528 || 853394528
// **********************************************************************
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MVCMode
{
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());
}
}
}
步骤四:运行程序验证mvc模式
// WeChat||QQ: at853394528 || 853394528
// **********************************************************************
#endregion
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*MVC 模式代表 Model-View-Controller(模型-视图-控制器) 模式。这种模式用于应用程序的分层开发。
Model(模型) - 模型代表一个存取数据的对象或 JAVA POJO。它也可以带有逻辑,在数据变化时更新控制器。
View(视图) - 视图代表模型包含的数据的可视化。
Controller(控制器) - 控制器作用于模型和视图上。它控制数据流向模型对象,并在数据变化时更新视图。它使视图与模型分离开。
*/
namespace MVCMode
{
public class Main : MonoBehaviour
{
void Start()
{
//从数据可获取学生记录
Student model = retriveStudentFromDatabase();
//创建一个视图:把学生详细信息输出到控制台
StudentView view = new StudentView();
StudentController controller = new StudentController(model, view);
controller.UpdateView();
//更新模型数据
controller.SetStudentName("John");
controller.UpdateView();
}
private static Student retriveStudentFromDatabase()
{
Student student = new Student();
student.SetName("Robert");
student.SetRollNo("10");
return student;
}
}
}
步骤五:输出结果
资源源码地址:https://download.csdn.net/download/qq_37310110/10284136