Application Building Blocks part 2 - Commands
Leveraging the browser back and forward functionality in Flash using Commands and the Gugga Flash Framework
With this new posting we would like to introduce one more of the key aspects used in the Gugga Flash Framework, the Commands, along with a real world example how to apply them. As a wide aspect framework GUGGAFF takes advantage of this well known concept in software development to introduce some key features. Commands' sole purpose is to abstract requests and encapsulate their logic. They decouple the caller from the callee, allowing undoable actions and preprocessing.
We will show you how you can apply that to manage the browser history functionality in a Flash application, or in more familiar terms the browser back and forward buttons. Although there are already examples of such implementations most of what we seen is for applications build using frame based approach. But what happens when you do not have frames and you want to build a flash application using OO methodology? The need to persist application state, ability to buffer what the user does with your application and restore that state.
Command Pattern
We are not going to lay out in detail what the command pattern is all about but for the sake of context and explanations how we applied it in GuggaFF here is a brief layout:
„Sometimes it's necessary to issue requests to objects without knowing anything about the operation being requested or the receiver of the request....
The Command pattern lets objects make requests of unspecified application objects by turning the request itself into an object. This object can be stored and passed around like other objects. The key to this pattern is an abstract Command class, which declares an interface for executing operations. In the simplest form this interface includes an abstract Execute operation. Concrete Command subclasses specify a receiver-action pair by storing the receiver as an instance variable and by implementing Execute to invoke the request. The receiver has the knowledge required to carry out the request...“

GuggaFF and Commands
In GUGGAFF the Commands primary usage is to abstract navigational requests. A navigation request might imply a various expected results: opening or closing a secton, a popup, following a deep link or cross section navigation, position a component in a state.
When you build an application with GUGGAFF, you can base the navigation architecture of your application on commands for navigating through the Sections or inside components.
The Benefits
Using commands allows us to abstract the invoker of a navigation requests from the receiver.
We are also able to maintain a command history buffer , which helps us to support browser history events such as back and forward.
Key Players and their responsibilities
There are several key players in the framework that cooperate using commands.
MenuItemsController, responsible for execution of NavigationCommands in response to user activities with your application's navigation menu. It is the invoker from the diagram above.
NavigationCommand, a specific command implementation possesing enough information to navigate through the application.
OpenPopupCommand, another implementation of command used for opening popup within your application
CommandManager , a singleton responsible for executing commands without being aware of their specific implementations since they expose the command interface. It also acts as a history buffer, but we will get to that later.
Application, a command receiver for NavigationCommands, available through the IApplicationCommandReceiver interface.
How does it all work?
We have already made a brief lay out of the command pattern and also identified the key players participating. Here is a more detailed explanation about the players in the GuggaFF and how they cooperate.
The Application class was identified as the command receiver for navigation requests, and it should provide the IApplicationCommandReceiver interface to the commands. The interface defines methods for:
1. Navigating inside the application using a given Section Path (a dot notation of sections or components hierarchy describing it uniquely)
We would expect the commands in our history buffer to be able to reverse the application state. Considering that the application also exposes the following methods :
2.Creating navigation state (Memento Desing pattern) preserving the current navigation Path.
3.Setting navigation state to a previously preserved one
A MenuItemsController in our application acts as an invoker for commands. To ensure the invokation, we equipe the controller with commands for the items it aggregates. It is usually done durring the initialization of the controller.
To maintain a history buffer, the MenuItemsController delegates the command execution to the CommandManager singletone. Once the execution is delegated to him, the command is stored in the buffer and processed.
The CommandManager also needs to be able to reverse the application state. Reversing of the application state is available calling the manager, to navigate to a particular command in its buffer. To identify the command in the buffer we use a command index. A command index should be remembered whenever we want to be able to reverse to a state.
This ability to reverse state forces the commands to create and store a navigation state memento right before their execution.
The next aspect is how the command pattern implemented within the framework gives us the ability to manage the browser history back and forward.
Real world example of managing browser history back and forward
Solution architecture
The first issue we face when building a solution to manage browser back and forward buttons is the fact that the browser history buffer is not affected when the flash application is navigated. A way to solve this problem, is to issue parallel requests to the browser, every time the application is navigated, and it is also an important point that these requests should not cause reload of the flash movie. To do that, we choose to update the hash of the browser address bar.
Once we are able to use the browser buttons, we would expect them to reverse the application to a previous navigational state. In order to do that, our application is forced to maintain its own history buffer for navigational states.
It should meet the following requirements
1.Navigational requests should be abstracted so the history buffer can work with them
2.The history buffer should be able to reverse the application state using the stored requests
To meet the requirements we abstract navigational requests into commands and store them into the history buffer. The commands also might be undone or re-executed later to reverse the application state.
Once we have the commands we should put them together with the items in the browser history, which we do by passing a command identifier to the address bar hash. Given that we are able to reverse the application navigational state when the browser history is navigated.
In addition to the previously mentioned key players we also:
1.Need a class abstracting the communication with the address bar. This is the AddressBar.
2.And a class maintaining the communication between the AddressBar and the CommandManager. This is the AddressBarManager.
The following sequence diagram illustrates the interactions between the participants in our solution.

[DOWNLOAD]
You can test the application here:
History Management Sample App
You can download the sample application from here:
HistoryManagement.zip
With this new posting we would like to introduce one more of the key aspects used in the Gugga Flash Framework, the Commands, along with a real world example how to apply them. As a wide aspect framework GUGGAFF takes advantage of this well known concept in software development to introduce some key features. Commands' sole purpose is to abstract requests and encapsulate their logic. They decouple the caller from the callee, allowing undoable actions and preprocessing.
We will show you how you can apply that to manage the browser history functionality in a Flash application, or in more familiar terms the browser back and forward buttons. Although there are already examples of such implementations most of what we seen is for applications build using frame based approach. But what happens when you do not have frames and you want to build a flash application using OO methodology? The need to persist application state, ability to buffer what the user does with your application and restore that state.
Command Pattern
We are not going to lay out in detail what the command pattern is all about but for the sake of context and explanations how we applied it in GuggaFF here is a brief layout:
„Sometimes it's necessary to issue requests to objects without knowing anything about the operation being requested or the receiver of the request....
The Command pattern lets objects make requests of unspecified application objects by turning the request itself into an object. This object can be stored and passed around like other objects. The key to this pattern is an abstract Command class, which declares an interface for executing operations. In the simplest form this interface includes an abstract Execute operation. Concrete Command subclasses specify a receiver-action pair by storing the receiver as an instance variable and by implementing Execute to invoke the request. The receiver has the knowledge required to carry out the request...“

GuggaFF and Commands
In GUGGAFF the Commands primary usage is to abstract navigational requests. A navigation request might imply a various expected results: opening or closing a secton, a popup, following a deep link or cross section navigation, position a component in a state.
When you build an application with GUGGAFF, you can base the navigation architecture of your application on commands for navigating through the Sections or inside components.
The Benefits
Key Players and their responsibilities
There are several key players in the framework that cooperate using commands.
How does it all work?
We have already made a brief lay out of the command pattern and also identified the key players participating. Here is a more detailed explanation about the players in the GuggaFF and how they cooperate.
The Application class was identified as the command receiver for navigation requests, and it should provide the IApplicationCommandReceiver interface to the commands. The interface defines methods for:
1. Navigating inside the application using a given Section Path (a dot notation of sections or components hierarchy describing it uniquely)
We would expect the commands in our history buffer to be able to reverse the application state. Considering that the application also exposes the following methods :
2.Creating navigation state (Memento Desing pattern) preserving the current navigation Path.
3.Setting navigation state to a previously preserved one
A MenuItemsController in our application acts as an invoker for commands. To ensure the invokation, we equipe the controller with commands for the items it aggregates. It is usually done durring the initialization of the controller.
To maintain a history buffer, the MenuItemsController delegates the command execution to the CommandManager singletone. Once the execution is delegated to him, the command is stored in the buffer and processed.
The CommandManager also needs to be able to reverse the application state. Reversing of the application state is available calling the manager, to navigate to a particular command in its buffer. To identify the command in the buffer we use a command index. A command index should be remembered whenever we want to be able to reverse to a state.
This ability to reverse state forces the commands to create and store a navigation state memento right before their execution.
The next aspect is how the command pattern implemented within the framework gives us the ability to manage the browser history back and forward.
Real world example of managing browser history back and forward
Solution architecture
The first issue we face when building a solution to manage browser back and forward buttons is the fact that the browser history buffer is not affected when the flash application is navigated. A way to solve this problem, is to issue parallel requests to the browser, every time the application is navigated, and it is also an important point that these requests should not cause reload of the flash movie. To do that, we choose to update the hash of the browser address bar.
Once we are able to use the browser buttons, we would expect them to reverse the application to a previous navigational state. In order to do that, our application is forced to maintain its own history buffer for navigational states.
It should meet the following requirements
1.Navigational requests should be abstracted so the history buffer can work with them
2.The history buffer should be able to reverse the application state using the stored requests
To meet the requirements we abstract navigational requests into commands and store them into the history buffer. The commands also might be undone or re-executed later to reverse the application state.
Once we have the commands we should put them together with the items in the browser history, which we do by passing a command identifier to the address bar hash. Given that we are able to reverse the application navigational state when the browser history is navigated.
In addition to the previously mentioned key players we also:
1.Need a class abstracting the communication with the address bar. This is the AddressBar.
2.And a class maintaining the communication between the AddressBar and the CommandManager. This is the AddressBarManager.
The following sequence diagram illustrates the interactions between the participants in our solution.

[DOWNLOAD]
You can test the application here:
History Management Sample App
You can download the sample application from here:
HistoryManagement.zip

10 Comments:
Great, great work on the framework. Just wanted to point out that the sample doesn't work for me with IE 6 and FP 9 though it does work on Firefox. It seems that the command index is not incrementing.
Hello again,
Refering to the previous post, the problem with MTASC is that you have a package named mx it confuses MTASC since he goes looking for flashes own mx package.
I think that, for practical purposes, you should change the name of the package. It´s my humble opinion but I´m sure it will help the people who use your framework in a more strict environment. Thank you for your work. And please keep up :)
regarding the previous post. MTASC desconsiders your mx package overruling it for flashes mx package. Of course this is a classpath issue but I believe that many, as I, would still like to have a classpath directed to flashes default class directory.
I don't know of a solution unless changing the package name. On the other hand the framework could be aimed not to please MTASC users. nonetheless thank you for your huge work.
We would like to move the discussion about MTASC to the Google Group.
There is a new post there that would hopefully answer your question as to why we have mx package and how to compile with MTASC
Please follow this link:
http://groups.google.com/group/guggaff/browse_thread/thread/808bb3404e5770e2
The previuos post was cutting off the URL to the post in the group.
Google Group Post
We have updated the sample and now it works for all browsers. We had to change the implementation because of IE and now it works using external interface and only for flash 8 and above. This offcourse had influenced a bit the implementation in flash but nothing major
could you post the .fla for the history management example?
Its in the zip file at the end of the article there is a link
hi there,
first of all, big kuddos for the work done!
in this example we rely on the memory generated by the application to move from states in the application.
What if you want to use deeplinking?
in that case, the application would have no memory, so the parameters obtained through the url should send us to an specific state/section of the application...
it seems to me that this example was krafted without that case in mind, so my question is: does Gugga have an out of the box solution for deeplinking?
regards,
goliatone
Hi goliatone,
We are really happy to hear that you appreciate our work and we invite you to join in our Google Group http://groups.google.com/group/guggaff.
According to the deeplinking in GuggaFF we have a class called gugga.common.DeepLinksSerializer. This class is responsible for serialization and deserialization of the application state, and it is integrated with BrowserHistoryManager.
You can download the latest release of GuggaFF and find the usage of this class in our sample applications - http://www.gugga.com/GuggaFlashFramework/
Thanks,
Branimir, Gugga Team
Post a Comment
Links to this post:
Create a Link
<< Home