MVC設計模式


Model基類:

using UnityEngine;
using System.Collections;

/// <summary>
/// Model,數據類型基類,永久存在
/// </summary>
public  abstract class Model  {
    //名字
    public abstract string Name { get; }
    //發送通知
    public void SendEvent(string eventName, object args = null)
    {
        PureMVC.SendEvement(eventName, args);
    }
}

View基類:

using UnityEngine;
using System.Collections;

/// <summary>
/// View,視圖基類 永久存在
/// </summary>
public abstract class View : MonoBehaviour {
    //名字
    public abstract string Name { get; }
    //獲取模型
    public T GetModel<T>() where T:Model
    {
        return null;
    }
    //發送通知
    public void SendEvent(string eventName, object args = null)
    {
        PureMVC.SendEvement(eventName, args);
    }
}

Controller基類:

using UnityEngine;
using System.Collections;

/// <summary>
/// 控制器  使用完之后銷毀
/// </summary>
public abstract class Controller
{
    //名字
    public abstract string Name { get; }
    //獲取模型
    public T GetModel<T>() where T : Model
    {
        return PureMVC.GetModel<T>();
    }
    //獲取視圖
    public T GetView<T>() where T : View
    {
        return PureMVC.GetView<T>();
    }
    //控制器執行邏輯代碼
    public abstract void Execute(object args);         
}

總控制器PureMVC類

using UnityEngine;
using System.Collections.Generic;
using System;

/// <summary>
/// mvc 的總控制器
/// </summary>
public static class PureMVC  {
    private static Dictionary<string, Model> models = new Dictionary<string, Model>();
    private static Dictionary<string, View> views = new Dictionary<string, View>();
    private static Dictionary<string, Type> ctrls = new Dictionary<string, Type>();

    //向models集合里面加model這個元素(注冊其實就是向集合里面添加元素)
    public static void RegisterModel(Model model)
    {
        models[model.Name] = model;
    }
    //向views集合里面加入view這個元素(注冊其實就是向集合里面添加相應元素)
    public static void Registerview(View view)
    {
        views[view.name] = view;
    }
    //向控制器類型里面加入相應的類型
    public static void RegisterCtrl(string name, Type type)
    {
        ctrls[name] = type;
    }
    //從models集合里獲取指定類型(T)模型
    public static T GetModel<T>() where T : Model
    {
        foreach (Model m in models.Values)
        {
            if (m is T)
            {
                return m as T;
            }         
        }
        return null;
    }
    //從views集合獲取指定類型(T)視圖
    public static T GetView<T>() where T : View
    {
        foreach (View m in views.Values)
        {
            if (m is T)
            {
                return m as T;
            }
        }
        return null;
    }
    //發送事件函數,根據傳進來的控制器的名字獲取相應類型的控制器
    public static void SendEvement(string evementName, object args=null)
    {
        //如果集合包含對應名字類型的控制器
        if (ctrls.ContainsKey(evementName))
        {
            //拿到控制器隊形
            Type ctrlType = ctrls[evementName];
            //使用反射實例化控制器隊形(創建一個ctrlType類型的實例,並且將其強轉為Controller類型)
            Controller ctrl = (Controller)Activator.CreateInstance(ctrlType);
            //執行命令
            ctrl.Execute(args);
        }
    }
}

開始游戲控制器類:

 

using UnityEngine;
using System.Collections;
using System;

public class StartGameController : Controller
{
    public override string Name
    {
        get
        {
            //throw new NotImplementedException();
            return (Consts.E_StartGame);
        }
    }
    public override void Execute(object args)
    {
        //獲取模型
        //獲取視圖
        //獲取邏輯代碼
        Debug.Log("執行游戲開始邏輯代碼");
    }
}

 

 

 

 

常量類:

using UnityEngine;
using System.Collections;

public class Consts  {
    public const string E_StartGame = "StartGameController";    
}

測試腳本類:

using UnityEngine;
using System.Collections;

public class GameTest : MonoBehaviour {    
    void Start ()
    {
        //注冊事件
        PureMVC.RegisterCtrl(Consts.E_StartGame, typeof(StartGameController));
        //發送事件
        PureMVC.SendEvement(Consts.E_StartGame);
    }
    
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM