ActionScript, Flash, and Flex
Posts tagged Design Patterns
The Power of Separated Logic: Opening the Door
Jun 9th
I have to give the Adobe Flash team big props on the new Spark framework’s focus on separated logic. Not only does it enhance my productivity and sanity, but it has opened wide the door for and encouraged the Flex community to create vast reusable code in ways that Flex 3 had problems encouraging developers to do.
For instance, the Spark separated layout structure with a DI slant has already saved time and encouraged so many layout concepts that are no longer component based solutions (example). The community is now able to focus on solving small solutions and then combining them into greater solution for a given project!
I expect to see more focused classes coming soon.
UPDATE (June 10, ’10):
The focus on solution driven instead of component has allowed Adobe to have features like Layout Mirroring (link). This further demonstrates the power of developing a DI driven framework. I encourage more of us to architect this way. OH MAN, did I really suggest us to architect like Adobe?!
Popularity: 6% [?]
Parsing Algorithms – Creational Patterns
Mar 27th
This is as much a recommendation as a rant, so pardon me while I do both
Situation
Open-source project or foundational classes for use within your organization that contains methods for parsing data.
Background
Often, I’ve run into issues where I am using an open-source library (public or private) and I need to add extra logic to a creational algorithm(s) and cannot or cannot easily do so. This is what has sparked me to write this post, because I am increasingly seeing these types of issues.
Recommendation
When creating algorithms that create objects from other objects (eg models and serialization) , please use offload the logic to accessible and easily modifiable methods. In Design Pattern terms, I’m requesting and ranting that the Factory Method Pattern, Strategy Pattern, or something similar be used. This only compounds in importance when there are several objects created in a single method! <– That right there is arguably bad practice in general for large projects.
Example
NOTE: Showing a simple example seems to not show the problem, but I’ll try. For a better example, please refer to “Real-Life Example” below.
Mapping XML properties to models within a project.
protected updateList():void
{
var header:Header = new Header;
header.title = _xml.header.@title;
header.lang = _xml.header.@lang;
header.colors = _xml.header.colors.toArray();
var body:Body = _body;
var child:XML;
for (var i:Number = 0; i < _xml.body.item.length(); ++i)
{
child = _xml.body.item[i];
body.getItemAt(i).text = child.@text;
}
}
Real-Life Example
NOTE: I do not mean to pick on or criticize the fine work done by the peeps working on the OpenVideoPlayer Project and MUST EMPHASIZE that the project is very useful and saves time. This merely serves as an easy example to show. Moreover, I’ve written my fair share of problematic code along these lines too.
Popularity: 11% [?]
Tween Library Preferences and Strategy Patterns
Jan 17th
Problem:
When it comes to Tween libraries, it’s either a project’s requirements and/or developer personal preferences that can conflict in selecting a library. Honestly, the library shouldn’t matter. The importance is that the objects are tweened. This is especially important when it comes to open-source projects, as the issue only compounds where it’s much more likely that both personal preferences and project requirements will both be the issue with the open-source project.
Solution: Use the Strategy Pattern
Example (real-life example): Duncan Reid created a ToolTip UI class for ActionScript3. The problem I ran into was that fl.transitions.Tween was being used and I was working on an AS3 application that is using the now Greensock’s TweenLite. The solution that Duncan and I discussed was to use a Strategy Pattern to decouple the Tween library from the ToolTip class itself and offload the reference onto implementation. In other words, by utilizing the Strategy Pattern, it allows the developer to decide which tweening library he/she would prefer to use. For Duncan Reid’s ToolTip UI I did the following:
- Added New Files:
- com.hybrid.ui.strategies.IToolTipTweenStrategy
- com.hybrid.ui.strategies.ToolTipTweenStrategy – The only place where fl.transitions.Tween is being used/referenced
- ToolTip now has a required constructor parameter tweenStrategy which will accept anything implementing the IToolTipTweenStrategy
This now means that the ToolTip UI class depends on one thing – the Tween Strategy. In fact, the strategy could be set to null (not given to the ToolTip), then no tweening would take place. Instead, the tooltip would just display or hide immediately. As such, the required construction param could be made optional (ie default set to null).
In action this simply looks like
new ToolTip(new com.hybrid.ui.strategies.ToolTipTweenStrategy)
//OR
new ToolTip(new com.novelastudios.ui.strategies.ToolTipTweenStrategy)
Source Files / Source Examples (using Duncan Reid’s ToolTip UI)
- Example of dynamic strategy changing ( visual demo / source )
- Click the Radio Buttons at the top of the visual demo to switch between the different strategies.
- Implementing it into Duncan’s original example
- Using fl.transitions.Tween ( visual demo / source .fla | .as )
- Using com.greensocks.TweenLite ( visual demo / source .fla | .as )
Note
The above source is not officially supported / maintained by Duncan Reid and as such I recommend that this simply be used as an example and refer you to his site and source code for production use.
Additional Information
For more information on the Strategy Pattern with ActionScript3 checkout the following:
Popularity: 100% [?]
Most Recent Comments