Factories

Started by
3 comments, last by TheRealBartman 20 years, 12 months ago
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
Michael BartmanLead ProgrammerDark Omen Studios
Advertisement
Kind of, the factory object is usually seperate from the object you are creating.


  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
"There will come a time when you believe everything is finished. That will be the beginning." -Louis L'Amour
Hey,

Thanks a ton AlanKemp, now I understand it much better. Now I need to go read the section on singletons

Later,


Michael Bartman
Michael BartmanLead ProgrammerDark Omen Studios
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.

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]
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Ahhh,

Alright, this is making much more sense here than it does in other places I have read.

Thankyou for all of your help!

Michael Bartman
Michael BartmanLead ProgrammerDark Omen Studios

This topic is closed to new replies.

Advertisement