Home Tutorials Forums Articles Blogs Movies Library Employment Press Buy templates

Go Back   ActionScript.org Forums > General > Best Practices

Reply
 
Thread Tools Rate Thread Display Modes
Old 08-13-2007, 09:12 PM   #1
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default Factory Design Pattern

Does anybody actually use Factory Design Pattern?

Seems like a load of whoey, or O'Reilly's team isn't very good at explaining anything within a practical application.
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman
Flash Gordon is offline   Reply With Quote
Old 08-13-2007, 09:21 PM   #2
senocular
six eyes
 
senocular's Avatar
 
Join Date: Jan 2003
Location: San Francisco, CA (USA)
Posts: 7,758
Send a message via ICQ to senocular Send a message via AIM to senocular Send a message via MSN to senocular Send a message via Yahoo to senocular
Default

I do. My Mii Editor ( http://www.miieditor.com ), for example, uses a factory to create instances to manage the loading and saving of files. A factory is used because depending on whether or not the application is being run as an AIR app or from a web page, different classes are used to handle the load and save operations. Its the factory's responsibility to determine which instance is needed and returns that to the main application.
__________________
(6)
senocular is offline   Reply With Quote
Old 08-13-2007, 09:25 PM   #3
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default

Okay. thanks for the response. I wont rule it out then.

Do you have a suggestion for a small application that would/could/should use this pattern that I can try to build so I can post it up for criticism?
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman
Flash Gordon is offline   Reply With Quote
Old 08-13-2007, 09:29 PM   #4
senocular
six eyes
 
senocular's Avatar
 
Join Date: Jan 2003
Location: San Francisco, CA (USA)
Posts: 7,758
Send a message via ICQ to senocular Send a message via AIM to senocular Send a message via MSN to senocular Send a message via Yahoo to senocular
Default

As in an excuse to write something that uses it?
__________________
(6)
senocular is offline   Reply With Quote
Old 08-13-2007, 10:03 PM   #5
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default

Yea, i can't seem to wrap my head around its usefulness or what would I need to use it for or when.
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman
Flash Gordon is offline   Reply With Quote
Old 08-13-2007, 10:11 PM   #6
senocular
six eyes
 
senocular's Avatar
 
Join Date: Jan 2003
Location: San Francisco, CA (USA)
Posts: 7,758
Send a message via ICQ to senocular Send a message via AIM to senocular Send a message via MSN to senocular Send a message via Yahoo to senocular
Default

Basically its when you have a need for one or more objects with you having a collection of many objects that facilitate the need but given your current circumstances should only be using one type of those objects.

For example, you need to display a set of OS components on the screen but, as is the case with Flash, your environment does not support native OS widgets. So what you have done is recreated each supported OSs' widgets for your application from scratch. However you will only want to display your Mac OS widget components when playing on the Mac and Windows OS widget components when on Win. To do that you would have two factories, a WinComponent factory and a MacComponent factory each of which create the same widget components with the same functionality but does so in 2 different visual (and behavioral) styles. Which factory to use will be determined at startup when the OS is detected.

ActionScript Code:
var componentFactory = (OS == "Mac") ? MacComponentFactory : WinComponentFactory; addChild(componentFactory.newButton()); ...
__________________
(6)
senocular is offline   Reply With Quote
Old 08-13-2007, 10:21 PM   #7
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default

Ok, I think I understand it a little. So an other example might be:

A users enters their birthday. Based upon their age, their avatar character is either a baby, teen, young adult, or old fogy. All of these characters would extend a basic "Character" abstract class and therefore share some of the same method signatures.

The point of a Factory Pattern is to loosen the coupling between the client and the class, correct? When using a Factory pattern adding additional stuff later on should be easier and require no changes to current code, but adding more code?
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman
Flash Gordon is offline   Reply With Quote
Old 08-13-2007, 10:23 PM   #8
senocular
six eyes
 
senocular's Avatar
 
Join Date: Jan 2003
Location: San Francisco, CA (USA)
Posts: 7,758
Send a message via ICQ to senocular Send a message via AIM to senocular Send a message via MSN to senocular Send a message via Yahoo to senocular
Default

I think you're getting it : )
__________________
(6)
senocular is offline   Reply With Quote
Old 08-13-2007, 10:25 PM   #9
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default

cool. Let me see if I can find a way to put this to use and post up an example in a day or two.

Thanks senocular for the help. The concepts of OO had be kind of hard to wrap my head around as they are never a direct line of sight...always the long way around.
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman
Flash Gordon is offline   Reply With Quote
Old 08-14-2007, 01:10 PM   #10
hangalot
lala
 
hangalot's Avatar
 
Join Date: Feb 2002
Location: on the road
Posts: 2,859
Default

senocular's example is what you call an abstract factory. this is where you create a class according to your needs that has the same methods as an interface which all these class you can create implements, and this class gives you the specific feedback you desire.

this is prob the least used type of factory. a different type of factory is the factory method.

lets say for example i havce a piece of xml that defines which controls i want to display, the my factory method will just be a switch that returns the correct type of control from some static function.

two different factory types for different levels of complexity.

abstract factories are especially used for tokanizers and parsers, while a factory methods occur all over most peoples code without them knowing what it is.
wheneevr you see a method
ActionScript Code:
public function getThing(v:Number):ISomething { switch(v) { case 0: return new LittleThing() as ISomething; break; case 1: return new BigThing() as ISomething; break } }

thats a factory (method)!
__________________
oi poloi
http://www.memorphic.com/news/
hangalot is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Flash navigation: Design Pattern? TimUK ActionScript 2.0 3 03-21-2007 12:42 AM
A Design Pattern Question, Singleton LostInRecursion ActionScript 2.0 8 02-19-2007 05:11 AM
Double-check locking design pattern ccapndave ActionScript 2.0 0 11-01-2004 02:27 PM
Composite design pattern maglez ActionScript 2.0 0 07-19-2004 10:03 AM
design pattern for your pleasure geak ActionScript 1.0 (and below) 2 07-24-2002 10:00 AM


All times are GMT. The time now is 02:21 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2009 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.