【問題描述】介紹設計模式的文章和書很多
【理論】什麼是MVC?
MVC就是模型(model)
【實例】
圖
工程中將用戶數據單獨封裝
先看代碼
【代碼清單】
MainMidlet
package com
import java
import javax
import javax
import javax
import com
public class MainMidlet extends MIDlet {
private Display display;
private static UIController controller;
public MainMidlet() {
// TODO Auto
super();
display=Display
}
/* (non
* @see javax
*/
protected void startApp() throws MIDletStateChangeException {
controller=new UIController(this);
try {
controller
} catch (IOException e) {
// TODO Auto
e
}//初始化RecordStore
}
/* (non
* @see javax
*/
protected void pauseApp() {
this
}
/* (non
* @see javax
*/
protected void destroyApp(boolean arg
controller=null;
}
public void setCurrent(Displayable disp){
display
}
public void setCurrent(Alert alert
display
}
public Displayable getCurrent(){
return display
}
public void exit(boolean arg
try{
destroyApp(arg
notifyDestroyed();
}catch(MIDletStateChangeException e){
//
}
}
}
模型(Model)
UserDataItem
package com
import com
public class UserDataItem {
private int id;
public String name = null;
public String passwd = null;
public UserDataItem(String name
{
this
this
}
public UserDataItem(int id
this
String temp=new String(data);
String temp_sub[] = StringDealMethod
this
this
}
public int getId(){
return id;
}
public void setId(int id){
this
}
public String getName(){
return name;
}
public void setName(String name){
this
}
public String getPasswd(){
return passwd;
}
public void setPasswd(String passwd){
this
}
public byte[] getBytes(){
String temp=null;
if(name==null||passwd==null){
return null;
}else{
temp=name+
}
return temp
}
}
控制(control)
UIController
package com
import java
import javax
import com
import com
import com
//import ponents
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 =
public UIController(MainMidlet midlet)
{
this
tokenRecord = new TokenDataRecord();
}
public void init() throws IOException{
try {
SplashScreen splashScreen = new SplashScreen();
setCurrent(splashScreen);
Thread
nfigureColor();
initObject();
//tokenRecord
if(tokenRecord
{
//System
ChaosMethods method = new ChaosMethods();
TokenDataItem tokenItem = new TokenDataItem(
id=tokenRecord
}
//System
TokenDataItem tokenItem
//System
if(tokenItem
{
this
}else
{
String flag =
Object [] args = {flag
this
}
} catch (InterruptedException e) {
// TODO Auto
e
}
}
private void initObject()
{
welcomeScreen = new WelcomeScreen(this);
reg= new UserRegist(this);
activeScreen = new ActiveScreen(this);
textBox = new PopUpTextBox(this
}
//getMethod
public void setCurrent(Displayable disp){
midlet
}
public void setCurrent(Alert alert
midlet
}
//定義事件ID內部類
public static class EventID{
private EventID(){
}
public static final byte EVENT_EXIT =
public static final byte EVENT_NEXT_WELCOME_SCREEN =
public static final byte EVENT_NEXT_USER_REGIST_SCREEN =
public static final byte EVENT_USER_REGIST_EDIT =
public static final byte EVENT_USER_REGIST_EDIT_BACK =
public static final byte EVENT_NEXT_ACTIVE_SCREEN =
//
}
//事件處理
public void handleEvent( int eventID
{
switch (eventID)
{
case EventID
{
midlet
break;
}
case EventID
{
welcomeScreen
midlet
break;
}
case EventID
case EventID
{
reg
Thread thread = new Thread(reg);
thread
midlet
break;
}
case EventID
{
textBox
midlet
break;
}
case EventID
{
activeScreen
Thread thread = new Thread(activeScreen);
thread
midlet
break;
}
//
default:
break;
}
}
}
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27275.html