Revision: 23152
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at January 31, 2010 14:41 by drukepple
Initial Code
package { import flash.display.*; public class NamespaceStrategy extends Sprite { private var currentMode:Namespace; private namespace a; private namespace b; public function NamespaceStrategy() { this.mode = "a"; doSomething(); this.mode = "b"; doSomething(); } public function doSomething():void { currentMode::_doSomething(); } a function _doSomething():void { trace("A"); } b function _doSomething():void { trace("B"); } public function set mode(m:String):void { switch (m) { case "a": currentMode = a; break; case "b": currentMode = b; break; } } } }
Initial URL
Initial Description
I sometimes use namespaces to implements a sort of "mini strategy pattern." This starts to pay off when you have more than one method that needs to do different things depending on the "mode" (or strategy). You have your "which mode am I on?" logic in one place (`set mode`) and then each mode-dependent method simply utilizes the `currentMode::someMethod()` technique. Not as fancy, robust, or strict as a proper strategy pattern with interfaces and strategy objects, but if you want to keep things sort of self-contained, have a small amount of strategies to consider, or don't want the end user to have to worry about creating the strategy objects, then I've found this useful.
Initial Title
"Mini Strategy" pattern using namespaces
Initial Tags
Initial Language
ActionScript 3