Cocos2d-js中的简易MVC框架(三)中介者Mediator

时间:2022-07-22
本文章向大家介绍Cocos2d-js中的简易MVC框架(三)中介者Mediator,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Mediator作为这套MVC框架的核心部分承载了大部分的功能。Mediator主要分三类:DirectorMediator,SceneMediator,LayerMediator。这三类Mediator都是继承自IMediator,IMediator的实现如下:

/***************************************************************************The game.IMediator
@author ituuz
@date 2014-11-18
IView is the mediator of MVC, create your model to extend this class.
In this mediator can get model,current mediator and rootMediator.
***************************************************************************/
game.IMediator = cc.Class.extend({
    ctor:function () {  },
    show:function ()  {
        throw new Error("SubClass not overwrite show function.");
    },
    getCurrMediator:function () {
        var med = game.Facade._directorMediator.currSceneMediator.currLayerMediator;    
        if (med == null) {
            med = game.Facade._directorMediator.currSceneMediator.rootLayerMediator;
        }
        return med;
    },
    getRootMediator:function () {
        return game.Facade._directorMediator.currSceneMediator.rootLayerMediator;
    },
    getModel:function (cls) {
        return game.Facade._modelMap.get(cls);
    },
    //Use this function to send notification.
    send:function (key, obj)  {
        game.Notification.send(key, obj);
    }
});

show函数需要它的子类去实现,show的主要作用就是该Mediator被创建时显示的内容。send函数是用来发送消息的。除此之外还有三个get函数,分别是获取当前Mediator、获取RootMediator和获取Model对象。

下面单独介绍继承自Mediator的这三类Mediator的作用。

首先是DirecotorMediator,它主要是来控制场景的创建和销毁。代码实现如下:

/***************************************************************************
The DirectorMediator
@author ituuz
@date 2014-11-18
Control the scene show or hide.
***************************************************************************/
game.DirectorMediator = game.IMediator.extend({
    sceneMediatorStack:null, 
    currSceneMediator:null,
    ctor:function (view) {
        this.currView = view;
        this.sceneMediatorStack = new game.Stack();
    },
    show:function (parent) {  },
    //Open the new scene and destroy the previous.
    showScene:function (mediator) {
        if (this.currSceneMediator 
            && this.currSceneMediator.rootLayerMediator) { 
            this.currSceneMediator.rootLayerMediator._pDispose();
        } 
        if (this.currSceneMediator) { 
            var layMedList = this.currSceneMediator.layerMediatorList;                 while (layMedList.size() > 0) {
                var med = layMedList.pop();
                med._pDispose();
            }
        }    
        this.currSceneMediator = mediator;
        this.sceneMediatorStack = new game.Stack();
        this.sceneMediatorStack.push(mediator);
        cc.director.runScene(mediator.currScene);
    },
    //Open the new scene and push the previous into the stack
    pushScene:function (mediator) {
        this.currSceneMediator = mediator;       
        this.sceneMediatorStack.push(mediator);
        cc.director.pushScene(mediator.currScene);
    },
    //Pop the current scene to destroy then go to the previous scene.
    popScene:function () {
        var pop = this.sceneMediatorStack.pop();
        var curr = this.sceneMediatorStack.top();
        this.currSceneMediator = curr;
        cc.director.popScene();
    }
});

DirectorMediator除了继承了Mediator的几个函数外,还有几个控制Scene打开和关闭的函数,分别是showScene,pushScene,popScene,都是控制场景打开和关闭的。

然后介绍的是SceneMediator,它主要是控制LayerMediator的生命周期。控制着LayerMediator的创建和销毁。实现如下:

/***************************************************************************The SceneMediator
@author ituuz
@date 2014-11-18
Control the layer show or hide.
***************************************************************************/
game.SceneMediator = game.IMediator.extend({
    currScene:null,
    currLayerMediator:null,
    rootLayerMediator:null,
    layerMediatorList:null,
    ctor:function (view) {
        this.currScene = view;
        this.layerMediatorList = new game.Stack();
    },  
    show:function (obj) { 
        if (this.currLayerMediator == undefined 
          || this.currLayerMediator == null ) { 
            this.rootLayerMediator.init();
            this.rootLayerMediator.show(this.currScene, obj);
        } else {
            this.currLayerMediator.init();
            this.currLayerMediator.isRoot = false;
            this.currLayerMediator.show(this.currScene, obj);
        }
    },
    //Setting the root layer  
    rootLayer:function (layerMed) {
        this.rootLayerMediator = layerMed;
    },
    showRoot:function () {
        this.show();
    },
    //Open the new layer and destroy previous layer.
    showLayer:function (layerMed, obj) {
        this.currLayerMediator = layerMed;
        this.layerMediatorList = new game.Stack();
        this.layerMediatorList.push(layerMed);
        this.show(obj);
    },
    //Open the new layer and push the previous layer into stack.
    pushLayer:function (layerMed, obj) {
        this.currLayerMediator = layerMed;
        this.layerMediatorList.push(layerMed);
        this.show(obj);
    },
    //Pop the current layer to destroy then go to the previous layer.
    popLayer:function (obj) {
        var layer = this.layerMediatorList.pop();
        layer && layer._pDispose();
        this.currLayerMediator = this.layerMediatorList.top();
        if (this.currLayerMediator) {
            this.currLayerMediator.freshen(obj);
        } else { 
            this.rootLayerMediator.freshen(obj);
        }
    }
});

它同DirectorMediator一样,控制着LayerMediator,并且拥有几个控制它的函数,包括showLayer、pushLayer、popLayer,除此之外他还有个rootLayer和showRoot函数,这两个函数是用来设置和显示场景上的初始Layer的,cocos2dx中场景内必须有Layer才能显示内容,所以这两个函数设置并显示了root layer。同时SceneMediator还重写了show函数,在重写的show函数中初始化了LayerMediator并且调用了LayerMediator的show函数用来显示Layer。

最后我们介绍一下LayerMediator,这个Mediator我们使用的频率最高,同时包含的功能也比较多。先来看代码:

/**************************************************************************
The LayerMediator
@author ituuz
@date 2014-11-18
The layer's logic.
*************************************************************************/
game.LayerMediator = game.IMediator.extend({
    currView:null,
    backBtn:null,
    mask:null,
    isRoot:true,
    ctor:function (view) {
        this.currView = view;
    },
    show:function (parent, obj) {
        if (this.isRoot) return;
        var size = cc.winSize;
        this.mask = new cc.LayerColor(cc.color(0,0,0,200), size.width, size.height);
        parent.addChild(this.mask);
        cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
            onTouchBegan: this.onTouchBegan 
        }, this.mask);
    },
    onTouchBegan:function(touch, event) {
        //Could to do sth.
        return true;
    },
    init:function () {
        throw new Error("SubClass must be overwrite init function and regist event in this function.");
    },
    freshen:function (obj) {
        cc.log("freshen");
    },
    //Use this function to send notification.
    send:function (key, obj)  {
        game.Notification.send(key, obj);
    },
    subscrib:function (type, callback, target) {
        game.Notification.subscrib(type, callback, target);
    },
    unsubscrib:function (type, callback) {
        game.Notification.unsubscrib(type, callback);
    },
    //Switch scene
    showScene:function (sceneMed) {
        game.Facade._directorMediator.showScene(sceneMed);
        game.Facade._directorMediator.currSceneMediator.showRoot();
    },
    //Push new scen into the stack
    pushScene:function (sceneMed) {
        game.Facade._directorMediator.pushScene(sceneMed);
        game.Facade._directorMediator.currSceneMediator.showRoot();
    },
    //Pop current scene out of the stack
    popScene:function () {
        game.Facade._directorMediator.popScene();
    },
    //Switch layer
    showLayer:function (layerMed, obj) {
        game.Facade._directorMediator.currSceneMediator.showLayer(layerMed, obj);  
    },
    //Push new layer into the stack
    pushLayer:function (layerMed, obj) {
        game.Facade._directorMediator.currSceneMediator.pushLayer(layerMed, obj);  
    },  
    //Pop current scene out of the stack
    popLayer:function (obj) {
        game.Facade._directorMediator.currSceneMediator.popLayer(obj);
    },
    //private 
    _pDispose:function () {
        var that = this;
        that.destroy();
        that.currView.removeFromParent(true);
        that.mask && this.mask.removeFromParent(true);
    },
    destroy:function () {
        throw new Error("SubClass must be overwrite destroy function and delete event in this function.");
    }
});

这里面通用的几个pop、show、push函数就不多说了,主要说一下其他特殊的函数。首先是构造函数,它有一个参数view,这个view就是和这个Mediator对应的Layer,这里保存了个引用,方便使用。然后是重写的show函数,在该函数中的两个参数parent是该layer的父级容器,obj是在打开该Layer时传入的参数。在show里还对打开的Layer进行了模态处理。还有init函数,该函数是在该LayerMediator初始化时调用的,可以在该函数中初始化一些数据,或者注册监听等。然后LayerMediator中还提供了几个消息相关的函数,分别是发送消息send、注册消息subscrib、删除消息注册unsubscrib。还有freshen(obj)函数是在上层Layer关闭时调用的,obj是可以传递的值,在该函数中可以做一些UI刷新的功能。最后比较重要的两个函数都是有关销毁的。destroy函数是必须在子类重写的,在LayerMediator被销毁时调用,_pDispose是私有的函数,负责销毁公有的对象,不能显示调用。

Mediator在MVC中比较重要,除了自身的动能外,还承载了其他几个功能。首先Mediator中持有View对象,可以对View进行显示更新,同时也要响应View上的各种事件。然后Mediator可以注册和发送消息。同时Mediator还可以获取Model对象,进行操作。