PureMVC笔记

PureMVC是一个轻量级MVC框架,对Model,View和Controller进一步解耦,使框架更容易扩展。PureMVC最初是用Adobe Flex中的ActionScript 3写的,后来移植到其它语言,现在支持:
  •  ActionScript 2
  •  ActionScript 3
  •  C ++
  •  C #
  •  ColdFusion
  •  Dart
  •  Haxe
  •  Java
  •  JavaScript
  •  Objective C
  •  Perl
  •  PHP
  •  Python
  •  Ruby
PureMVC中多了3种角色,它们分别和Model,  View和Controller紧密联系:
  • Model和Proxy: Model保存Proxy的引用,Proxy负责操作Model中都数据,使Model复用性更高
  • View和Mediator: View保存Mediator引用,Mediator操作视图组件,处理UI事件
  • Controller和Command: Controller中保存了Command的映射,Command主要控制业务逻辑

Model, View和Controller之间的通讯并没有使用平台相关的UI事件机制,而使用了观察者模式。其中:
  • Mediator可以发送和处理Notification
  • Proxy只能发送Notification
  • Notification也可以触发Command

PureMVC提供了一个Facade类封装了对Model,View和Controller了的访问。Facade也负责Model,view和Contoller的初始化以及Proxy,Mediator和Command的注册。

下面是一个PyQt4的demo,实现一个简单的计算器:


Facade是个单例,在初始化方法中注册两个Command。StartUpCommand 注册Proxy和Mediator,ButtonPressedCommand调用model的计算方法

main.py:

class AppFacade(Facade):
    
    START_UP = 'facade.start_up'
    INPUT = 'facade.input'
    
    @staticmethod
    def getInstance():
        return AppFacade()
    
    '''register command to controller'''
    def initializeFacade(self):
        super(AppFacade, self).initializeFacade()
        
        self.registerCommand(AppFacade.START_UP, controller.StartUpCommand)
        self.registerCommand(AppFacade.INPUT, controller.ButtonPressedCommand)

Controller.py

class ButtonPressedCommand(SimpleCommand):
    
    def execute(self, note):
        calcService = self.facade.retrieveProxy(model.CalcProxy.NAME)
        calcService.calc(note.getBody())
        
class StartUpCommand(SimpleCommand):
    
    def execute(self, note):
        appDialog = note.getBody()
        
        '''register mediator, connect app dialog with mediator'''
        self.facade.registerMediator(view.CalcMediator(appDialog ))

        '''register proxy'''
        self.facade.registerProxy(model.CalcProxy())

View中处理UI事件和发出Notification, 触发Controller中对应的Command

view.py

class CalcMediator(Mediator):
    
    NAME = 'CalcMediator'
    
    def __init__(self, widget):
        super(CalcMediator, self).__init__(self.NAME,widget)
        
        QtCore.QObject.connect(self.viewComponent, QtCore.SIGNAL('buttonPressed'), self.buttonPressed)
    
    
    '''list of notification name, override'''
    def listNotificationInterests(self):
        return [
            model.CalcProxy.UPDATE_SCREEN
        ]
    
    '''handle notification'''
    def handleNotification(self, note):
        if (note.getName() ==  model.CalcProxy.UPDATE_SCREEN):
            self.viewComponent.screenLable.setText(note.getBody())
    
    '''slot'''
    def buttonPressed(self, text):
        screenValue = self.viewComponent.screenLable.text()
        
        if ('=' == text):
            self.sendNotification(main.AppFacade.INPUT, screenValue, None)
        else:
            if screenValue != '0':
                screenValue += text
            else:
                screenValue = text
            
            self.viewComponent.screenLable.setText(screenValue)
 

model中实现计算,完成后发出更新UI的通知:

model.py

class CalcProxy(Proxy):
    
    NAME = 'CalcProxy'
    
    UPDATE_SCREEN = 'model.update'
        
    def __init__(self, data = None):
        super(CalcProxy, self).__init__(self.NAME, data)
        
    def calc(self, string):
        
        string = str(string)
        
        '''int to flot'''
        if string.find('.') == -1 :
            string += '.'
        
        '''remove 0, in the end of result if it's integer, e.g: 15.0'''
        result = eval(string)
        if (result == int(result)):
            result = int(result)
        
        self.sendNotification(CalcProxy.UPDATE_SCREEN, str(result), None)

DEMO源码


其它资料:

PureMVC:http://www.puremvc.org

实战PureMVC:http://www.ibm.com/developerworks/cn/java/j-lo-puremvc/index.html

PyQt:http://www.riverbankcomputing.co.uk/software/pyqt/intro

Qt Calculator Example: http://doc.qt.nokia.com/4.7-snapshot/widgets-calculator.html

本课程总体分为五大部分:一: MVC基本设计理念    首先讲解游戏(包含VR、AR)与软件项目为什么需要有架构与框架的理念。然后讲解游戏引擎、服务端与客户端框架的区分,且梳理分析国内目前主流的游戏客户端架构模式以及优缺点分析。以及PureMVC与StrangeIOC两种MVC开源架构的总体技术刨析。二:PureMVC开源框架插件下载与搭建基础环境。    笔者以开发一个简单的“Hello World”为例,详细讲解PureMVC基础的概念,与框架搭建过程,让学员以较快的时间,学会PureMVC框架的基础理念与应用技巧。三:PureMVC架构设计理念。    基于前面简单版本的开发过程,初步刨析PureMVC架构以“Mediator+事件通知机制”总体设计理念。讲解什么是中介者设计模式(Mediator Pattern),以及事件机制在PureMVC中的基础作用。**分析PureMVC架构基本的开发流程,以及两种不同类型的消息机制。四:开发一款游戏项目(Flappy Birds),集成PureMVC+SUIFW(此框架课程均以发布)两种框架模式,讲解在具体开发一款游戏产品时,如何分析、把握框架与游戏脚本之间的关联关系,如果发挥PureMVC彻底解耦的开发理念。五:基于前面游戏项目的开发,进一步刨析PureMVC架构的高级开发技巧:    1:PureMVC框架类与脚本之间的相互通讯机理。    2:基于PureMVC框架原理,实现游戏项目模型与脚本的彻底分离技术,真正实现美工与研发的同步开发 与无缝对接技术。    3:掌握命令层(即:控制层)中SimpleCommand 与MacroCommand 的区别与具体开发应用技巧。 一、热更新系列(技术含量:中高级):A:《lua热更新技术中级篇》https://edu.csdn.net/course/detail/27087B:《热更新框架设计之Xlua基础视频课程》https://edu.csdn.net/course/detail/27110C:《热更新框架设计之热更流程与热补丁技术》https://edu.csdn.net/course/detail/27118D:《热更新框架设计之客户端热更框架(上)》https://edu.csdn.net/course/detail/27132E:《热更新框架设计之客户端热更框架(中)》https://edu.csdn.net/course/detail/27135F:《热更新框架设计之客户端热更框架(下)》https://edu.csdn.net/course/detail/27136二:框架设计系列(技术含量:中级): A:《游戏UI界面框架设计系列视频课程》https://edu.csdn.net/course/detail/27142B:《Unity客户端框架设计PureMVC篇视频课程(上)》https://edu.csdn.net/course/detail/27172C:《Unity客户端框架设计PureMVC篇视频课程(下)》https://edu.csdn.net/course/detail/27173D:《AssetBundle框架设计_框架篇视频课程》https://edu.csdn.net/course/detail/27169三、Unity脚本从入门到精通(技术含量:初级)A:《C# For Unity系列之入门篇》https://edu.csdn.net/course/detail/4560B:《C# For Unity系列之基础篇》https://edu.csdn.net/course/detail/4595C: 《C# For Unity系列之中级篇》https://edu.csdn.net/course/detail/24422D:《C# For Unity系列之进阶篇》https://edu.csdn.net/course/detail/24465四、虚拟现实(VR)与增强现实(AR):(技术含量:初级)A:《虚拟现实之汽车仿真模拟系统 》https://edu.csdn.net/course/detail/26618五、Unity基础课程系列(技术含量:初级) A:《台球游戏与FlappyBirds—Unity快速入门系列视频课程(第1部)》 https://edu.csdn.net/course/detail/24643B:《太空射击与移动端发布技术-Unity快速入门系列视频课程(第2部)》https://edu.csdn.net/course/detail/24645 C:《Unity ECS(二) 小试牛刀》https://edu.csdn.net/course/detail/27096六、Unity ARPG课程(技术含量:初中级):A:《MMOARPG地下守护神_单机版实战视频课程(上部)》https://edu.csdn.net/course/detail/24965B:《MMOARPG地下守护神_单机版实战视频课程(中部)》https://edu.csdn.net/course/detail/24968C:《MMOARPG地下守护神_单机版实战视频课程(下部)》https://edu.csdn.net/course/detail/24979
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值