Developing MIDET programmers may often be troubled by the navigation problem of the interface, especially when the interface is more, usually seven, eight interfaces will make people hurt. This article tells how to apply MVC design patterns to solve such problems.
The MVC design model is very mature and widely used in the development of Web Application, and the Struts of Apache's open source projects is a typical example. The essence of MVC is logical and display separation, and coordinated through the controller. Usually we will feel that the controller is compared, this is a controversial issue. MIDP user interface development is relatively simple, only 20 categories. However, since navigation generally can only be implemented through Command, in the case of increased interface, if there is no effective organization, the program is very chaotic, the most deadly is the ability to readability, poor extensibility, and poor maintainability.
The key to adding MVC to solve this problem is to provide a controller for a bridge, which typically has a MIDLET as a parameter. For example, Public UiController (PhoneBookmidlet PBM) {this.PhoneBookmidlet = PBM;} In order to transfer events, you can define an internal class that defines the code of the event. This is very convenient for, for example, public static class eventid {private eventid () {}
public static final byte EVENT_NEW_RECORD_SELECTED = 1; public static final byte EVENT_SAVE_RECORD = 2; public static final byte EVENT_NEWPHONE_BACK_MAINUI = 3; public static final byte EVENT_LISTPHONE_BACK_MAINUI = 4; public static final byte EVENT_SEARCHUI_BACK_MAINNUI = 5;
public static final byte ADD_NEW_RECORD = 100; public static final byte SEARCH_RECORD = 101; public static final byte CLEAR_RECORD = 102; public static final byte LIST_RECORD = 103; public static final byte HELP = 104;} we need to initialize the controller in each Interface class, so we can navigate according to different event code. public void init (Model model) {this.display = Display.getDisplay (phoneBookMIDlet); this.model = model; indexFunctionUI = new IndexFunctionUI (this); infomationUI = new InfomationUI (); newPhoneUI = new NewPhoneUI (this); listPhoneUI = New ListphoneUi (this); searchPhoneui = new searchPhoneui (this); Displaywelcome ();
Public void setCurrent (Displayable DISP) {Display.SetCurrent (DISP); PUBLIC VOID SETCURRENT (Alert Alert, Displayable Disp) {Display.SetCurrent (Alert, DISP);} Since this article mainly tells how to implement navigation, therefore regarding Model does not Any introduction. If you are careful, you may see that I have these code is the function of completing a phone book. I will introduce my own phone book you wrote from the Record Management System from the entry into the master. The most important thing in the controller class is that the event is then navigating, that is, the unless interface. Therefore, its event processing method is like this. public void handleEvent (int eventID) {switch (eventID) {case EventID.ADD_NEW_RECORD: {display.setCurrent (newPhoneUI); break;} case EventID.LIST_RECORD: {display.setCurrent (listPhoneUI); break;} case EventID.SEARCH_RECORD: {display.setCurrent (searchPhoneUI); break;} case EventID.EVENT_NEWPHONE_BACK_MAINUI: {display.setCurrent (indexFunctionUI); break;} case EventID.EVENT_LISTPHONE_BACK_MAINUI: {display.setCurrent (indexFunctionUI); break;} case EventID.EVENT_SEARCHUI_BACK_MAINNUI: {display .Setcurrent (IndexFunctionUi); Break;}
Public void Handleevent (int eventid, object [] obj) {
} This is an overloaded method that we call the following methods when there is a parameter passed.
Next we look at the interface class, they usually include controller classes, and Items in the interface also have some Command. public NewPhoneUI (UIController uicontroller) {super (Title.add_record); this.uicontroller = uicontroller; nameField = new TextField (Title.name, null, 25, TextField.ANY); mobileField = new TextField (Title.mobile, null, 25 , TextField.PHONENUMBER); choice = new ChoiceGroup (Title.choice, ChoiceGroup.MULTIPLE); phoneField = new TextField (Title.phone, null, 25, TextField.PHONENUMBER); emailField = new TextField (Title.email, null, 25 , TextField.EMAILADDR); choice.append (Title.detail, null); this.append (nameField); this.append (mobileField); this.append (choice); this.addCommand (saveCommand); this.addCommand (backCommand ); This.SetcommandListener (this); this);} Usually they transfer controller class as a parameter to the constructor, and register the listener inside the constructor, draw the interface. Them to pass through the event number commandAction () method to the controller class to process, e.g. public void commandAction (Command arg0, Displayable arg1) {if (arg0 == backCommand) {uicontroller .handleEvent (UIController.EventID.EVENT_NEWPHONE_BACK_MAINUI);}} This will basically complete the navigation problem, which is very easy to expand, add an interface class, and then initialize and add the appropriate event number in the controller class. Please visit Mingjava's website: http://www.j2medev.com/