puremvc中的设计模式

pureMVC是一个纯MVC结构,purvMVC中负责数据层(M),视图层(V),和控制层(C)分布是Model类,View类,和Control类

Model类负责操作控制Proxy集合(注册/移除Proxy实例),View类负责操作Mediator集合(依然是注册/移除Mediator实例),Control类维护Command类

Model类,View类与Control类不和框架外的类进行通信,三者作为成员变量聚合在Facade类下(设计模式:聚合),

由Facade类统一于外界进行通信(设计模式:门面模式),Facade类使外部程序在不需要了解pureMVC的内部结构的情况下,而与pureMVC进行交互

//Facade
public void registerCommand( String noteName, Class commandClassRef )
{
	//Facade类的registerCommand,函数内部调用了Control类的registerCommand
	this.controller.registerCommand( noteName, commandClassRef );
}


Facade类在pureMVC中只有一个实例(设计模式:单例模式),相应的,由于Model类,View类和Control类是Facade的一个成员变量,
Model,View和Control在pureMVC框架中也只存在一个实例
public synchronized static Facade getInstance( )
{
	//懒汉式单例
	if (instance == null) {
		try {
			instance = new Facade();
		} catch (Exception e) {
		}
	}
	return instance;
}

前面说过,View类负责注册/移除mediator实例,View在注册Mediator时,首先调用Mediator的listNotificationInterests方法,获得该Mediator感兴趣的Notification(命令通知),
然后实例化一个Observer对象,进行注册,那么这时框架在接受到一个命令通知的时候,就会通知了相应的Observer类
public void registerMediator( final IMediator mediator )
	{
		// Register the Mediator for retrieval by name
		this.mediatorMap.put(mediator.getMediatorName(), mediator);

		//获得Mediator感兴趣的事件
		String[] noteInterests = mediator.listNotificationInterests();
		if (noteInterests.length == 0) {
			return;
		}

		// 创建一个匿名类,实现onNotification方法,告诉Observer(观察
            //  者),对于Mediator感兴趣的事件,应该统一调用mediator.handleNotification
		IFunction function = new IFunction()
		{
			public void onNotification( INotification notification )
			{
				mediator.handleNotification(notification);
			}
		};

		// Create Observer
		Observer observer = new Observer(function, mediator);

		// Register Mediator as Observer for its list of Notification interests
		for (int i = 0; i < noteInterests.length; i++) {
			registerObserver(noteInterests[i], observer);
		}
	}

在这里,应用了观察者模式,View是一个主题,Oberserver类是一个观察者,Notication是一个通知
registerObserver这个方法是将observer实例放在在一张hashtable中,这个hashtable用来保存通知和Observer的映射关系,它的key是通知名,value是一个Observer的集合类Observers类

在Control中,有一个类似的注册方法:registerCommand,它用来维护Notification的映射关系,使用的仍然是观察者模式
//notificationName:通知名
	//commandClassRef:Command类
	public void registerCommand( String notificationName, Class commandClassRef )
	{
		if (null != this.commandMap.put( notificationName, commandClassRef )) return;
		this.view.registerObserver( notificationName, new Observer(
		new IFunction()
		{//创建匿名类,实现onNotification方法
			public void onNotification( INotification notification )
			{
				//executeCommand方法从根据notificationName实例化一个Command,执行这个Command类的execute,
				//所以说Command它是无状态,它只有被需要的时候才会被创建
				executeCommand( notification );
			}
		}, this ) );
	}


在Proxy类,Mediator类中在发送通知时sendNotification时,最后都会调用View类notifyObservers
	public void notifyObservers( INotification note )
	{
		Observers observers = (Observers) this.observerMap
				.get(note.getName());
		if (observers != null) {
		//notifyObservers方法循环调用每一个Observer的onNotification,对应
              //Mediator注册的方法,最终调用Mediator.handleNotification进行处理,
            //对于Command,则最终调用调用Command.execute
			observers.notifyObservers(note);
		}
	}

你可能感兴趣的:(设计模式,数据结构,框架,mvc)