設計模式 深入淺出之——MVC模式

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章