MVC設計模式

來源:互聯網
上載者:User

標籤:

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);
    }
    
}

 

MVC設計模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.