Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

TheRealBartman

Factories

This topic is 5541 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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 renderer

RendererFactory MyFactory;
Renderer * MyRenderer;

MyRenderer = MyFactory.CreateRenderer("DirectX");


Usually I would make the factory class a singleton (another design pattern).

Alan

Share this post


Link to post
Share on other sites
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 : IMonsterFactory
nightmare_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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!