puremvc 笔记

原文链接

核心层(单例):

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

发送通知

原文地址:https://www.cnblogs.com/lpcoder/p/7142514.html