【更新】本文將適時做出更新,所以現在不是終稿,如有不當之處,敬請指出。
【問題描述】介紹設計模式的文章和書很多,但只有使用時,才能理解設計模式的妙處。接上一篇《JavaMe連載(2)-低級界面繪圖之菜單》,本文一則對UIController作出解釋,一則將自己理解的MVC設計模式,結合實例,與大家交流學習。
【理論】什麼是MVC?
MVC就是模型(model)、視圖(vIEw)和控制(control)。什麼是模型呢?本例中,模型就是對數據使用時的封裝。視圖很容易理解,那就是顯示內容的具體表示。控制呢?有很多人對視圖和控制分不清。在本例中,控制包含視圖控制器以及方法的封裝。
【實例】
1、先看工程結構,如圖1所示。
圖1 工程結構
工程中將用戶數據單獨封裝,作為model。供控制器和視圖調用。將顯示頁面單獨封裝,作為視圖。將視圖控制器UIController和常用方法封裝為util。UIController就是控制。
2、UML圖(後續更新時補充)
先看代碼,再分析工作機理
【代碼清單】
MainMidlet.Java
- package com.token.midlet;
- import Java.io.IOException;
- import Javax.microedition.lcdui.*;
- import Javax.microedition.midlet.MIDlet;
- import Javax.microedition.midlet.MIDletStateChangeException;
- import com.token.util.UIController;
- public class MainMidlet extends MIDlet {
- private Display display;
- private static UIController controller;
- public MainMidlet() {
- // TODO Auto-generated constructor stub
- super();
- display=Display.getDisplay(this);
- }
- /* (non-Javadoc)
- * @see Javax.microedition.midlet.MIDlet#pauseApp()
- */
- protected void startApp() throws MIDletStateChangeException {
- controller=new UIController(this);
- try {
- controller.init();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }//初始化RecordStore
- }
- /* (non-Javadoc)
- * @see Javax.microedition.midlet.MIDlet#pauseApp()
- */
- protected void pauseApp() {
- this.notifyPaused();
- }
- /* (non-Javadoc)
- * @see Javax.microedition.midlet.MIDlet#destroyApp(boolean)
- */
- protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
- controller=null;
- }
- public void setCurrent(Displayable disp){
- display.setCurrent(disp);
- }
- public void setCurrent(Alert alert, Displayable disp){
- display.setCurrent(alert, disp);
- }
- public Displayable getCurrent(){
- return display.getCurrent();
- }
- public void exit(boolean arg0){
- try{
- destroyApp(arg0);
- notifyDestroyed();
- }catch(MIDletStateChangeException e){
- //
- }
- }
- }
模型(Model)
UserDataItem.Java
- package com.token.model;
- import com.token.util.StringDealMethod;
- public class UserDataItem {
- private int id;
- public String name = null;
- public String passwd = null;
- public UserDataItem(String name,String passwd)
- {
- this.name = name;
- this.passwd = passwd;
- }
- public UserDataItem(int id,byte[] data){
- this.id=id;
- String temp=new String(data);
- String temp_sub[] = StringDealMethod.split(temp, ",");
- this.name = temp_sub[0];
- this.passwd = temp_sub[1];
- }
- public int getId(){
- return id;
- }
- public void setId(int id){
- this.id=id;
- }
- public String getName(){
- return name;
- }
- public void setName(String name){
- this.name = name;
- }
- public String getPasswd(){
- return passwd;
- }
- public void setPasswd(String passwd){
- this.passwd = passwd;
- }
- public byte[] getBytes(){
- String temp=null;
- if(name==null||passwd==null){
- return null;
- }else{
- temp=name+","+passwd;
- }
- return temp.getBytes();
- }
- }
控制(control)
UIController.Java
- package com.token.util;
- import Java.io.IOException;
- import Javax.microedition.lcdui.*;
- import com.token.midlet.MainMidlet;
- import com.token.model.*;
- import com.token.vIEw.*;
- //import com.token.vIEw.components.Color;
- public class UIController {
- private MainMidlet midlet;
- private TokenDataRecord tokenRecord;
- private WelcomeScreen welcomeScreen;
- private UserRegist reg;
- private ActiveScreen activeScreen;
- private MainScreen mainScreen;
- private GenPasswd gen;
- private CheckScreen check;
- private ViewToken vIEwToken;
- private UserManage manage;
- private ShowHelp help;
- private UserAuth auth;
- private PopUpTextBox textBox;
- int id = 1;
- public UIController(MainMidlet midlet)
- {
- this.midlet = midlet;
- tokenRecord = new TokenDataRecord();
- }
- public void init() throws IOException{
- try {
- SplashScreen splashScreen = new SplashScreen();
- setCurrent(splashScreen);
- Thread.sleep(1000);
- Configure.configureColor();
- initObject();
- //tokenRecord.db_deleteAllRecord();
- if(tokenRecord.db_getRecord(1)==null)
- {
- //System.out.println("add");
- ChaosMethods method = new ChaosMethods();
- TokenDataItem tokenItem = new TokenDataItem(1,(method.ChaosInitCode()+",false").getBytes());
- id=tokenRecord.db_addRecord(tokenItem);
- }
- //System.out.println(id);
- TokenDataItem tokenItem1 = tokenRecord.db_getRecord(id);
- //System.out.println(tokenItem1.token+","+tokenItem1.isActive);
- if(tokenItem1.getStatus().equals("false"))
- {
- this.handleEvent(UIController.EventID.EVENT_NEXT_WELCOME_SCREEN,null);
- }else
- {
- String flag = "0";
- Object [] args = {flag,""};
- this.handleEvent(UIController.EventID.EVENT_MAIN_SCREEN,args);
- }
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- private void initObject()
- {
- welcomeScreen = new WelcomeScreen(this);
- reg= new UserRegist(this);
- activeScreen = new ActiveScreen(this);
- ...
- textBox = new PopUpTextBox(this,"輸入文本","", 1000, TextFIEld.ANY);
- }
- //getMethod
- public void setCurrent(Displayable disp){
- midlet.setCurrent(disp);
- }
- public void setCurrent(Alert alert, Displayable disp){
- midlet.setCurrent(alert, disp);
- }
- //定義事件ID內部類
- public static class EventID{
- private EventID(){
- }
- public static final byte EVENT_EXIT = 0;//退出
- public static final byte EVENT_NEXT_WELCOME_SCREEN = 1;//歡迎界面
- public static final byte EVENT_NEXT_USER_REGIST_SCREEN = 2;//用戶注冊
- public static final byte EVENT_USER_REGIST_EDIT = 3;//用戶注冊編輯
- public static final byte EVENT_USER_REGIST_EDIT_BACK = 4;//用戶注冊編輯返回
- public static final byte EVENT_NEXT_ACTIVE_SCREEN = 5;
- //...
- }
- //事件處理
- public void handleEvent( int eventID, Object[] args)
- {
- switch (eventID)
- {
- case EventID.EVENT_EXIT:
- {
- midlet.exit(false);
- break;
- }
- case EventID.EVENT_NEXT_WELCOME_SCREEN:
- {
- welcomeScreen.show();
- midlet.setCurrent(welcomeScreen);
- break;
- }
- case EventID.EVENT_NEXT_USER_REGIST_SCREEN:
- case EventID.EVENT_USER_REGIST_EDIT_BACK:
- {
- reg.show(args);
- Thread thread = new Thread(reg);
- thread.start();
- midlet.setCurrent(reg);
- break;
- }
- case EventID.EVENT_USER_REGIST_EDIT:
- {
- textBox.init(args);
- midlet.setCurrent(textBox);
- break;
- }
- case EventID.EVENT_NEXT_ACTIVE_SCREEN:
- {
- activeScreen.show(args);
- Thread thread = new Thread(activeScreen);
- thread.start();
- midlet.setCurrent(activeScreen);
- break;
- }
- //...
- default:
- break;
- }
- }
- }