Tween Library Preferences and Strategy Patterns
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% [?]