Pure MVC是在基于模型、视图和控制器MVC模式建立的一个轻量级的应用框架,这个框架是开源免费的,目前支持18中开发语言。
官网:http://puremvc.org
github地址:https://github.com/PureMVC
单例模式(Singleton):保证一个类仅有一个实例,并提供一个访问它的全局访问点。
代理模式(Proxy):为其他对象提供一种代理以控制对这个对象的访问。
中介者模式(Mediator):用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显示地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
迪米特法则(Law of Demeter,简称LoD)也叫最小知识原则(Least Knowledge Principle 简写LKP)如果俩个类不必直接通信,那么这俩类就不应当发生直接的相互作用。如果其中一个类需要调用另一个类的某一个方法的话,可以通过第三者转发这个调用。
命令模式(Command):讲一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。
外观模式(Facade):为了系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
观察者模式(Publish/Subscribe):定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态发生变化时,会通知所有观察者对象,使它们能够自动更新自己。
demo地址:https://gitee.com/ZhaoYongshuang/puremvc_framework_demo
简单案例.png2.3 代码实现2.3.1 ApplicationFacade.csusing PureMVC.Patterns;using PureMVC.Interfaces;public class ApplicationFacade :Facade{ public new static IFacade Instance { get { if (m_instance == null) { lock (m_staticSyncRoot) { if (m_instance == null) { m_instance = new ApplicationFacade(); } } } return m_instance; } } protected override void InitializeModel() { base.InitializeModel(); RegisterProxy(new MyProxy()); } protected override void InitializeView() { base.InitializeView(); RegisterMediator(new MyMediator()); } protected override void InitializeController() { base.InitializeController(); RegisterCommand("Cmd_StartUpApp", typeof(CmdStartUpApp)); RegisterCommand("Cmd_AddCount", typeof(CmdAddCount)); }}
2.3.2 MyUI.csusing System;using UnityEngine;using UnityEngine.UI;public class MyUI : MonoBehaviour{ private Text txtLevel; private Button btnAddCount; public Action actionBtnCount; void Start() { txtLevel = transform.Find("TxtCount").gameObject.GetComponent<Text>(); btnAddCount = transform.Find("BtnCount").gameObject.GetComponent<Button>(); btnAddCount.onClick.AddListener(OnClickAddCount); } private void OnClickAddCount() { actionBtnCount?.Invoke(); } public void SetTxtCount(MyData myData) { txtLevel.text = myData.Count.ToString(); }}
2.3.3 StartGame.csusing UnityEngine;public class StartGame : MonoBehaviour { public MyUI myUI; void Start() { ApplicationFacade appFacade = ApplicationFacade.Instance as ApplicationFacade; if (appFacade != null && myUI != null) { appFacade.SendNotifi