Creating object factories
Object factories are a useful design pattern used in all sorts of areas in programming. In game development specifically, a factory might be used to spawn enemy objects, spawn bullet objects, particle effects, item objects, and much more. In fact, AndEngine even uses the factory pattern when we create sounds, music, textures, and fonts, among other things. In this recipe, we'll find out how we can create an object factory and discuss how we can use them to provide simplicity in object creation within our own projects.
Getting ready
Please refer to the class named ObjectFactory
in the code bundle.
How to do it…
In this recipe, we're using the ObjectFactory
class as a way for us to easily create and return subtypes of the BaseObject
class. However, in a real-world project, the factory would not normally contain inner classes.
Before we create our object factory, we should create our base class as well as at least a couple subtypes extending the base class:
public static class BaseObject { /* The mX and mY variables have no real purpose in this recipe, however in * a real factory class, member variables might be used to define position, * color, scale, and more, of a sprite or other entity. */ private int mX; private int mY; // BaseObject constructor, all subtypes should define an mX and mY value on creation BaseObject(final int pX, final int pY){ this.mX = pX; this.mY = pY; } }
Once we've got a base class with any number of subtypes, we can now start to consider implementing the factory design pattern. The
ObjectFactory
class contains the methods which will handle creating and returning objects of typesLargeObject
andSmallObject
in this case:public class ObjectFactory { // Return a new LargeObject with the defined 'x' and 'y' member variables. public static LargeObject createLargeObject(final int pX, final int pY){ return new LargeObject(pX, pY); } // Return a new SmallObject with the defined 'x' and 'y' member variables. public static SmallObject createSmallObject(final int pX, final int pY){ return new SmallObject(pX, pY); } }
How it works…
In the first step of this recipe, we are creating a BaseObject
class. This class includes two member variables called mX
and mY
, which we can imagine would define the position on the device's display if we are dealing with AndEngine entities. Once we've got our base class set up, we can start creating subtypes of the base class. The BaseObject
class in this recipe has two inner classes which extend it, one named LargeObject
and the other, SmallObject
. The object factory's job is to determine which subtype of the base class that we need to create, as well as define the object's properties, or mX
and mY
member variables in this instance.
In the second step, we are taking a look at the ObjectFactory
code. This class should contain any and all variations for object creation relating to the specific object-types that the factory deals with. In this case, the two separate objects simply require an mX
and mY
variable to be defined. In a real-world situation, we may find it helpful to create a SpriteFactory
class. This class might contain a few different methods for creating ordinary sprites, button sprites, or tiled sprites, via SpriteFactory.createSprite()
, SpriteFactory.createButtonSprite()
, and SpriteFactory.createTiledSprite()
. On top of that, each of these methods would probably require parameters that define the position, scale, texture region, color, and more. The most important aspect to this class is that its methods return a new subtype of an object as this is the whole purpose behind the factory class.