[Unity框架]PureMVC基础

PureMVC框架下载:

http://puremvc.github.io/

而github上的有很多都是不需要的,所以也可以直接下载简化版的(c#):

http://pan.baidu.com/s/1bcYgfc

官方文档:

http://puremvc.org/component/option,com_wrapper/Itemid,183/



核心层(单例):

Model:保存对Proxy对象的引用,Proxy负责操作数据类型。

View:保存对Mediator对象的引用,Mediator负责操作具体的视图组件。

Controller:保存所有Command的映射,Command只在需要时才被创建。



层通信:

层与层之间的通信是通过Observer/Notification 机制来实现的,你只需要使用一个非常简单的方法从 Proxy,Mediator, Command 和 Facade 发送 Notification。



Facade(单例,只发送不接受Notification):

1.提供与核心层通信的唯一接口,负责初始化核心层(Model,View 和Controller),并能访问它们的 Public 方法。Proxy、Mediator 和 Command 就可以通过创建的 Facade 类来相互访问通信。

2.Facade 保存了 Command 与 Notification 之间的映射。当 Notification(通知)被发出时,对应的 Command(命令)就会自动地由 Controller 执行。

3.Facade 类对象负责初始化 Controller(控制器),建立 Command 与 Notification 名之间的映射,并执行一个 Command 注册所有的 Model 和 View。



Mediator(可以发送和接受Notification):

1.在 Mediator 实例化时,Mediator 的 listNotifications 方法会被调用,以数组形式返回该 Mediator 对象所关心的所有 Notification。之后,当系统其它角色发出同名的 Notification(通知)时,关心这个通知的Mediator 都会调用 handleNotification 方法并将 Notification 以参数传递到方法。

2.因为 Mediator 也会经常和 Proxy 交互,所以经常在 Mediator 的构造方法中取得Proxy 实例的引用并保存在 Mediator 的属性中,这样避免频繁的获取 Proxy 实例。



Proxy(只发送不接受Notification):

1.在很多场合下 Proxy 需要发送 Notification(通知),比如:Proxy 从远程服务接收到数据时,发送 Notification 告诉系统;或当 Proxy 的数据被更新时,发送 Notification 告诉系统。



Command(可以发送和接受Notification):

1.Controller 会注册侦听每一个 Notification,当被通知到时,Controller 会实例化一个该 Notification 对应的 Command 类的对象。最后,将 Notification 作为参数传递给execute 方法。

2.Command 对象是无状态的;只有在需要的时候( Controller 收到相应的Notification)才会被创建,并且在被执行(调用 execute 方法)之后就会被删除。所以不要在那些生命周期长的对象(long-living object)里引用 Command 对象。

3.通过发送 Notification 通知 Controller 来执行 Command,而且只能由Controller 实例化并执行 Command。



在puremvc中有两种Command:

1.SimpleCommand 只有一个 execute 方法,execute 方法接受一个Inotification 实例做为参数。实际应用中,你只需要重写这个方法就行了。

2.MacroCommand 让你可以顺序执行多个 Command。每个执行都会创建一个 Command 对象并传参一个对源 Notification 的引用。MacroCommand 在构造方法调用自身的 initializeMacroCommand 方法。实际应用中,你需重写这个方法,调用 addSubCommand 添加子 Command。你可以任意组合 SimpleCommand 和 MacroCommand 成为一个新的 Command。



/

源码分析:


接口:

INotifier:发送信息

INotification:信息体


具体类:

Command:

类型:1.SimpleCommand:单条命令   2.MacroCommand:多条命令

重要方法:

1.public virtual void Execute(INotification notification)


Mediator:

重要方法:

1.ListNotificationInterests 返回view组件感兴趣的通知

2.HandleNotification 处理通知


Proxy:

重要方法:

1.public virtual void OnRegister() 当proxy被注册时调用

2.public virtual void OnRemove() 当proxy被移出时调用


Facade:

1.public virtual void RegisterCommand(string notificationName, Type commandType)

注册命令,当通知发送时,对应命令的Execute方法就会执行

2.public virtual void RegisterMediator(IMediator mediator)

注册中介,并获取中介的ListNotificationInterests,当这些通知发送时,中介的HandleNotification就会执行

3.public virtual void RegisterProxy(IProxy proxy)

注册代理,并调用代理的OnRegister方法

4.SendNotification

发送通知


Unity 中实现 MVC 架构通常需要以下几步: 1. 创建 Model 类:Model 类用于存储数据和数据操作方法,例如: ``` public class PlayerModel { private int _health; public int Health { get { return _health; } set { _health = value; if (_health <= 0) { // 触发死亡事件 OnDeath(); } } } public event Action OnDeath; public void TakeDamage(int amount) { Health -= amount; } } ``` 2. 创建 View 类:View 类用于显示数据,例如: ``` public class PlayerView : MonoBehaviour { public TextMeshProUGUI healthText; public void UpdateHealth(int health) { healthText.text = "Health: " + health; } } ``` 3. 创建 Controller 类:Controller 类用于控制 Model 和 View,例如: ``` public class PlayerController { private PlayerModel _model; private PlayerView _view; public PlayerController(PlayerModel model, PlayerView view) { _model = model; _view = view; // 监听 Model 的数据变化并更新 View _model.OnHealthChanged += _view.UpdateHealth; // 监听 View 的用户操作并控制 Model _view.OnTakeDamage += _model.TakeDamage; } } ``` 4. 在场景中创建 View 对象,并在 Controller 中传入 Model 和 View: ``` public class GameController : MonoBehaviour { public PlayerView playerViewPrefab; private void Start() { // 创建 Model 对象 var playerModel = new PlayerModel(); // 创建 View 对象并绑定到场景中的 GameObject 上 var playerView = Instantiate(playerViewPrefab); playerView.transform.SetParent(transform, false); // 创建 Controller 对象并传入 Model 和 View var playerController = new PlayerController(playerModel, playerView); } } ``` 这样就完成了一个简单的 MVC 架构的实现。在实际项目中,可能需要更复杂的 Model 和 View 类,以及更多的 Controller 类来控制不同的数据和视图。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值