Web Design Resources & Tutorials to help you design the best website!
Here is a small example using the ClassFactory class, basically the whole idea is you
can dynamically set the properties in the ClassFactory. If you created a Square class that was hard coded to only display white then you would have to extend it and override methods.
package { import flash.display.Graphics; import mx.core.UIComponent; public class Square extends UIComponent { public var color:uint=0xFFFFFF; public function Square() { super(); } protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void { super.updateDisplayList(unscaledWidth,unscaledHeight); var g:Graphics = graphics; g.clear(); g.beginFill(color); g.drawRect(0,0,100,100); g.endFill(); } } }
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initApp()" layout="horizontal"> <mx:Script> <![CDATA[ private var myClassFactory:ClassFactory = new ClassFactory(Square); private function initApp():void { var whiteSquare:Square = myClassFactory.newInstance(); myClassFactory.properties={color:0x000000}; var blackSquare:Square = myClassFactory.newInstance(); myClassFactory.properties={color:0xFF0000}; var redSquare:Square = myClassFactory.newInstance(); addChild(whiteSquare); addChild(blackSquare); addChild(redSquare); } ]]> </mx:Script> </mx:Application>

9 Responses to Example of using the ClassFactory Class
notgettingit
June 5th, 2008 at 3:38 pm
I dont see the benefit really ..
Andrew Traviss
June 5th, 2008 at 5:35 pm
Mainly I think the benefit is that it allows you to pre-configure properties for an object that will be created by another component, without having to pass all of the styling information on to that component. You just hand it a factory to make instances with. It makes the interface dead simple, and avoids the pain of having to change the signatures of a bunch of methods every time you add a new property.
I could see the technique being particularly useful for, say, a particle system, aside from what Flex already uses it for.
小小菜鸟
June 12th, 2008 at 3:28 am
when i want to change same property of a class, but it is a private property ,can i use it?
小小菜鸟
June 12th, 2008 at 3:35 am
i need a more Complex example~
thanks u for you examples~
Sherif
June 12th, 2008 at 10:05 am
No You Can not, A private Property is private, it needs to be public.
N/A
June 24th, 2008 at 8:18 am
How is this working without implementing IFactory?
Flug Los Angeles
August 26th, 2008 at 6:02 am
I don´t think it will work without IFactory.
But is it possible to change the Property form private to public?
kredit move
November 5th, 2008 at 6:09 am
Its Tricky!
Anonimous
July 26th, 2010 at 1:39 pm
Why don’t you put the application running so we can see it working?