Design Patterns in ActionScript-Command

insurance-256x256 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 :)

Var directionBtn:Button = new Button();
directionBtn.addMouseEventListener(Mouse.Click, “downloadTile”);

function downloadTile():void
Var conn:Connection = ConnectionFactory.getConnection();


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.

directionBtn.addMouseEventListener(Mouse.Click, “downloadTile”);
function downloadTile():void
requestStack.push(new Request());

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.


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 :) Download Download Full Project


Share and Enjoy:
  • Digg
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

One Response to “Design Patterns in ActionScript-Command”

Leave a Reply