@iamzealotwang
2014-08-26T04:18:12.000000Z
字数 8728
阅读 861
代码规范
| 类型 | 命名方式 | 例子 |
|---|---|---|
| 包名 | 驼峰方式,首字母小写 | com.copyEngine.scene.sceneResize |
| 类名 | 驼峰方式,首字母大写 | PlayerProfileDialog.as |
| 函数(判断类) | 以is开头+函数的意义 | isCanAddEachStep |
| 函数(返回数据) | 以get开头+函数意义 | getMissionSubStepMetaByID |
| 函数传参 | 以下划线开头 | |
| Private变量 | 首字母为m | mUserInfoData |
| Protected变量 | 首字母为m | mStarlingFactory |
| Array | 写清楚Array中装的是什么+Array结尾 | mAllUserInfoDataArray |
| Vector | 写清楚Vector中装的是什么+Vector结尾 | mAllUserInfoDataVector |
| Dictonary | 写清楚Dictonary中装的什么+Dic结尾 | mAllUserInfoDataDic |
包 的命名规则
首字母 小写 驼峰方式
copyEngine.scene.screenResize
对于一个标准的模块结构如下
ProfileDialog模块:
|-- profileDialog
| |-- handle
| |-- ProfileDialogDataHandleMediator.as
| |-- ProfileDialogDataHandleNotification.as
|
| |-- view
| |-- data
| |-- ProfileDialogUserInfoData.as
|
| |-- component
| |-- infoBar
| |-- ProfileDialogInfoBarComponent.as
| |-- groupBtn
| |-- ProfileDialogGroupBtnComponent.as
|
| |-- ProfileDialog.as
| |-- ProfileDialogPreset.as
| |-- ProfileDialogConfig.as
| |-- ProfileDialogDataHandleUtils.as
| |-- PofileDialogRPCName.as
说明
profileDialog 为模块名称,模块名称以 模块文档 内定义的名称为准
主目录为模块的入口文件
ProfileDialog.as 主入口,xxxDialog 或者 xxxScene
PresetProfileDialog.as 预加载(处理加载相应的资源or数据), Presetxxx
ProfileDialogConfig.as 该模块下需要使用的配置常量
注意!!: 仅仅可以在xxxConfig中定义常量,在其组件中不可以使用未定义的常量or定义常量
ProfileDialogDataHandleUtils view层和handle层交互的接口类
PofileDialogRPCName 存放和后端定义的所有的RPC接口名称
handle目录 处理数据部分逻辑
ProfileDialogDataHandleMediator.as 数据处理类 处理和ProfileDialog相关的所有数据逻辑
注意!! 这里处理是ProfileDialog相关的逻辑而不是 PlayerProfileVo(ProfileDialog中用于显示的数据源)相关的逻辑
ProfileDialogDataHandleNotification.as 定义Mediator发出的及处理的Notification
view目录 处理UI显示部分逻辑
view根目录标准模式下不设置任何类
view->data目录 在view中多个Component交互时候用到的数据源
ProfileDialogUserInfoData.as 数据类,注意 需要以Data结尾以方便和Vo数据源做区别
view->component目录 一个模块都是由多个Component组合围成
每个模块均单独建立一个文件夹
如果模块内部需要多个类完成,在该文件夹下再建立名字为sub的文件夹
> 首字母 **大写** 驼峰方式
> `ScreenResizeManger.as`
package app.dialog.userProfile{public class ProfileDialog extends BasicSceneDialog{//所有内部变量(private & protected) 均需要以m开头(Member)//名称为m+类名(去除前缀)--->m + UserInfo + Componentprivate var mUserInfoComponent:ProfileDialogUserInfoComponent;private var mUserEditComponent:ProfileDialogUserEditComponent;private var mInteractionButtonComponent:ProfileDialogIntercationButtonComponent;private var mUserInfoData:ProfileDialogUserInfoData;//Boolean值的命名以IsXXXX开头private var mIsCanAdd:Boolean;private var mIsCanVisit:Boolean;private var mIsCanChat:Boolean;//一个参数传入的变量以下划线开头,代码内部不再使用下划线//参数名称为私有名称将m改为下划线//mUserProfileVo----> _userProfileVopublic function UserProfileDialog(_userProfileVo:UserProfileVo, _isCanAdd:Boolean, _isCanVisit:Boolean, _isCanChat:Boolean){mUserProfileVo = _userProfileVo;mIsCanAdd = _isCanAdd;mIsCanVisit = _isCanVisit;mIsCanChat = _isCanChat;}//============================================////==Initialize&Dispose Function//============================================///** 无论 Dialog or Scene 初始化均 override doInitialize函数,该函数内* 仅仅为了组织该模块中使用的组件,不进行具体的逻辑处理*/override protected function doInitialize():void{mUserInfoData = new ProfileDialogUserInfoData();mUserInfoComponent = new ProfileDialogUserInfoComponent(mUserInfoData);//将Component注册进来及可完成相应的初始化registerUIComponent(mUserInfoComponent);mUserEditComponent = new ProfileDialogUserEditComponent(this, mUserInfoData);registerUIComponent(mUserCanEditComponent);...}/** 无论 Dialog or Scene 退出时候均 override doDispose函数,该函数内* 无需对已经注册的Component进行Dispose* 比如 mUserEditComponent&&mUserEditComponent.dispose() 这样是不必要的* (写了也不会报错)*/override protected function doDispose():void{}/** 如果需要在Component都初始化后执行某些操作 需要override该函数* 注意Component内部的onAddToStage函数会比Dialog or Scene的先执行*/override protected function onAddToStage():void {...}//============================================////==Public Interface Function//============================================///** 在Dialog or Scene存在的Public方法均为onClickXXXX系列,及由每个Component回调Manger* 执行相关的Logic*/public function onClickSaveBtnHandle():void{onClickComponentToEditForRefresh(false);ProfileDialogDataHandleUtils.editUserProfileInfo(mUserSharedData);MissionHandleUtils.gameMissionAddProgressFromEditProfile();}public function onClickComponentToEditForRefresh(_currentIsEdit:Boolean):void {...}//============================================////==OverrideAble Function//============================================///** 返回当前 Dialog or Scene 的素材类 如果不override该函数则建立空场景*/override protected function createUIContainerSymbolMeta():CESDisplayObjectMeta{return mResProxy.getGUIMeta(GUIName.FILE_PROFILE_DIALOG, "UserProfileDialog");}/** 如果当前模块需要监听某个Notification需要override该方法* 该方法需要返回一个Array,Array中每一项均是一个Array* 每一项的[0] 为需要监听的Notification* 每一项的[1] 为对该Notification的处理函数** 在Dispose时候引擎层会自动帮助取消监听,Logic不需要再Dispose函数中手动处理监听移除*/override protected function getListNotification():Array{return [[XXXXXNotification.UI_XXXX_VO_CHANGE,onReceiveXXXVoChange]];}}}
package app.dialog.userProfile.sub{public class ProfileDialogUserEditComponent extends CESceneUIComponentBasic{//每个模块内部对Component的引用类全都用mMangerprivate var mManager:UserProfileDialog;private var mUserInfoData:ProfileDialogUserInfoData;private var mEditUserLearnYearMc:DisplayObject;private var mEditUserLearnYearTF:CESUITextfield;...public function ProfileDialogUserEditComponent(_manager:UserProfileDialog, _userInfoData:UserProfileDialogShardData){mManager = _manager;mUserInfoData = _userInfoData;}//============================================////==Initialize&Dispose Function//============================================///** 每个Component在 doInitialize函数内部 调用DoubleCallFunction去取得相应的组件*/override protected function doInitialize():void{mRootRegisterHelper.registerDoubleCallFun(function():void{mEditUserLearnYearMc = mRootRegisterHelper.getDisplayTreeMc('root[xx]');mEditUserLearnYearTF = mRootRegisterHelper.getDisplayTreeMc('root[xx][xx]');});}/** 在 doOnAddToStage 函数内部执行组件的初始化逻辑* (不可以在doInitialize中执行初始化操作)*/override protected function doOnAddToStage():void{mEditUserLearnYearMc.visible = mUserInfoData.userID == GlobalConst.PLAYER_ID;mNormalUserLearnYearMc.visible = mUserInfoData.userID != GlobalConst.PLAYER_ID;mEditUserAboutMeMc.visible = mUserInfoData.userID == GlobalConst.PLAYER_ID;mNormalUserAboutMeMc.visible = mUserInfoData.userID != GlobalConst.PLAYER_ID;...refreshDisplay();}/** 不需要对从DoubleCall取出的Mc进行dispose操作* 比如 mEditUserLearnYearBtn && mEditUserLearnYearBtn.dispose() 这种操作是* 没有必要的(但是不会报错)*/override protected function doDispose():void{}//============================================////==Public Interface Function//============================================///** Component在80%情况下仅仅需要提供 refreshDisplay 方法即可,所有的数据更改操作 均在* mManger中执行(如果为viewData更改 mManger内部处理,如果为vo层更改则通过DataHandel)* 处理,处理结束后回调相应Component的 refreshDisplay方法*/public function refreshDisplay():void{mEditUserLearnTearTF.text = '' + mUserInfoData.learnYear;mNormalUserLearnYearTF.text = '' + mUserInfoData.learnYear;mEditUserAboutMeTF.text = '' + mUserInfoData.userAboutMeInfo;mNormalUserAboutMeTF.text = '' + mUserInfoData.userAboutMeInfo;//常量均要定义在模块的Config文件(ProfileDialogConfig)中mEditUserLearnYearMinusBtn.enabled = int(mEditUserLearnTearTF.text) > ProfileDialogConfig.USER_MIN_AGE_NUM;mEditUserLearnYearAddBtn.enabled = int(mEditUserLearnTearTF.text) < ProfileDialogConfig.UER_MAX_AGE_NUM;}//========================////==Private Function//=========================///** Componet内部的Event处理函数均以onClickXXXXBtn 或 onClickXXXXListCellRender 等命名* !!注意 在onClick事件中仅仅是对事件的一次封装,将点击事件原封不动交给Manger处理* 不可以在此处执行任何的数据处理* 不可以执行 mUserInfoData.learnYear++ 这种操作*/private function onClickAddUserLearnYearBtn():void{mManager.onClickAddUserLearnYearBtn();}private function onClickSubUserLearnYearBtn():void{mManager.onClickAddUserLearnYearBtn();}}}
package data.handle.playerProfile{public final class ProfileDialogDataHandleMediator implements IDataHandleMediator{public function ProfileDialogDataHandleMediator(){}/** 在onRegister函数内部 注册当前Mediator需要监听的所有消息*/public function onRegister():void{CENotificationFacade.instance.register(ProfileDialogDataHandleNotification.HANDLE_CHANGE_PLAYER_PROFILE, onHandleChangePlayerProfile);...}private function onHandleChangePlayerProfile(_arg:Object):void{var playerProfileVo:PlayerProfileVo=PlayerProfileVoProxy.instance.playerProfileVo;//执行数据逻辑相关的操作...//发送RPC通知服务器改变CERpcManger.instance.addRpcCommand(CERpcConfig.RPC_TYPE_IMMEDIATELY,PofileDialogRPCName.COMMAND_CHANGE_PLAYER_PROFILE,{vo: playerProfileVo});//通知UI层CENotificationFacade.instance.sendNotification(CommonPlayProfilVoHandleNotification.UI_PLAYER_PROFILE_VO_CHANGE);}}}}
| 简写 | 全称 | 例子 |
|---|---|---|
| Bg | BackGround | homeBg |
| Mc | DisplayObject | flagMc |
| Btn | Button | okBtn |
| TF | TextFiled | mMegTF |
| Dic | Dictonary | mAllMetaMissionDic |
| Obj | Object | mMissionJsonObj |
- 特别注意
- 不允许 简写
Array
Arrarr
VectorVec
只有在极少数情况下会出现需要使用动态类情况:
其他时候应该极力避免使用动态类,如果必须要使用,对于去内部属性也要有区分
需要采用mMissionJsonObj["xxx"] 这种方式取属性,不可以直接 mMissionJsonObj.xxx 取属性