Design Patterns in ActionScript-Command

本文介绍了一款基于J2ME平台的地图应用开发过程中的挑战与解决方案。针对拖拽地图时用户体验不佳的问题,通过引入命令模式,使用栈来管理请求,有效避免了过多并发连接导致的延迟,提升了应用性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A few months ago, I was an intern of a company. I joined a group which builds a LBS application, and our target platform including J2ME and Android. I was asked to make a J2ME demo.

This demo was just like the Google map on J2ME. Eh, actually, we did something more than that; but you don’t need to know, just considering we’re going to build a Google map on J2ME platform.

 

As our project going, we can fetched the map tile and display it on the screen. When the user pressed the direction key, we created a new connection to fetch the map tile, and then display it on the screen. The code is maybe as follows. I hope you can understand the following code :)

  1. Var directionBtn : Button = new Button () ;
  2. directionBtn . addMouseEventListener ( Mouse . Click , “ downloadTile ) ;
  3.  
  4. function   downloadTile () : void
  5. {
  6. Var   conn : Connection = ConnectionFactory . getConnection () ;
  7.  
  8. DrawOnScreen ( conn ) ;
  9. }

Note: the above code is pseudo code. Don’t try to complier it.

If you ever use some similar application on your phone, you may realize where the problem is. Let me figure it out. When the user wants to drag the map, the application will seems to be as no response. It means, when the user holds a direction, the application will produce many connections, and only 5 connections can be executed at the same time, and each connection may be finished in 3 or 5 seconds. (Note: 5 connections at one time are depending on our test, and each connection’s time spending is depends on the network.)

So, our problem is that, when the user drags the map, the application will download all the tiles. Actually, not all the tiles are needed, the tiles the user wants is where the user drags to not the way it passes.

Wow, genius, maybe you have your own solution now. And our solution is use a stack to hold the commands. That is, when the user presses a direction key, it puts a common into the stack, and we execute the commands from the top one. So, the most recent request will execute first. And now, our code will be as below.

  1. directionBtn . addMouseEventListener ( Mouse . Click , “ downloadTile ) ;
  2. function   downloadTile () : void
  3. {
  4. requestStack . push ( new   Request ()) ;
  5. }

The most difference between the two is that we put a method into an object. And this is the Command pattern.

See the intent now :)

Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

– By THE GOF BOOK

With this pattern, you can make a request queue, or implement a logger system or the UNDO / REDO command.

If you want, you can take a look at the example code :)DownloadDownload Full Project

Enjoy!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值