目录
目录
1:写pureMvc第一步需要做什么?
2:业务执行顺序是什么?
3:主要代码
3.1:数据部分(Proxy)
3.1.1:原始数据面板中text显示的value
3.1.2:proxy类
3.2:视图部分(mediator)
3.3:控制层(Command)
3.3.1:增加数据逻辑
3.3.2:减少数据逻辑
3.4:Facade(经纪人--就是登记前面三个的)
4:项目参考及源文件
1:写pureMvc第一步需要做什么?
根据官方图和调用关系图来解释一下
- 左侧mediator包含的是游戏面板中的所有ui组件,和一些ui事件的绑定。除此之外还应该有来自proxy发送的信息的集合。
- 右侧的proxy包含的是数据层,基本的要有对数据的操作方法。
- 上侧的Command是操作的动作,需要调用proxy中具体的方法。
- 下侧的facade是将一个mvc模块集合封装在一起,包含上面3种的登记。
2:业务执行顺序是什么?
想象一下如果你点击场景中的按钮 当你点击Add就会触发mediator中你为按钮绑定的事件,事件中包含向Command层发送一个消息(SendNotification让其调用自己的Execute方法),command收到后直接调用proxy中的方法( proxy.Function();这里是直接调用并不是发送信息让对方自己调用),调用proxy中的方法包含对数据的处理以及将数据发送(发送信息SendNotification)至mediator中进行面板视图的更新。
3:主要代码
3.1:数据部分(Proxy)
3.1.1:原始数据面板中text显示的value
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class valuedata : MonoBehaviour
{
public int value;
}
3.1.2:proxy类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PureMVC.Patterns.Proxy;
public class proxy : Proxy
{
public new const string NAME = "myproxy";
valuedata mydata = null;
public proxy():base(NAME)
{
mydata = new valuedata();
}
//对数据操作
public void addvalue()
{
mydata.value++;
SendNotification("msg_add", mydata);
}
public void subvalue()
{
mydata.value--;
SendNotification("msg_sub", mydata);
}
}
3.2:视图部分(mediator)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PureMVC.Patterns.Mediator;
using UnityEngine.UI;
using PureMVC.Interfaces;
public class mediator : Mediator
{
public new const string NAME = "Mymediator";
public Text text;
public Button addbtn;
public Button subbtn;
public mediator(Transform root) : base(NAME)
{
text = root.Find("Text").GetComponent();
addbtn = root.Find("Add").GetComponent
3.3:控制层(Command)
3.3.1:增加数据逻辑
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PureMVC.Patterns.Command;
using PureMVC.Interfaces;
public class Command : SimpleCommand
{
public override void Execute(INotification notification)
{
proxy proxy = (proxy)Facade.RetrieveProxy(proxy.NAME);
proxy.addvalue();
}
}
3.3.2:减少数据逻辑
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PureMVC.Patterns.Command;
using PureMVC.Interfaces;
public class Commandsub : SimpleCommand
{
public override void Execute(INotification notification)
{
proxy proxy = (proxy)Facade.RetrieveProxy(proxy.NAME);
proxy.subvalue();
}
}
3.4:Facade(经纪人--就是登记前面三个的)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PureMVC.Patterns.Facade;
public class FacadeA : Facade
{
public FacadeA(Transform root)
{
RegisterCommand("cmd_add", () => { return new Command(); });
RegisterCommand("cmd_sub", () => { return new Commandsub(); });
RegisterMediator(new mediator(root));
RegisterProxy(new proxy());
}
}
4:项目参考及源文件
- pureMvc:官网里面有中文文档:The PureMVC framework
- pureMvc4.2版本git链接:PureMVC/puremvc-csharp-standard-framework: PureMVC Standard Framework for C# (github.com)
- 本文项目链接:https://github.com/JAM893736346/PureMvcText.git
3.1.1:原始数据面板中text显示的value
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class valuedata : MonoBehaviour
{
public int value;
}
3.1.2:proxy类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PureMVC.Patterns.Proxy;
public class proxy : Proxy
{
public new const string NAME = "myproxy";
valuedata mydata = null;
public proxy():base(NAME)
{
mydata = new valuedata();
}
//对数据操作
public void addvalue()
{
mydata.value++;
SendNotification("msg_add", mydata);
}
public void subvalue()
{
mydata.value--;
SendNotification("msg_sub", mydata);
}
}
3.2:视图部分(mediator)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PureMVC.Patterns.Mediator;
using UnityEngine.UI;
using PureMVC.Interfaces;
public class mediator : Mediator
{
public new const string NAME = "Mymediator";
public Text text;
public Button addbtn;
public Button subbtn;
public mediator(Transform root) : base(NAME)
{
text = root.Find("Text").GetComponent();
addbtn = root.Find("Add").GetComponent
3.3:控制层(Command)
3.3.1:增加数据逻辑
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PureMVC.Patterns.Command;
using PureMVC.Interfaces;
public class Command : SimpleCommand
{
public override void Execute(INotification notification)
{
proxy proxy = (proxy)Facade.RetrieveProxy(proxy.NAME);
proxy.addvalue();
}
}
3.3.2:减少数据逻辑
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PureMVC.Patterns.Command;
using PureMVC.Interfaces;
public class Commandsub : SimpleCommand
{
public override void Execute(INotification notification)
{
proxy proxy = (proxy)Facade.RetrieveProxy(proxy.NAME);
proxy.subvalue();
}
}
3.4:Facade(经纪人--就是登记前面三个的)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PureMVC.Patterns.Facade;
public class FacadeA : Facade
{
public FacadeA(Transform root)
{
RegisterCommand("cmd_add", () => { return new Command(); });
RegisterCommand("cmd_sub", () => { return new Commandsub(); });
RegisterMediator(new mediator(root));
RegisterProxy(new proxy());
}
}
4:项目参考及源文件
- pureMvc:官网里面有中文文档:The PureMVC framework
- pureMvc4.2版本git链接:PureMVC/puremvc-csharp-standard-framework: PureMVC Standard Framework for C# (github.com)
- 本文项目链接:https://github.com/JAM893736346/PureMvcText.git
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PureMVC.Patterns.Proxy;
public class proxy : Proxy
{
public new const string NAME = "myproxy";
valuedata mydata = null;
public proxy():base(NAME)
{
mydata = new valuedata();
}
//对数据操作
public void addvalue()
{
mydata.value++;
SendNotification("msg_add", mydata);
}
public void subvalue()
{
mydata.value--;
SendNotification("msg_sub", mydata);
}
}
3.2:视图部分(mediator)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PureMVC.Patterns.Mediator;
using UnityEngine.UI;
using PureMVC.Interfaces;
public class mediator : Mediator
{
public new const string NAME = "Mymediator";
public Text text;
public Button addbtn;
public Button subbtn;
public mediator(Transform root) : base(NAME)
{
text = root.Find("Text").GetComponent();
addbtn = root.Find("Add").GetComponent
3.3:控制层(Command)
3.3.1:增加数据逻辑
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PureMVC.Patterns.Command;
using PureMVC.Interfaces;
public class Command : SimpleCommand
{
public override void Execute(INotification notification)
{
proxy proxy = (proxy)Facade.RetrieveProxy(proxy.NAME);
proxy.addvalue();
}
}
3.3.2:减少数据逻辑
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PureMVC.Patterns.Command;
using PureMVC.Interfaces;
public class Commandsub : SimpleCommand
{
public override void Execute(INotification notification)
{
proxy proxy = (proxy)Facade.RetrieveProxy(proxy.NAME);
proxy.subvalue();
}
}
3.4:Facade(经纪人--就是登记前面三个的)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PureMVC.Patterns.Facade;
public class FacadeA : Facade
{
public FacadeA(Transform root)
{
RegisterCommand("cmd_add", () => { return new Command(); });
RegisterCommand("cmd_sub", () => { return new Commandsub(); });
RegisterMediator(new mediator(root));
RegisterProxy(new proxy());
}
}
4:项目参考及源文件
- pureMvc:官网里面有中文文档:The PureMVC framework
- pureMvc4.2版本git链接:PureMVC/puremvc-csharp-standard-framework: PureMVC Standard Framework for C# (github.com)
- 本文项目链接:https://github.com/JAM893736346/PureMvcText.git
3.3.1:增加数据逻辑
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PureMVC.Patterns.Command;
using PureMVC.Interfaces;
public class Command : SimpleCommand
{
public override void Execute(INotification notification)
{
proxy proxy = (proxy)Facade.RetrieveProxy(proxy.NAME);
proxy.addvalue();
}
}
3.3.2:减少数据逻辑
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PureMVC.Patterns.Command;
using PureMVC.Interfaces;
public class Commandsub : SimpleCommand
{
public override void Execute(INotification notification)
{
proxy proxy = (proxy)Facade.RetrieveProxy(proxy.NAME);
proxy.subvalue();
}
}
3.4:Facade(经纪人--就是登记前面三个的)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PureMVC.Patterns.Facade;
public class FacadeA : Facade
{
public FacadeA(Transform root)
{
RegisterCommand("cmd_add", () => { return new Command(); });
RegisterCommand("cmd_sub", () => { return new Commandsub(); });
RegisterMediator(new mediator(root));
RegisterProxy(new proxy());
}
}
4:项目参考及源文件
- pureMvc:官网里面有中文文档:The PureMVC framework
- pureMvc4.2版本git链接:PureMVC/puremvc-csharp-standard-framework: PureMVC Standard Framework for C# (github.com)
- 本文项目链接:https://github.com/JAM893736346/PureMvcText.git
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PureMVC.Patterns.Command;
using PureMVC.Interfaces;
public class Commandsub : SimpleCommand
{
public override void Execute(INotification notification)
{
proxy proxy = (proxy)Facade.RetrieveProxy(proxy.NAME);
proxy.subvalue();
}
}
3.4:Facade(经纪人--就是登记前面三个的)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PureMVC.Patterns.Facade;
public class FacadeA : Facade
{
public FacadeA(Transform root)
{
RegisterCommand("cmd_add", () => { return new Command(); });
RegisterCommand("cmd_sub", () => { return new Commandsub(); });
RegisterMediator(new mediator(root));
RegisterProxy(new proxy());
}
}
4:项目参考及源文件
- pureMvc:官网里面有中文文档:The PureMVC framework
- pureMvc4.2版本git链接:PureMVC/puremvc-csharp-standard-framework: PureMVC Standard Framework for C# (github.com)
- 本文项目链接:https://github.com/JAM893736346/PureMvcText.git
- pureMvc:官网里面有中文文档:The PureMVC framework
- pureMvc4.2版本git链接:PureMVC/puremvc-csharp-standard-framework: PureMVC Standard Framework for C# (github.com)
- 本文项目链接:https://github.com/JAM893736346/PureMvcText.git
注:本人也处于学习状态,其内部源码并没完全理解只是将自己的理解记录下来,如有错误的地方还请大佬指出,,您指出的越多我越开心!



