Skip to content
September 26, 2008 / Shrikant Patil

Simple Factory Design Pattern

The Factory Design pattern is used to create a loose coupling between the Client object and the Product objects by delegating the object creation to a method called a factory; hence the name Factory Method.

In ActionScript we use new keyword, when we want to create an instance of a object from a concrete classes. For Example;


package flexScript
{
     Public class AudioPlayer
    {
        Public function PlaySong(){
            Var song:Object = new EnglishSong();
        }
    }

}

Here class AudioPlayer creates a new instance of EnglishSong object. AudioPlayer class is known as ‘client’ and EnglishSong class is known as ‘Product’ class. The model creates a dependency between AudioPlayer and EnglishSong classes. In future any changes in EnglishSong class also need to update in AudioPlayer class as it coupled with EnglishSong class.
To sort out this problem of tight coupling between client and product, we need to use factory pattern. The factory pattern creates intermediate called ‘creator’ between client (User) and product (Account) classes. The ‘creator’ returns one of product of several possible products depending on the data passed by client class. This is done by a method called ‘factory’ method of the creator. The factory method instantiate the objects and return them to client. Many of client objects can use ‘factory’ method of a ‘creator’ to get desired ‘products’.
Here ‘Client’ is base class and ‘ClientA’ and ‘ClientB’ are inherited from ‘Client’. The ‘Factory’ class decides which of subclasses is to create and return depending on the arguments passed to it as ‘ab’. The ‘Factory’ class has a method called ‘getProduct’ which accepts parameters ‘ab’ and depending on those parameters value, method creates subclasses and returns instance of class ‘Client’. The decision which class is to create and return is totally up to ‘Factory’ class. The above Patterns is what know as “Simple Factory”.
Here is a simple factory class which creates and return different language song object depending on the parameter passes to it.


package flexScript
{
     Public class SimpleFactory
    {
        private var returnSong:Song;

        Public function getSong(language:String):Song{
            switch(language){
                case "English": returnSong = new EnglishSong();
                                return returnSong;
                                break;
                case "Spanish": returnSong = new SpanishSong();
                                return returnSong;
                                break;
                case "Chines":  returnSong = new ChinesSong();
                                return returnSong;
                                break;
            }
        }
    }

}

Advertisements

4 Comments

Leave a Comment
  1. Stephen Stulov / Sep 26 2008 1:00 pm

    Pretty common practice is to implement IFactory interface. The only method it has is like getInstance() …

  2. Jason / Mar 9 2009 9:08 pm

    This is a good example of a “Simple Factory” but it’s worth mentioning that a simple factory is not really considered a factory pattern and that the two are commonly confused. Your example here is almost identical to one described in Advanced ActionScript 3 With Design Patterns page 80 – http://books.google.com/books?id=DT7INNE3ui8C&printsec=frontcover&hl=en&ct=result#PPA80,M1

    The simple factory is a decent choice when there are a small number of options that will never change. For something like languages or audio formats, though, it might be better to use a real factory pattern instead so that new formats and languages can be added without re-writing.

    • brooklyn / Feb 20 2010 12:59 am

      The only nice statement in your comment is the link you provided.

  3. andres / May 8 2009 12:52 am

    Hi, very util explication of Simple Factory and example in AS3.

    But you can use getqualifiedclassname for create a Simple Factory class base. In my blog (http://www.3wstudio.com.ar/simple-factory-en-as3-278.html) I generate this class. Check and comment if you want.

    PD: Sorry for my english.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: