Example of using the ClassFactory Class

5 Jun
2008
Delicious

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>
Incoming Search Terms :flex classfactory , classfactory flex , flex classfactory example , classfactory in flex , flex class factory , flex classfactory properties , flex ifactory example , classfactory properties , flex 3 classfactory , classfactory flex 3 Incoming Search Terms :flex classfactory , classfactory flex , flex classfactory example , classfactory in flex , flex class factory , flex classfactory properties , flex ifactory example , classfactory properties , flex 3 classfactory , classfactory flex 3

Share this story with a friend !
Join SherifAbdou's Rss Feed Stumble This Article! Add this to your Delicious Bookmarks! Digg this story ! Add this story to Reddit ! Tweet This Brush


9 Responses to Example of using the ClassFactory Class

Avatar

notgettingitNo Gravatar

June 5th, 2008 at 3:38 pm

I dont see the benefit really ..

Avatar

Andrew TravissNo Gravatar

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.

Avatar

小小菜鸟No Gravatar

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?

Avatar

小小菜鸟No Gravatar

June 12th, 2008 at 3:35 am

i need a more Complex example~

thanks u for you examples~

Avatar

SherifNo Gravatar

June 12th, 2008 at 10:05 am

No You Can not, A private Property is private, it needs to be public.

Avatar

N/ANo Gravatar

June 24th, 2008 at 8:18 am

How is this working without implementing IFactory?

Avatar

Flug Los AngelesNo Gravatar

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?

Avatar

kredit moveNo Gravatar

November 5th, 2008 at 6:09 am

Its Tricky!

Avatar

AnonimousNo Gravatar

July 26th, 2010 at 1:39 pm

Why don’t you put the application running so we can see it working?

Comment Form