In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about what is the MVC design pattern in JavaMe development. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
[problem description] there are many articles and books about design patterns, but only when you use them can you understand the beauty of design patterns. To explain UIController, one will be their own understanding of MVC design patterns, combined with examples, to communicate and learn with you.
[theory] what is MVC?
MVC is model, view, and control. What is a model? In this case, the model is the encapsulation of data when it is used. The view is easy to understand, and that is the concrete representation of what is displayed. What about control? There are many people who can't tell the difference between view and control. In this case, the control includes the view controller and the encapsulation of the method.
[example]
1. First look at the engineering structure, as shown in figure 1.
Fig. 1 Engineering structure
In the project, the user data is encapsulated separately as a model. For the controller and view to call. The display page is encapsulated separately as a view. Encapsulate the view controller UIController and common methods as util. UIController is control.
2. UML diagram (added in subsequent updates)
First look at the code, and then analyze the working mechanism
[code list]
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 () } / / initialize 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 (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 (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, "input text", ", 1000, TextField.ANY);} / / getMethod public void setCurrent (Displayable disp) {midlet.setCurrent (disp);} public void setCurrent (Alert alert, Displayable disp) {midlet.setCurrent (alert, disp) } / / define the event ID inner class public static class EventID {private EventID () {} public static final byte EVENT_EXIT = 0 public static final byte EVENT_NEXT_WELCOME_SCREEN / exit public static final byte EVENT_NEXT_WELCOME_SCREEN = 1 pm / welcome interface public static final byte EVENT_NEXT_USER_REGIST_SCREEN = 2 / / user registration public static final byte EVENT_USER_REGIST_EDIT = 3ram / user registration editor public static final byte EVENT_USER_REGIST_EDIT_BACK = 4user registration editor return public static final byte EVENT_NEXT_ACTIVE_SCREEN = 5 / / event handling 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 () {} / / Open RecordStore. If not, create public void openDataBase () {try {info = RecordStore.openRecordStore (RECORDSTORE_NAME, true);} catch (RecordStoreException ex) {info = null }} / / close RecordStore public void closeDataBase () {if (info = null) {try {info.closeRecordStore (); info=null } catch (RecordStoreException ex) {} / / add record 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 } / / Update record 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) {}} / / access a record 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;} / / Delete a record public void db_deleteRecord (int id) {try {this.openDataBase (); info.deleteRecord (id); this.closeDataBase () } catch (RecordStoreException ex) {}} / / Delete all records public void db_deleteAllRecord () {try {RecordStore.deleteRecordStore (RECORDSTORE_NAME);} catch (RecordStoreException ex) {}} / / access all records public Vector db_getRecords () {Vector items=new Vector (10Lab 3) This.openDataBase (); / / Open 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 () / / close RecordStore} / / end finally return items;} / / A simple comparator private class InnerComparator implements RecordComparator {public int compare (byte [] rec1, byte [] rec2) {if (rec1.length > rec2.length) return FOLLOWS; else if (rec1.length)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.