歡迎您光臨本站 註冊首頁

小議J2ME手機遊戲引擎程序架構

←手機掃碼閱讀     火星人 @ 2014-03-10 , reply:0

  隨著國內3G的啟動,新一代移動通信大潮已經到來.技術的進步是的無線網路取得不錯的發展,移動互聯網巨大前景也隨著顯現.無線網路速度的提高,催生大量的手機聯網應用程序.手機聯網功能的強化,是的手機應用更具價值,進一步擴展了手機功能.

  現在我們就來實現一個基於J2ME的手機聯網程序.考慮到手機運算資源的限制,我們採用客戶端/伺服器 style="COLOR: #000000" href="http://server.it168.com/" target=_blank>伺服器的模式來實現,J2ME只做為客戶端運行於手機上,負責展現和處理簡單的業務邏輯,保存少量的關鍵數據;伺服器端採用J2EE實現,負責保存用戶數據,以及響應在線用戶的複雜業務邏輯.

  在這裡,服務端J2EE的實現不是本文的重點,只進行簡單的描述,我們主要對手機客戶端J2ME的講解.

  在J2ME客戶端,我們可以採用的MVC軟體架構模式,進行邏輯分層,使代碼更為清晰,權責更為明確,也利於代碼維護和功能升級.

  1、Handle(Controller):它既做為控制器,也做為簡單邏輯處理器.如處理網路請求,網路消息分發.它是關鍵層,涉及到整體結構的每一層,用於控制應用程序的流程.它處理事件並作出響應.複雜業務邏輯的處理權責已經分化到伺服器端,只處理簡單的邏輯和少量的數據訪問,此層沒有進一步劃分出業務層,統一劃為處理層.

  2、DAO:數據訪問對象(Data Access Object),用於封裝數據的於Database的讀取和存儲 style="COLOR: #000000" href="http://storage.it168.com/" target=_blank>存儲操作.便於Handle的調用完成簡單業務邏輯的處理.

  3、Database:用來存儲少量數據,即負責關鍵數據的持久化.在J2ME中,RMS(Record Management System)是這個層次主要承擔者.在實際應用中,如果數據間關係很簡單,也可以選用文件進行保存,如XML格式或普通文本格式.Handler會控制對Database的存儲和提取,用來View層顯示.

  4、Model:數據模型用於封裝與應用程序的業務邏輯相關的數據以及對數據的處理方法.數據的抽象化分離了具體的View,也方便Handle對數據持久化操作.

  5、View: 這層用來顯示用戶界面,並且響應和處理鍵盤的指令.將Handler層指派的一些信息顯示出來,並且將需求信息送給Handler去處理.這層直接於Handler溝通,不會直接涉及到Database或網路信息.

  我們這裡做的這個聯網程序是一個即時讀取互聯網資訊的工具,從任意一個網站的資訊列表頁面獲取其中的資訊標題和鏈接,返回給手機客戶端.而在手機客戶端選取一條資訊進行打開時,又去聯網獲取文章的文本內容.從伺服器端返回的數據中只包含文本信息,無任何除資訊資訊URL的HTML數據.這樣就大大減少了無用數據傳輸,降低了網路通信費用,提高訪問速度,更為方便地訪問WWW網站.

  服務端要處理的業務邏輯有:根據提供的鏈接地址,獲取頁面內容並進行分析,提取資訊條目或資訊內容數據.返回給手機客戶端.這部分的處理邏輯有一定的複雜,需要耗費一定的資源,將其劃分到服務端進行處理.

  客戶端只負責發送請求和接收服務端返回的數據,進行簡單處理后將內容呈現到用戶瀏覽界面.相當於一個網頁瀏覽器.

  --演示截圖-

-

1 package com.efan.wb.view;
2
3 import javax.microedition.lcdui.Command;
4 import javax.microedition.lcdui.CommandListener;
5 import javax.microedition.lcdui.Display;
6 import javax.microedition.lcdui.Displayable;
7 import javax.microedition.lcdui.Form;
8 import javax.microedition.lcdui.TextBox;
9 import javax.microedition.midlet.MIDlet;
10 import com.efan.wb.handle.WbAction;
11

12 public class WebBrowser extends MIDlet implements CommandListener {
13
14 private TextBox textbox;
15 private Display display = null;
16 private Form mainForm = null;
17 public static final Command exitCommand = new Command("Exit", Command.OK, 1);
18
19 public void startApp() {

20 Display.getDisplay(this).setCurrent(textbox);
21 if (display == null) {
22 display = Display.getDisplay(this);
23 }
24 mainForm = new Form("News Form");
25
26 // 從控制器載入
27 WbAction action = new WbAction();
28 String newsList = action.getNews();
29
30 mainForm.append(newsList);// 載入默認新聞標題列表

31 mainForm.addCommand(exitCommand);
32 mainForm.setCommandListener(this);
33 display.setCurrent(mainForm);
34 }
35
36 public void commandAction(Command cmd, Displayable displayable) {
37 if (cmd == exitCommand) {
38 destroyApp(false);
39 notifyDestroyed();
40 }
41 }
42
43 public void pauseApp() {
44 }
45
46 public void destroyApp(boolean unconditional) {

47 }
48 }
49

1 package com.efan.wb.handle;
2
3 import java.io.DataInputStream;
4 import java.io.IOException;
5
6 import javax.microedition.io.Connector;
7 import javax.microedition.io.HttpConnection;
8 import com.efan.wb.dao.WbDao;
9 import com.efan.wb.model.UrlEntity;
10
11 public class WbAction {
12
13 public String getNews() {
14

15 // 從RMS中獲取默認的URL
16 WbDao dao = new WbDao();
17 UrlEntity ue = dao.getDefaultURL();
18 String url = ue.getUrl();
19
20 WebExplorer we = new WebExplorer(url);
21 we.start();// 啟動網路新聞獲取線程
22
23 // 輪循等待操作完成
24 while (!we.isComplete()) {
25 // 超時處理,此略

26 }
27 return we.getNewsList();
28 }
29
30 // 為了簡化代碼,把這個訪問網路的線程類作為內部類
31 class WebExplorer extends Thread {
32
33 private String newsList;
34
35 private String url;
36
37 public WebExplorer(String url) {
38 this.url = url;
39 }
40
41 public WebExplorer() {

42 }
43
44 public void setUrl(String url) {
45 this.url = url;
46 }
47
48 private boolean isComplete() {
49 return this.newsList == null ? false : true;
50 }
51
52 public String getNewsList() {
53 return newsList;

54 }
55
56 public void run() {
57 try {
58 HttpConnection conn = (HttpConnection) Connector.open(this.url);
59 DataInputStream is = conn.openDataInputStream();
60 this.newsList = is.readUTF();
61 } catch (IOException e) {
62 e.printStackTrace();
63 }
64 }
65 }
66 }
67

1 package com.efan.wb.dao;

2
3 import com.efan.wb.model.UrlEntity;
4
5 public class WbDao {
6
7 public UrlEntity getDefaultURL() {
8
9 UrlEntity ue = new UrlEntity();
10 // 訪問RMS,查詢默認的URL,此略,直接硬編碼來測試
11 String url = "http://localhost:8080/rss/news";
12 String name = "Test Web URL";
13
14 ue.setName(name);

15 ue.setUrl(url);
16
17 return ue;
18 }
19 }

1 package com.efan.wb.model;
2
3 public class UrlEntity {
4
5 private String name;
6
7 private String url;
8
9 public String getName() {
10 return name;
11 }
12

13 public void setName(String name) {
14 this.name = name;
15 }
16
17 public String getUrl() {
18 return url;
19 }
20
21 public void setUrl(String url) {
22 this.url = url;
23 }
24
25 }

1 package cn.rssweb.site.web;

2
3 import java.io.DataOutputStream;
4 import java.io.IOException;
5 import java.util.Iterator;
6 import java.util.List;
7
8 import javax.servlet.ServletException;
9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12
13 import cn.rssweb.edp.component.spider.RobotFactory;
14 import cn.rssweb.edp.component.spider.model.DataModel;
15 import cn.rssweb.edp.component.spider.robot.Robot;
16
17 public class NewsListServlet extends HttpServlet {

18
19 @Override
20 protected void doGet(HttpServletRequest request, HttpServletResponse response)
21 throws ServletException, IOException {
22
23 String url = "http://www.techweb.com.cn/news/20.shtml";
24
25 Robot robot = RobotFactory.getInstance(Robot.HTML,url);//伺服器邏輯處理核心類,此略
26 List list = robot.parseList();
27
28 Iterator it = list.iterator();
29 int i=0;

30 StringBuffer sb = new StringBuffer();
31 while(it.hasNext()){
32 i ;
33 DataModel data = (DataModel)it.next();
34 String title = data.getLinkText();
35 sb.append(i).append(".").append(title).append("n");
36 }
37
38 DataOutputStream dos = new DataOutputStream(response.getOutputStream());
39
40 dos.writeUTF(sb.toString());
41 dos.flush();

42 dos.close();
43
44 }
45
46 @Override
47 protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1)
48 throws ServletException, IOException {
49
50 super.doPost(arg0, arg1);
51 }
52
53 }


[火星人 ] 小議J2ME手機遊戲引擎程序架構已經有796次圍觀

http://coctec.com/docs/java/show-post-61904.html