【问题描述】介绍设计模式的文章和书很多但只有使用时才能理解设计模式的妙处接上一篇《JavaMe连载()低级界面绘图之菜单》本文一则对UIController作出解释一则将自己理解的MVC设计模式结合实例与大家交流学习
【理论】什么是MVC?
MVC就是模型(model)视图(view)和控制(control)什么是模型呢?本例中模型就是对数据使用时的封装视图很容易理解那就是显示内容的具体表示控制呢?有很多人对视图和控制分不清在本例中控制包含视图控制器以及方法的封装
【实例】
先看工程结构如图所示
图 工程结构
工程中将用户数据单独封装作为model供控制器和视图调用将显示页面单独封装作为视图将视图控制器UIController和常用方法封装为utilUIController就是控制
UML图(后续更新时补充)
先看代码再分析工作机理
【代码清单】
MainMidletjava
packagecomtokenmidlet;
importjavaioIOException;
importjavaxmicroeditionlcdui*;
importjavaxmicroeditionmidletMIDlet;
importjavaxmicroeditionmidletMIDletStateChangeException;
importcomtokenutilUIController;
publicclassMainMidletextendsMIDlet{
privateDisplaydisplay;
privatestaticUIControllercontroller;
publicMainMidlet(){
//TODOAutogeneratedconstructorstub
super();
display=DisplaygetDisplay(this);
}
/*(nonJavadoc)
*@seejavaxmicroeditionmidletMIDlet#pauseApp()
*/
protectedvoidstartApp()throwsMIDletStateChangeException{
controller=newUIController(this);
try{
controllerinit();
}catch(IOExceptione){
//TODOAutogeneratedcatchblock
eprintStackTrace();
}//初始化RecordStore
}
/*(nonJavadoc)
*@seejavaxmicroeditionmidletMIDlet#pauseApp()
*/
protectedvoidpauseApp(){
thisnotifyPaused();
}
/*(nonJavadoc)
*@seejavaxmicroeditionmidletMIDlet#destroyApp(boolean)
*/
protectedvoiddestroyApp(booleanarg)throwsMIDletStateChangeException{
controller=null;
}
publicvoidsetCurrent(Displayabledisp){
displaysetCurrent(disp);
}
publicvoidsetCurrent(AlertalertDisplayabledisp){
displaysetCurrent(alertdisp);
}
publicDisplayablegetCurrent(){
returndisplaygetCurrent();
}
publicvoidexit(booleanarg){
try{
destroyApp(arg);
notifyDestroyed();
}catch(MIDletStateChangeExceptione){
//
}
}
}
模型(Model)
UserDataItemjava
packagecomtokenmodel;
importcomtokenutilStringDealMethod;
publicclassUserDataItem{
privateintid;
publicStringname=null;
publicStringpasswd=null;
publicUserDataItem(StringnameStringpasswd)
{
thisname=name;
thispasswd=passwd;
}
publicUserDataItem(intidbyte[]data){
thisid=id;
Stringtemp=newString(data);
Stringtemp_sub[]=StringDealMethodsplit(temp);
thisname=temp_sub[];
thispasswd=temp_sub[];
}
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
thisid=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
thisname=name;
}
publicStringgetPasswd(){
returnpasswd;
}
publicvoidsetPasswd(Stringpasswd){
thispasswd=passwd;
}
publicbyte[]getBytes(){
Stringtemp=null;
if(name==null||passwd==null){
returnnull;
}else{
temp=name++passwd;
}
returntempgetBytes();
}
}
控制(control)
UIControllerjava
packagecomtokenutil;
importjavaioIOException;
importjavaxmicroeditionlcdui*;
importcomtokenmidletMainMidlet;
importcomtokenmodel*;
importcomtokenview*;
//importponentsColor;
publicclassUIController{
privateMainMidletmidlet;
privateTokenDataRecordtokenRecord;
privateWelcomeScreenwelcomeScreen;
privateUserRegistreg;
privateActiveScreenactiveScreen;
privateMainScreenmainScreen;
privateGenPasswdgen;
privateCheckScreencheck;
privateViewTokenviewToken;
privateUserManagemanage;
privateShowHelphelp;
privateUserAuthauth;
privatePopUpTextBoxtextBox;
intid=;
publicUIController(MainMidletmidlet)
{
thismidlet=midlet;
tokenRecord=newTokenDataRecord();
}
publicvoidinit()throwsIOException{
try{
SplashScreensplashScreen=newSplashScreen();
setCurrent(splashScreen);
Threadsleep();
nfigureColor();
initObject();
//tokenRecorddb_deleteAllRecord();
if(tokenRecorddb_getRecord()==null)
{
//Systemoutprintln(add);
ChaosMethodsmethod=newChaosMethods();
TokenDataItemtokenItem=newTokenDataItem((methodChaosInitCode()+false)getBytes());
id=tokenRecorddb_addRecord(tokenItem);
}
//Systemoutprintln(id);
TokenDataItemtokenItem=tokenRecorddb_getRecord(id);
//Systemoutprintln(tokenItemtoken++tokenItemisActive);
if(tokenItemgetStatus()equals(false))
{
thishandleEvent(UIControllerEventIDEVENT_NEXT_WELCOME_SCREENnull);
}else
{
Stringflag=;
Object[]args={flag};
thishandleEvent(UIControllerEventIDEVENT_MAIN_SCREENargs);
}
}catch(InterruptedExceptione){
//TODOAutogeneratedcatchblock
eprintStackTrace();
}
}
privatevoidinitObject()
{
welcomeScreen=newWelcomeScreen(this);
reg=newUserRegist(this);
activeScreen=newActiveScreen(this);
textBox=newPopUpTextBox(this输入文本TextFieldANY);
}
//getMethod
publicvoidsetCurrent(Displayabledisp){
midletsetCurrent(disp);
}
publicvoidsetCurrent(AlertalertDisplayabledisp){
midletsetCurrent(alertdisp);
}
//定义事件ID内部类
publicstaticclassEventID{
privateEventID(){
}
publicstaticfinalbyteEVENT_EXIT=;//退出
publicstaticfinalbyteEVENT_NEXT_WELCOME_SCREEN=;//欢迎界面
publicstaticfinalbyteEVENT_NEXT_USER_REGIST_SCREEN=;//用户注册
publicstaticfinalbyteEVENT_USER_REGIST_EDIT=;//用户注册编辑
publicstaticfinalbyteEVENT_USER_REGIST_EDIT_BACK=;//用户注册编辑返回
publicstaticfinalbyteEVENT_NEXT_ACTIVE_SCREEN=;
//
}
//事件处理
publicvoidhandleEvent(inteventIDObject[]args)
{
switch(eventID)
{
caseEventIDEVENT_EXIT:
{
midletexit(false);
break;
}
caseEventIDEVENT_NEXT_WELCOME_SCREEN:
{
welcomeScreenshow();
midletsetCurrent(welcomeScreen);
break;
}
caseEventIDEVENT_NEXT_USER_REGIST_SCREEN:
caseEventIDEVENT_USER_REGIST_EDIT_BACK:
{
regshow(args);
Threadthread=newThread(reg);
threadstart();
midletsetCurrent(reg);
break;
}
caseEventIDEVENT_USER_REGIST_EDIT:
{
textBoxinit(args);
midletsetCurrent(textBox);
break;
}
caseEventIDEVENT_NEXT_ACTIVE_SCREEN:
{
activeScreenshow(args);
Threadthread=newThread(activeScreen);
threadstart();
midletsetCurrent(activeScreen);
break;
}
//
default:
break;
}
}
}