Use the MVC design pattern to solve the problems of j2_application navigation

Source: Internet
Author: User

Programmers who develop MIDlet may often suffer from interface navigation problems. Especially when there are many interfaces, there are usually seven or eight interfaces, which can be a headache. This article describes how to use the MVC design pattern to solve such problems.

The MVC design pattern has been very mature and widely used in Web application development. Struts is a typical example of Apache open-source project. The essence of MVC is that the logic and display are separated and coordinated by the Controller. We usually feel that the controller is too large. This is a controversial issue. MIDP user interface development is relatively simple, there are only 20 categories. However, because navigation can only be implemented through command, when the interface is increased, if there is no valid organization, the program is very messy to write, the most critical is the poor readability, scalability, and maintainability of such programs.

The key to using MVC to solve this problem is to provide a bridge controller, which usually has a MIDlet as the parameter. For example, public uicontroller (phonebookmidlet PBM)
{
This. phonebookmidlet = PBM;
}
To pass events, you can define an internal class in which the event code is defined. This is very convenient to use, 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 various interface classes in this controller so that we can navigate based on different event codes.
Public void Init (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 describes how to implement navigation, we will not introduce the model. If you are careful, you may see that my code is implementing the function of a phone book. From getting started to mastering the record management system, I will introduce my own phone book. In the Controller class, the most important thing is to accept the event and then navigate, that is, the interface that cannot be displayed. Therefore, the event processing method is as follows.
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;
}
Default:
Break;
}
}

Public void handleevent (INT eventid, object [] OBJ)
{

} This is an overloaded method. When a parameter is passed, we call the following method.

Next we will look at the interface class. They usually include the Controller class, the interface item, and some commands.
Public newphoneui (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. setitemstatelistener (this );
} They usually pass the Controller class as a parameter to the constructor, and register the listener inside the constructor to draw the interface. They use the commandaction () method to pass event numbers to the Controller class for processing. For example
Public void commandaction (command arg0, displayable arg1)
{
// Todo auto-generated method stub
If (arg0 = backcommand)
{
Uicontroller
. Handleevent (uicontroller. eventid. event_newphone_back_mainui );
}

} In this way, the navigation problem is basically completed, and it is very easy to expand. You can add an interface class, initialize it in the Controller class, and add an appropriate event number.

Think of it, this solution is really classic !!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.