Factories
Hey, I''ve been hearing a lot off and on about using factories in game programming. For example, a rendering factory or an AI factory.
I happen to have a book on object oriented programming by addison wesley. Inside of it, they talk about abstract factories and factories. I''ve been trying to learn about them, but I am a little confused.
Would this be an example of code using a rendering factory?
Device *a = new Device;
a = a->CreateDevice(DIRECTX9);
Using a factory you could also do this:
a = a->CreateDevice(OPENGL);
Couldn''t you?
I guess I just need a little boost to help me understand how to use a factory in game programming.
Thanks,
Michael Bartman
Kind of, the factory object is usually seperate from the object you are creating.
Usually I would make the factory class a singleton (another design pattern).
Alan
class Renderer{};class DirectXRenderer : Renderer{};class OpenGLRenderer : Renderer{};class RendererFactory{public: Renderer * CreateRenderer(std::string RendererName) { if (RendererName == "DirectX") { return (Renderer *)new DirectXRenderer; } else if (RendererName == "OpenGL") { return (Renderer *)new OpenGLRenderer; } return NULL; }};// Create a new rendererRendererFactory MyFactory;Renderer * MyRenderer;MyRenderer = MyFactory.CreateRenderer("DirectX");
Usually I would make the factory class a singleton (another design pattern).
Alan
Hey,
Thanks a ton AlanKemp, now I understand it much better. Now I need to go read the section on singletons
Later,
Michael Bartman
Thanks a ton AlanKemp, now I understand it much better. Now I need to go read the section on singletons
Later,
Michael Bartman
Abstract factory adds another layer ontop of the factory method.
It's essentialy making a factory using the factory method. e.g. given the user's difficulty selection, we create a different factory that produces our monsters. This makes it easy to keep the monsters power consistent given the difficulty level.
[edited by - Magmai Kai Holmlor on April 20, 2003 1:54:53 AM]
It's essentialy making a factory using the factory method. e.g. given the user's difficulty selection, we create a different factory that produces our monsters. This makes it easy to keep the monsters power consistent given the difficulty level.
IMonsterFactory{IMonster* makeBadBuy1()=0;//...}; donthurtme_factory : IMonsterFactorynightmare_factory : IMonsterFactory IMonsterFactory* monster_factory=0;monster_factory = create_monster_factory(difficulty_setting); IMonsterFactory* create_monster_factory(int diff){switch(diff) { case 0: return new donthurtme_factory; //... case 5: return new nightmare_factory; default: assert(0); return new nightmare_factory; }}
[edited by - Magmai Kai Holmlor on April 20, 2003 1:54:53 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement