溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

JavaMe開發(fā)中什么是MVC設(shè)計(jì)模式

發(fā)布時(shí)間:2021-10-29 16:03:25 來源:億速云 閱讀:134 作者:柒染 欄目:編程語言

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)JavaMe開發(fā)中什么是MVC設(shè)計(jì)模式,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

【問題描述】介紹設(shè)計(jì)模式的文章和書很多,但只有使用時(shí),才能理解設(shè)計(jì)模式的妙處。對UIController作出解釋,一則將自己理解的MVC設(shè)計(jì)模式,結(jié)合實(shí)例,與大家交流學(xué)習(xí)。

【理論】什么是MVC?

MVC就是模型(model)、視圖(view)和控制(control)。什么是模型呢?本例中,模型就是對數(shù)據(jù)使用時(shí)的封裝。視圖很容易理解,那就是顯示內(nèi)容的具體表示??刂颇??有很多人對視圖和控制分不清。在本例中,控制包含視圖控制器以及方法的封裝。

【實(shí)例】

1、先看工程結(jié)構(gòu),如圖1所示。

JavaMe開發(fā)中什么是MVC設(shè)計(jì)模式
圖1 工程結(jié)構(gòu)

工程中將用戶數(shù)據(jù)單獨(dú)封裝,作為model。供控制器和視圖調(diào)用。將顯示頁面單獨(dú)封裝,作為視圖。將視圖控制器UIController和常用方法封裝為util。UIController就是控制。

2、UML圖(后續(xù)更新時(shí)補(bǔ)充)

先看代碼,再分析工作機(jī)理

【代碼清單】

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內(nèi)部類      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;          }      }  }

UserDataRecord.java

package com.token.util;   import java.util.Vector;   import javax.microedition.rms.RecordComparator;  import javax.microedition.rms.RecordEnumeration;  import javax.microedition.rms.RecordStore;  import javax.microedition.rms.RecordStoreException;   import com.token.model.*;   public class UserDataRecord {      private static final String RECORDSTORE_NAME="USER_DB";      private static RecordStore info;            public UserDataRecord(){      }            //打開RecordStore,沒有則創(chuàng)建      public void openDataBase() {          try {              info = RecordStore.openRecordStore(RECORDSTORE_NAME, true);          }catch (RecordStoreException ex) {                          info =null;          }      }      //關(guān)閉RecordStore      public void closeDataBase() {          if (info!= null) {              try {                  info.closeRecordStore();                  info=null;              } catch (RecordStoreException ex) {}          }      }      //增加記錄      public int db_addRecord(UserDataItem item) {                                       try {                      this.openDataBase();                      byte[] data=item.getBytes();                      int id=info.getNextRecordID();                      info.addRecord(data,0,data.length);                      this.closeDataBase();                      return id;                  } catch (RecordStoreException ex) { }                    return -1;      }      //更新記錄      public void db_updateRecord(UserDataItem item){               try {                  this.openDataBase();                        byte[] data=item.getBytes();                  info.setRecord(item.getId(),data,0,data.length);                  this.closeDataBase();              } catch (RecordStoreException ex) { }      }      //訪問一條記錄      public UserDataItem db_getRecord(int id){          UserDataItem item=null;               try {                  this.openDataBase();                  item = new UserDataItem(id,info.getRecord(id));                  this.closeDataBase();              } catch (RecordStoreException ex) { }            return item;      }      //刪除一條記錄      public void db_deleteRecord(int id){              try {                  this.openDataBase();                  info.deleteRecord(id);                  this.closeDataBase();              } catch (RecordStoreException ex) {}      }            //刪除所有記錄      public void db_deleteAllRecord(){              try {                  RecordStore.deleteRecordStore(RECORDSTORE_NAME);              } catch (RecordStoreException ex) {}      }            //訪問所有記錄      public Vector db_getRecords(){          Vector items=new Vector(10,3);          this.openDataBase();//打開RecordStore          RecordEnumeration enum1=null;          int ind=0;          try{              UserDataItem item=null;              enum1=info.enumerateRecords(null,new InnerComparator(),false);                            while(enum1.hasPreviousElement()){                  ind=enum1.previousRecordId();                  item=new UserDataItem(ind,info.getRecord(ind));                  items.addElement(item);              }                        }catch(Exception ex){ex.printStackTrace();}          finally{                  try{                      enum1.destroy();                  }catch(Exception e){}                                    this.closeDataBase();//關(guān)閉RecordStore          }//end finally                return items;      }      //一個(gè)簡單的比較器      private class InnerComparator implements RecordComparator{          public int compare(byte[] rec1, byte[] rec2){              if(rec1.length>rec2.length)                  return FOLLOWS;              else if(rec1.length<rec2.length)                  return PRECEDES;              else                   return EQUIVALENT;          }       }  }

視圖(view)

WelcomeScreen.java

package com.token.view;   import javax.microedition.lcdui.*;  import javax.microedition.lcdui.game.GameCanvas;   import com.token.util.UIController;  import com.token.util.StringDealMethod;  import com.token.view.components.*;    public class WelcomeScreen extends GameCanvas {       private UIController controller;      private Graphics graphics;      private Font ft;      private String info;      private String info_wrap[];      private int width;      private int height;            private Menu menu;      private Head head;      private BackGroud backGroud;            public WelcomeScreen(UIController control) {          // TODO Auto-generated constructor stub          super(false);          controller=control;          setFullScreenMode(true);          graphics = getGraphics();                    width = getWidth();          height = getHeight();                    menu = new Menu(this);          head = new Head(this);          backGroud = new BackGroud(this);      }       public void show() {          // TODO Auto-generated method stub          //clearScreen();          backGroud.drawBackGroud(this, graphics);          head.drawHead(this,graphics,"");          menu.drawMenu(this, graphics,"下一步","退出");          drawBody();      }            public void drawBody()      {          ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE);                    info = "歡迎使用!\n"                 +"1 MVC測試;\n"                 +"2 自動(dòng)換行測試,繪制可自動(dòng)識別換行的字符串。\n";          info_wrap = StringDealMethod.format(info, width-10, ft);                    graphics.setColor(Color.text);          graphics.setFont(ft);          for(int i=0; i<info_wrap.length; i++)          {              graphics.drawString(info_wrap[i], 5, i * ft.getHeight()+40, Graphics.TOP|Graphics.LEFT);          }      }            public void clearScreen()      {          graphics.setColor(0xff,0xff,0xff);          graphics.fillRect(0, 0, width, height);      }            protected void keyPressed(int keycode)      {          switch(keycode)          {              case KeyID.SOFT_RIGHT:              {                  controller.handleEvent(UIController.EventID.EVENT_EXIT,null);                  break;              }              case KeyID.SOFT_LEFT:              {                  String editor = "regist_name";                  Object [] args = {"registScreen",editor, null,null,null};                  controller.handleEvent(UIController.EventID.EVENT_NEXT_USER_REGIST_SCREEN,args);                  break;              }              default:;          }      }        }

UserRegist.java

package com.token.view;   import javax.microedition.lcdui.Font;  import javax.microedition.lcdui.Graphics;  import javax.microedition.lcdui.game.GameCanvas;  import com.token.model.*;  import com.token.util.*;  import com.token.view.components.*;   public class UserRegist extends GameCanvas implements Runnable {       private UIController controller;      private Graphics graphics;            private Font ft;            private Menu menu;      private Head head;      private BackGroud backGroud;            private UserDataRecord userRecord;       private String title;            private TextEdit textEdit_name;      private TextEdit textEdit_passwd;      private TextEdit textEdit_passwd_re;      private int textEdit_name_x;      private int textEdit_name_y;      private int textEdit_passwd_x;      private int textEdit_passwd_y;      private int textEdit_passwd_re_x;      private int textEdit_passwd_re_y;      private int currentlySelectedIndex = 0;            private String username;      private String passwd;      private String passwd_re;            long caretBlinkDelay = 500L;      long lastCaretBlink = 0;      private String object_name;      private String editor;      private boolean cursorBlinkOn1;      private boolean cursorBlinkOn2;      private boolean cursorBlinkOn3;            private int width;      private int height;            public UserRegist(UIController control)       {          super(false);          this.controller=control;          this.title = "用戶注冊";          setFullScreenMode(true);          graphics = getGraphics();                    width = getWidth();          height = getHeight();                    menu = new Menu(this);          head = new Head(this);          backGroud = new BackGroud(this);                    userRecord = new UserDataRecord();                    textEdit_name = new TextEdit(this);          textEdit_passwd = new TextEdit(this);          textEdit_passwd_re = new TextEdit(this);      }       public void show(Object[] args) {          // TODO Auto-generated method stub          setFullScreenMode(true);                    object_name = ((String)args[0]!=null)?(String)args[0]:"";          editor = ((String)args[1]!=null)?(String)args[1]:"";          username = ((String)args[2]!=null)?(String)args[2]:"";          passwd = ((String)args[3]!=null)?(String)args[3]:"";          passwd_re = ((String)args[4]!=null)?(String)args[4]:"";                    if(editor.equals("regist_name"))          {              cursorBlinkOn1 = true;              cursorBlinkOn2 = false;              cursorBlinkOn3 = false;              currentlySelectedIndex =0;          }          else if(editor.equals("regist_passwd"))          {              cursorBlinkOn1 = false;              cursorBlinkOn2 = true;              cursorBlinkOn3 = false;              currentlySelectedIndex =1;          }          else if(editor.equals("regist_passwd_re"))          {              cursorBlinkOn1 = false;              cursorBlinkOn2 = false;              cursorBlinkOn3 = true;              currentlySelectedIndex =2;          }                    //System.out.println(object_name);          //System.out.println(editor);          draw();          redraw();      }       public void draw()      {          //clearScreen();          backGroud.drawBackGroud(this, graphics);          head.drawHead(this,graphics,this.title);          menu.drawMenu(this,graphics,"下一步","退出");          drawBody();      }       private void redraw()      {          switch(currentlySelectedIndex)          {              case 0:              {                  cursorBlinkOn2 = false;                  cursorBlinkOn3 = false;                  editor = "regist_name";                  break;              }              case 1:              {                  cursorBlinkOn1 = false;                  cursorBlinkOn3 = false;                  editor = "regist_passwd";                  break;              }              case 2:              {                  cursorBlinkOn1 = false;                  cursorBlinkOn2 = false;                  editor = "regist_passwd_re";                  break;              }              default:;          }                    textEdit_name.drawTextBox(this, graphics, username, textEdit_name_x, textEdit_name_y, cursorBlinkOn1);          textEdit_passwd.drawTextBox(this, graphics, passwd, textEdit_passwd_x, textEdit_passwd_y, cursorBlinkOn2);          textEdit_passwd.drawTextBox(this, graphics, passwd_re, textEdit_passwd_re_x, textEdit_passwd_re_y, cursorBlinkOn3);          textEdit_name.flushGraphics();      }       public void drawBody()      {          int margin =5;          ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE);                    String info = "用戶名:\n";          String info_wrap1[] = StringDealMethod.format(info, width-10, ft);                    graphics.setFont(ft);          graphics.setColor(Color.text);          for(int i=0; i<info_wrap1.length; i++)          {              graphics.drawString(info_wrap1[i],5, (i) * ft.getHeight()+40, Graphics.TOP|Graphics.LEFT);          }                    textEdit_name_x = 5;          textEdit_name_y = info_wrap1.length * ft.getHeight()+40;          textEdit_name.drawTextBox(this, graphics, username, textEdit_name_x, textEdit_name_y, cursorBlinkOn1);                    info = "用戶密碼:\n";          String info_wrap2[] = StringDealMethod.format(info, width-10, ft);                graphics.setFont(ft);          graphics.setColor(Color.text);          for(int i=0; i<info_wrap2.length; i++)          {              graphics.drawString(info_wrap2[i],5, (i+info_wrap1.length) * ft.getHeight()+textEdit_name.height+margin+40, Graphics.TOP|Graphics.LEFT);          }                    textEdit_passwd_x = 5;          textEdit_passwd_y = (info_wrap1.length+info_wrap2.length) * ft.getHeight()+textEdit_name.height+margin+40;          textEdit_passwd.drawTextBox(this, graphics, passwd, textEdit_passwd_x, textEdit_passwd_y, cursorBlinkOn2);                info = "密碼確認(rèn):\n";          String info_wrap3[] = StringDealMethod.format(info, width-10, ft);                graphics.setFont(ft);          graphics.setColor(Color.text);          for(int i=0; i<info_wrap3.length; i++)          {              graphics.drawString(info_wrap3[i],5, (i+info_wrap1.length+info_wrap2.length) * ft.getHeight()+textEdit_name.height+textEdit_passwd.height+2*margin+40, Graphics.TOP|Graphics.LEFT);          }                    textEdit_passwd_re_x = 5;          textEdit_passwd_re_y = (info_wrap1.length+info_wrap2.length+info_wrap3.length) * ft.getHeight()+textEdit_name.height+textEdit_passwd.height+2*margin+40;          textEdit_passwd_re.drawTextBox(this, graphics, passwd_re, textEdit_passwd_re_x, textEdit_passwd_re_y, cursorBlinkOn3);                      }       public void clearScreen()      {          graphics.setColor(0xff,0xff,0xff);          graphics.fillRect(0, 0, width, height);      }       public void checkTimeStamp()      {          long currentTime = System.currentTimeMillis();          //System.out.println("1");          if(lastCaretBlink + caretBlinkDelay < currentTime)          {              //System.out.println("2");              if(editor.equals("regist_name"))              {                  cursorBlinkOn1 =! cursorBlinkOn1;                  cursorBlinkOn2 = false;                  cursorBlinkOn3 = false;              }              else if(editor.equals("regist_passwd"))              {                  cursorBlinkOn1 = false;                  cursorBlinkOn2 =! cursorBlinkOn2;                  cursorBlinkOn3 = false;              }              else if(editor.equals("regist_passwd_re"))              {                  cursorBlinkOn1 = false;                  cursorBlinkOn2 = false;                  cursorBlinkOn3 =! cursorBlinkOn3;              }              lastCaretBlink = currentTime;          }      }       public void run()      {          //System.out.println("run");          while(true)          {              checkTimeStamp();                            redraw();              try               {                  synchronized(this)                  {                      //System.out.println("3");                      wait(50L);                  }                         }              catch(Exception e)              {                  e.printStackTrace();              }                        }      }       protected void keyPressed(int keyCode)      {          switch(keyCode)          {              case KeyID.SOFT_RIGHT:              {                  controller.handleEvent(UIController.EventID.EVENT_EXIT,null);                  break;              }              case KeyID.SOFT_LEFT:              {                  if(username!="" && passwd!=""&&passwd_re!="")                  {                      if(passwd.equals(passwd_re))                      {                                                                              userRecord.db_deleteAllRecord();                          if(userRecord.db_getRecord(1)==null)                          {                              UserDataItem userItem = new UserDataItem(1,(username+","+passwd).getBytes());                              userRecord.db_addRecord(userItem);                              userItem = null;                              System.gc();                          }                                                    String update = "start";                          Object [] args = {"activeScreen", null, update};                          controller.handleEvent(UIController.EventID.EVENT_NEXT_ACTIVE_SCREEN,args);                      }                  }                  break;              }              case KeyID.KEY_EDIT:              case KEY_NUM0:              case KEY_NUM1:              case KEY_NUM2:              case KEY_NUM3:              case KEY_NUM4:              case KEY_NUM5:              case KEY_NUM6:              case KEY_NUM7:              case KEY_NUM8:              case KEY_NUM9:              {                  //System.out.println(editor);                  Object[] args = {object_name,editor,username,passwd,passwd_re};                  controller.handleEvent(UIController.EventID.EVENT_USER_REGIST_EDIT,args);                  break;              }              default:;          }                        keyCode = getGameAction(keyCode);          switch(keyCode)          {              case UP:              case LEFT:              {                  currentlySelectedIndex--;                  if(currentlySelectedIndex<0)                  {                      currentlySelectedIndex=0;                  }                  else                 {                      redraw();                  }                  break;              }              case DOWN:              case RIGHT:              {                  currentlySelectedIndex++;                  if(currentlySelectedIndex>2)                  {                      currentlySelectedIndex=2;                  }                  else                 {                      redraw();                  }                                    break;              }          }      }   }

*TextEdit是利用GameCanvas繪制的自定義文本編輯框。后續(xù)文章將給出具體實(shí)現(xiàn)。

【分析】

1 在MainMidlet調(diào)用控制器UIController,并向UIController傳遞midlet作為參數(shù)。

controller=new UIController(<span style="color:#ff0000;">this</span>);  try {      controller.init();      } catch (IOException e) {      // TODO Auto-generated catch block      e.printStackTrace();      }//

2 控制器的實(shí)現(xiàn)是通過一個(gè)事件的機(jī)制實(shí)現(xiàn)的, 在UIController中,先建立一個(gè)事件ID的內(nèi)部類。

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;         //...      }

3 一次性初始化所有界面,分配內(nèi)存,調(diào)用時(shí),只是執(zhí)行視圖類的show方法。為什么不將初始話放在調(diào)用時(shí)進(jìn)行呢?主要是因?yàn)橐晥D類有多次重用,如果每一次調(diào)用都new(初始化,分配內(nèi)存)一次,手機(jī)有限的內(nèi)存很快會被用光,這是會出現(xiàn)一些程序自動(dòng)退出的問題。

private void initObject()  {      welcomeScreen = new WelcomeScreen(this);      reg= new UserRegist(this);      activeScreen = new ActiveScreen(this);      ...      textBox = new PopUpTextBox(this,"輸入文本","", 1000, TextField.ANY);  }

4 控制器對視圖的處理采用狀態(tài)機(jī)實(shí)現(xiàn)

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;         }     }

5 視圖類初始化時(shí),需要將控制器作為參數(shù)初始化,以對事件做出判斷。如WelcomeScreen.java中先做出如下聲明:

private UIController controller;

再在構(gòu)造函數(shù)中,傳遞控制器

public WelcomeScreen(UIController control) {          // TODO Auto-generated constructor stub          super(false);          controller=control;

6 視圖切換事件響應(yīng)采用如下方式,在keyPressed中,對按鍵事件進(jìn)行判斷,然后調(diào)用UIController的handEvent方法。

protected void keyPressed(int keycode)      {          switch(keycode)          {              case KeyID.SOFT_RIGHT:              {                  controller.handleEvent(UIController.EventID.EVENT_EXIT,null);                  break;              }              case KeyID.SOFT_LEFT:              {                  String editor = "regist_name";                  Object [] args = {"registScreen",editor, null,null,null};                  controller.handleEvent(UIController.EventID.EVENT_NEXT_USER_REGIST_SCREEN,args);                  break;              }              default:;          }      }        }

7 控制器可以通過handEvent的args傳遞參數(shù),如

String update = "start";  Object [] args = {"activeScreen", null, update};  controller.handleEvent(UIController.EventID.EVENT_NEXT_ACTIVE_SCREEN,args);

UserRegist傳遞了一個(gè)update變量給下一個(gè)視圖。

8 在看一下模型,在UserDataItem中存儲的是用戶注冊的信息。利用UserDataRecord類對記錄進(jìn)行操作。

在視圖類中,通過以下方式調(diào)用:

先聲明

private UserDataRecord userRecord;

構(gòu)建對象

userRecord = new UserDataRecord();

使用對象

userRecord.db_deleteAllRecord();                          if(userRecord.db_getRecord(1)==null)                          {                              UserDataItem userItem = new UserDataItem(1,(username+","+passwd).getBytes());                              userRecord.db_addRecord(userItem);                              userItem = null;                              System.gc();                          }

9 包c(diǎn)om.token.view.components是對視圖類中使用的自定義控件的封裝

綜述,這樣就實(shí)現(xiàn)了模型、視圖、控制的分離。

上述就是小編為大家分享的JavaMe開發(fā)中什么是MVC設(shè)計(jì)模式了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI