MVC design Pattern

Source: Internet
Author: User

Model base class:

Using Unityengine;
Using System.Collections;

<summary>
Model, data type base class, persistent
</summary>
Public abstract class Model {
Name
Public abstract string Name {get;}
Send Notifications
public void Sendevent (string eventName, object args = null)
{
Puremvc.sendevement (EventName, args);
}
}

View base class:

Using Unityengine;
Using System.Collections;

<summary>
View, depending on the permanent existence of the EOG class
</summary>
Public abstract class View:monobehaviour {
Name
Public abstract string Name {get;}
Get model
Public T getmodel<t> () where T:model
{
return null;
}
Send Notifications
public void Sendevent (string eventName, object args = null)
{
Puremvc.sendevement (EventName, args);
}
}

Controller base class:

Using Unityengine;
Using System.Collections;

<summary>
Destroy the controller after it is exhausted
</summary>
Public abstract class Controller
{
Name
Public abstract string Name {get;}
Get model
Public T getmodel<t> () where T:model
{
return puremvc.getmodel<t> ();
}
Get view
Public T getview<t> () where T:view
{
return puremvc.getview<t> ();
}
Controller Execution Logic code
public abstract void Execute (object args);
}

General controller PureMVC Class

Using Unityengine;
Using System.Collections.Generic;
Using System;

<summary>
Total Controller for 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> ();

Add the model element to the Models collection (registration is actually adding elements to the collection)
public static void Registermodel (model model)
{
Models[model. Name] = model;
}
Add the view element to the Views collection (registration is actually adding the corresponding element to the collection)
public static void Registerview (view view)
{
Views[view.name] = view;
}
Add the appropriate type to the controller type
public static void Registerctrl (string name, type type)
{
Ctrls[name] = type;
}
Gets the specified type (T) model from the Models collection
public static T getmodel<t> () where T:model
{
foreach (Model m in models. Values)
{
If (M is T)
{
return m as T;
}
}
return null;
}
Gets the specified type (T) view from the Views collection
public static T getview<t> () where T:view
{
foreach (View m in views. Values)
{
If (M is T)
{
return m as T;
}
}
return null;
}
Send event function to get the controller of the type according to the name of the controller passed in
public static void Sendevement (String evementname, Object Args=null)
{
If the collection contains a controller of the corresponding name type
if (ctrls. ContainsKey (Evementname))
{
Get the controller formation.
Type ctrltype = Ctrls[evementname];
Instantiate a controller formation using reflection (create an instance of the Ctrltype type and convert it to controller type)
Controller CTRL = (Controller) activator.createinstance (Ctrltype);
Execute command
Ctrl. Execute (args);
}
}
}

Start the game controller class:

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)
{
Get model
Get view
Get the Logic code
Debug.Log ("Execute Game Start Logic code");
}
}

Constant class:

Using Unityengine;
Using System.Collections;

public class Consts {
Public Const string e_startgame = "Startgamecontroller";
}

Test Script class:

Using Unityengine;
Using System.Collections;

public class Gametest:monobehaviour {
void Start ()
{
Registering events
Puremvc.registerctrl (Consts.e_startgame, typeof (Startgamecontroller));
Send Event
Puremvc.sendevement (Consts.e_startgame);
}

}

MVC design Pattern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.