Customizable Navigation Bar

Monday, March 28, 2011

The Factory Design Pattern

Hey guys, to hold you off I am going to do a small article on the factory design pattern. It makes instantiating multiple objects very easy and painless so it is definitely something that you want to keep in mind while you are coding away.

The easiest way for you to see what the factory does is for me to just show it to you. I am going to mostly code in C#, but as long as you can grasp the concept, it shouldn't matter what language this is in.

What we need to do is create a base class that all of the "manufactured" classes will extend from. In most cases this is already done for you:
        In flash this could be your Sprite class
        In C# or UnrealScript this could be your object class
        Unity has the GameObject class



You get the point.
      
so:

/*This is our base class that all of our "manufactured" classes will extend from*/
 public abstract class GameObject
{
          public GameObject()
        {
        }
}

Now lets make three classes that extend this base class. These will be our "manufactured" classes, since these will be the classes that are returned/instantiated by the factory class.

public class CoolObject: GameObject
{
        public CoolObject()
        {
                Console.WriteLine("I am the coolest object ever");
        }
}

public class AwesomeObject:GameObject
{
        public AwesomeObject()
        {
                Console.WriteLine("I am the most awesomest object ever. That's right, I used the word awesomest");
        }
}

public class TotallyRadObjectDude:GameObject
{
        public TotallyRadObjectDude()
        {
                Console.WriteLine("This object is totally rad dudes and dudettes.");
        }
}

Now that we have the three classes that will be manufactured in the factory class, we need to make the actual factory class.

One of the easier and more logical ways to do this is to create an enumerator that will be passed into the spawn function. Some programming languages do not support enumerators though(ActionScript! I'm looking at you!) so you'll need to either create your own enumerator class, or have public static constants.

public enum ObjectType{CoolObject = 0, AwesomeObject, TotallyRadObjectDude};

so now that we have this set up the class is extremely easy to set up. The most basic factory class has a constructor and a spawn function, and it looks something like this:

public class SweetObjectFactory
{
        public SweetObjectFactory()
        {
        }

        /*An extremely basic spawning function*/
        public GameObject spawnObject(ObjectType spawnedObjectType)
        {
                switch(spawnedObjectType)
                {

                        /*We simply just return a new object based on what is passed into the function. Since these all extend GameObject, and we return a GameObject, we can do this.*/

                case ObjectType.CoolObject :
                        return new CoolObject();
                        break;

                case ObjectType.AwesomeObject :
                        return new AwesomeObject();
                        break;
                case ObjectType.TotallyRadObjectDude:
                        return new TotallyRadObjectDude();
                        break;
                default:
                        break;


                }
        }
}

That's basically it for the factory class. Just a simple spawning class that takes in what you want to spawn. Now realistically, you are going to want to add more than what I have put here.

A spawning function that only takes in a type or enum is not really practical or overly useful. What most spawning functions need passed is the type, along with a position, a rotation and possibly a number of objects to be spawned and the time between each spawn. These 4 parameters add a ton of flexibility that this very basic factory class does not have.

If you have any questions on this or any other design patterns leave a comment and I'll give you a hand, and may even write an article to explain in detail what you are wanting to know.

Follow me on twitter or subscribe to my youtube channel, where you will find all of my posted videos.

Have a good one guys, and keep coding!