`
shake863
  • 浏览: 664811 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Design Patterns in ActionScript-Command

阅读更多

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

Enjoy!

分享到:
评论

相关推荐

    Go Design Patterns for Real-World Projects epub

    Go Design Patterns for Real-World Projects 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    Go Design Patterns for Real-World Projects azw3

    Go Design Patterns for Real-World Projects 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    Data Structures and Algorithms with Object-Oriented Design Patterns in CSharp - Bruno R. Preiss.chm

    Data Structures and Algorithms with Object-Oriented Design Patterns in CSharp - Bruno R. Preiss

    Laracasts - design-patterns-in-php

    "Laracasts - design-patterns-in-php.torrent"则可能是一个BT种子文件,用于通过BitTorrent协议下载整个课程的大型数据包,这通常包括所有视频讲座和其他相关文件。 在课程"设计模式在PHP中"中,你可能会学到以下...

    Design-Patterns-in-Modern-C++

    The topic of Design Patterns sounds dry, academically constipated and, in all honesty, done to death in almost every programming language imaginable—including programming languages such as JavaScript...

    Design Patterns in Modern C++--2018

    Reusable Approaches for Object-Oriented... Work with the behavioral patterns such as chain of responsibility, command, iterator, mediator and more Apply functional design patterns such as Monad and more

    design-patterns-for-humans设计模式中文翻译版

    https://github.com/kamranahmedse/design-patterns-for-humans 中文翻译,实例修改位JAVA代码

    Design-Patterns-In-Kotlin,在kotlin中实现的设计模式.zip

    《Kotlin设计模式实战解析》 在编程领域,设计模式是一种通用、可重用的...开源项目"Design-Patterns-In-Kotlin"则是一个很好的学习资源,它提供了各种设计模式在Kotlin中的具体实现,有助于开发者深入学习和实践。

    Design Patterns in ActionScript

    《ActionScript设计模式》是软件开发领域中针对ActionScript编程语言的一种实践指南,它深入探讨了如何在ActionScript项目中应用经典的设计模式。设计模式是软件工程中的宝贵经验总结,它们是解决常见问题的可复用...

    Go Design Patterns for Real-World Projects.epub

    Go: Design Patterns for Real-World Projects What You Will Learn: Install and configure the Go development environment to quickly get started with your first program Use the basic elements of the ...

    Java+Enterprise+Design+Patterns+-+Patterns+in+Java+Volume+3

    Java+Enterprise+Design+Patterns+-+Patterns+in+Java+Volume+3Java+Enterprise+Design+Patterns+-+Patterns+in+Java+Volume+3

    Pro-Objective-C-Design-Patterns-for-iOS

    《Pro-Objective-C-Design-Patterns-for-iOS》是一本专注于在iOS平台上利用Objective-C语言实现设计模式的专业书籍。书中旨在帮助已经有一定Cocoa开发基础的开发者,通过掌握设计模式的实践应用,提升软件开发的生产...

    java-design-patterns-master_JAVAdesignpattens_java_design_

    这个压缩包“java-design-patterns-master”显然是一个专注于Java设计模式的学习资源,旨在帮助开发者深入理解和应用这些模式。下面我们将详细探讨Java设计模式及其在实际开发中的应用。 1. **单例模式(Singleton...

    design-patterns-c-sharp

    "design-patterns-c-sharp"这个主题涵盖了C#编程环境中如何应用这些模式的知识。 首先,我们需要理解设计模式的基本概念。设计模式并不具体到任何特定的代码或库,而是一种描述在特定情况下如何设计软件组件的通用...

    Design Patterns in C#

    design pattern in C# language

Global site tag (gtag.js) - Google Analytics