In Part 4 you saw the full Service to Worker pattern demonstrated. However, the method discussed in the last tutorial doesn't fit every situation. In this tutorial you will learn a few "best-practices" for Cairngorm projects as well as an extension to the Service to Worker pattern for more complex cases.
Delegates, Commands, and Responders
These classes (Delegates, Commands, and Responders) have specific responsibilities. Many developers run into problems when they do not adhere to these responsibilities. The following tips are guidelines for your code.

Figure 1 - Data Transmission in the Service to Worker Pattern
The goal of this separation of responsibility is that services should be able to be switched out with only a change in the delegate. This may sound a bit unimportant, but if you are working for a large company, the server-side implementation can change drastically from development to production. For this to work properly, the service call will need to have two responders: the Delegate, and the actual Responder. Figure 1 illustrates this separation. The data passed from the service to the delegate can be in virtually any format, but the transfer from the Delegate to the Responder needs to only be application data (VO's, model classes).
In this case, the Delegate now has an added responsibility, parsing the raw data. To accomplish this, you can create a factory class to parse the data. In the sample below the class DeliciousFactory (because it parses data from a Del.icio.us feed) has a method buildPostArrayFromJSON which handles this responsibility.
Code Example 1 - A Delegate Parsing Raw Server Data
Parsing the server data is only a part of this extended design pattern. For this to work properly with the traditional Cairngorm flow, the delegate must "intercept" the result of the service call. To accomplish this, you will attach an AsyncToken [Reference] to the service call, create a responder for the call, and then attach the responder to the AsyncToken. This is illustrated in lines 23-30 of Code Example 1.
Now that the delegate has "intercepted" the result of the service call, it need to have methods to parse the data. When you created the responder inside of the delegate (line 27 of Code Example 1), you passed in a result and fault method. In this case it was onResult and onFault. You now need to add these methods to the delegate.
To pass the parsed data to the original responder, you will need to create a new instance of the ResultEvent [Reference] with the parsed data in the result property (line 40 of Code Example 1). This event will be passed as an argument to the original responder's result method.
Setting ModelLocator Values
Within a Cairngorm application, it can be extremely tempting to set values of ModelLocator variables in your events, delegates, or views, but this should be avoided. These values should be set only in the Command Level (see Figure 1) of the application. This means that you can set these values in a Command or Responder. This keeps every class in tune with its true function.
Sequence Commands
Cairngorm also contains a class, SequenceCommand [Reference] that allows the developer to chain command/events together for sequential execution. Initially this may seems like it breaks the framework's rule oft having every command triggered by an event, but it doesn't. Inside of a SequenceCommand you define the nextEvent property (which is an instance of a CairngormEvent). This event is executed when the executeNextCommand method is called. This is the most effective way to attach multiple commands to a single user gesture.
NOTE: The SequenceCommand class assumes that you are not separating your Command and Responder. You could easily extend the architecture and create a SequenceResponder class to achieve the same effect.
ViewHelper and ViewLocator Classes
Many people have inquired about the ViewHelper [Reference] and ViewLocator [Reference] classes. Both of these classes are now deprecated as of Cairngorm 2.1, and it is considered bad practice to use either of these classes in a new Cairngorm project. Should you be working with an existing project that still uses these classes, I would recommend refactoring your code to meet Cairngorm 2.2 standards.
Summary of Tips
Application Code
Beginning Code Download (9 Kb)
Finished Code Download (12 Kb)
References
AS3CoreLib
Looking Ahead
In the next (and final) tutorial on Cairngorm, you will learn tips to make your Cairngorm development go faster as well as learning more "best practices" for Cairngorm development.
HI David,
Thank you so much for this series of tutorial. This has helped me a lot in getting my journey started to the world of flex and CF.
Eagerly waiting for Part-6.
Appreciate your efforts.
Regards,
~Sudhir
HI David,
Thank you so much for this series of tutorial.
i am expecting MVC pattern in next video i.e part 6.
Appreciate your efforts.
Thank You.
Expecting further clarification at Part6
Good tutorials
[...] 原文:http://www.davidtucker.net/2007/11/30/getting-started-with-cairngorm-%E2%80%93-part-5/ [...]
[...] http://www.davidtucker.net/2007/11/30/getting-started-with-cairngorm-%E2%80%93-part-5/ [...]
Hi David,
Thank you very much for a nice tutorial.
I have question. If i must attach 4 commands to a single user gesture. I need in every command extends SequenceCommand ? And it will looks like this
Command 1 -> dispatch Command 2
Command 2 -> dispatch Command 3
Command 3 -> dispatch Command 4
is there any posibble to dispach all commands in one execute ?
This site called Unblock Websites also has some decent info on the subject.
I am new to framework and this is my first framework i am learning on myself with help of my mentor,i was wandering how to start and to understand the things i really thank full to DavidTucker.net for providing this tutorials
You’re the first site I always go to when I surf the net, its like a habit
Awesome help. 2 hours and I know I have learnt something new today ……