Programming Architecture
Parsing Algorithms – Creational Patterns
0This 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: 10% [?]
Tween Library Preferences and Strategy Patterns
0Problem:
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% [?]
The Genius of Programming
0This post is simply a quote from my brother-in-law Forrest Kyle, which he kindly spent time writing out what he said to me on the phone while we discussed architectural practices. I wanted to share this, because I believe it is so well put.
In programming, genius rarely manifests itself in some radical new idea, or some blazing new algorithm that redefines the parameters of what is possible. Often, genius is the visible result of a forceful and determined intensity of focus placed upon mundane details, and making sure software is fundamentally correct. A good sign of a genius programmer is one who is willing to work closely with details that are “beneath him” if it serves the greater good of program efficiency and correctness.
Don’t allow yourself to make a design decision for which you can’t articulate a verbal defense. Imagine yourself explaining why you made your design choices to a panel of super geniuses. If you find yourself at a loss for words, it is likely that future users and developers of your program will find themselves at a loss for patience.
A source file is more than just a set of instructions for your computer; it is a formal engineering schematic and must be crafted in such a way that communication with other developers is as deeply ingrained in the presentation as the function of the program itself. You are not writing source files for yourself, you are writing them for current and future coworkers.
Programmers who don’t feel the need to write clear, helpful documentation are like cats that don’t feel the need to use the litter box.
-Forrest Kyle
In closing, this really is not just a great explanation of solid programming, but of communication and thought out action.
Popularity: 13% [?]