Classic office conversation
(1:41:25 PM) adam…_ra: sorry.. at the hospital right now
(1:41:27 PM) adam…_ra: gf had a surgery
(1:43:37 PM) adam…_ra: quick question
(1:43:52 PM) adam…_ra: do you know how to pass multiple sys vars to a bamboo build?
(1:41:25 PM) adam…_ra: sorry.. at the hospital right now
(1:41:27 PM) adam…_ra: gf had a surgery
(1:43:37 PM) adam…_ra: quick question
(1:43:52 PM) adam…_ra: do you know how to pass multiple sys vars to a bamboo build?
One of the major problems with building your generic Singleton is that you cannot extend it. Once you’ve locked yourself into a particular implementation you cannot create a new version since all the old code is referencing the old version, in the old package structure, with a specific name. You cannot swap out singletons. Or can you?
The way Adobe built the Singleton class and PopUpManager, PopUpManagerImpl, IPopUpManager, etc. used to bother me a lot, because it didn’t make any sense and the purpose of having all those classes – when they could have used a standard singleton pattern – was never explained.
Well I finally got it, and it does make sense and I’m tempted to tell all of our Flex developers working on our framework to begin using the pattern established by the Flex mx.core.Singleton Class.
The Old Way
The standard Singleton approach is to do something like this:
package
{
public class MyStandardSingleton
{
public function MyStandardSingleton()
{
if (_instance)
{
throw new Error("Singleton violation.");
}
}
private static var _instance : MyStandardSingleton;
public function getInstance() : MyStandardSingleton
{
if (!_instance)
{
_instance = new MyStandardSingleton();
}
return _instance;
}
}
}
The problem with this approach is that one cannot ever create a MyStandardSingleton2 and move all the old code to using it without, well, updating all the old code and every single reference. That’s not good. Especially if you have lots of layered libraries using that Singleton.
The Right Way
The solution is found by using the approach provided by Adobe:
1) Create an interface representing the functionality of your Singleton (e.g. IMySingleton)
2) Create an implementation of that interface (e.g. MySingletonImpl implements IMySingleton)
3) Register your implementation with mx.core.Singleton, using the interface class name (with package) as the name:
Singleton.registerClass("my.package.IMySingleton", MySingletonImpl);
4) Create a class that doesn’t implement the interface (e.g. MySingleton) but has the same methods as public static. Then in each of those methods, call Singleton.getInstance(…) and ask for the string name you provided in (3), cast as the interface from step (1), and call the same method:
public static function myMethod(param1:DisplayObject):Object
{
return (Singleton.getInstance("my.package.IMyInterface") as IMyInterface).myMethod(param1);
}
Conclusion
Now, if you ever want to create a new implementation of IMySingleton, just make sure you register it with mx.core.Singleton before the old implementation and all your code will just work.
So what would you do if you wanted to create your own IPopUpManager to override the functionality of the Flex PopUpManager? Simply register it with Singleton under the name “mx.managers::IPopUpManager” before the Flex framework does.
Unfortunately, I’m too busy to figure all that out right now, so maybe someone else can enlighten me as to the timing.
- Josh
10 Commandments of Programming
10 Doctrines of Programming
“And thirdly, the code is more what you’d call “guidelines” than actual rules.” – Barbossa
- Josh
This is my official release of the Graph Control suite which I am now developing on Google code here:
http://code.google.com/p/flex-graph-control/
What is the Flex Graph Control?
The Flex Graph Control aims to be the opposite of a charting framework. It is to be a small suite of classes and components designed to allow a user to select values along two axis.
How can I use it?
I’m designing the Flex Graph Control to allow you to easily build items like the following:
The end goal is to provide developers with a suite of tools to allow them to specify things like minimum and maximum values, axis options including resolution and whether to snap to pixels, etc. and a suite to allow an infinite variation of custom layered renderers (background, tick-mark renderers, custom overlays, and drag-and-drop controls that automatically update the selected values in the base control).
What is in the Repo?
There are two projects checked into the repository. The first is the library swc for the graph control suite. The second is a Flex Builder 3 application demoing the latest in features (I am using this to test everything).
How do I use it?
At this point, the DateSlider control in the repo is an example of how to build on top of the core GraphControl object.
- Josh
Here is a little utility I built to easily retrieve the Class for a given String definition or instantiated object:
—-
package
{
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
/**
* Wraps getDefinitionByName and simply returns null if the class cannot be found.
*
* If you pass in a non-String object, will return the class associated with that object.
*
* @param definitionNameOrObject A definition name (e.g. "com.myDomain.MyClass") or an actual object,
* like 'this'.
*/
public function getClass(definitionNameOrObject : *) : Class
{
var clazz : Class;
if (definitionNameOrObject is String)
{
try
{
clazz = getDefinitionByName(definitionNameOrObject as String) as Class;
return clazz;
}
catch (error : Error)
{
return null;
}
}
else if (definitionNameOrObject is Object)
{
var className : String = getQualifiedClassName(definitionNameOrObject);
clazz = getDefinitionByName(className) as Class;
return clazz;
}
return null;
}
}
Note: Make sure the file is exactly named “getClass.as” or else the global function will not work.
- Josh
Here they are. Enjoy with a side of java for best results. You can probably build a 250-300 line asdoc ready Flex component with these in about 30 minutes:
Flex Actionscript Code Snippets
Instructions
1: Extract the xml files from the zip file inside the doc file
2: Open the view titled “Snippets” in Eclipse (I believe this will only work in the Flex / Flash Builder plugin – not the stand alone)
3: Right click in the snippets view
4: Select “Customize…”
5: Import each xml file (you may need to add an item / category before the “Import” button is clickable)
6: Double-click on a snippet to use it.
The most important snippets are the property snippets. Try ‘em out!
I had to wrap the zip file in a Word Document in order to get it to act nicely with WordPress.
- Josh