Creating a component and Object Factory

Now that we have built a stage factory, building a component factory should be easy. Let's take a look at what a component and object factory would look like:

//Component Factory 
class M5ComponentFactory
{
public:
~M5ComponentFactory(void);
void AddBuilder(M5ComponentTypes type,
M5ComponentBuilder* builder);
void RemoveBuilder(M5ComponentTypes type);
M5Component* Build(M5ComponentTypes type);
void ClearBuilders(void);
private:
typedef std::unordered_map<M5ComponentTypes,
M5ComponentBuilder*> BuilderMap;
typedef BuilderMap::iterator MapItor;
BuilderMap m_builderMap;
};

//Object Factory
class M5ObjectFactory
{
public:
~ M5ObjectFactory (void);
void AddBuilder(M5ArcheTypes type,
M5ObjectBuilder* builder);
void RemoveBuilder(M5ArcheTypes type);
M5Object* Build(M5ArcheTypes type);
void ClearBuilders(void);
private:
typedef std::unordered_map< M5ArcheTypes,
M5ObjectBuilder *> BuilderMap;
typedef BuilderMap::iterator MapItor;
BuilderMap m_builderMap;
};

Looking at those classes, you will notice that they are almost identical to the M5StageFactory class. The only things that are different are the types involved. Instead of M5StageTypes, we use M5ComponentTypes or M5ArcheTypes. Instead of M5StageBuilder, we use M5ComponentBuilder or M5ObjectBuilder. Finally, instead of the Build method returning an M5Stage*, we return either an M5Component* or M5Object*.

If we were to implement these classes, the code would be identical as well. You might think that the M5ObjectFactory would be a little different since the M5Object isn't part of an inheritance hierarchy, but it actually doesn't matter. Even though the derived class builders are all doing different work, they are always just returning a single pointer type. The builders might be different, but the return type isn't.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.93.20