• Advertisement


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

Examples of common Design Patterns in games

This topic is 6229 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

I was wondering if people would like to share their usage of Design Patterns in games they have worked on. Looking at the Design Patterns book itself, I can see how they are all useful, but have found that I don''t actually need most of them. But this may just be down to my failure to see the best way of implementing certain features. Therefore I thought it might be handy for people to give some game-oriented examples of how such patterns have come in useful. I use the Composite pattern, or at least some variant of it, in many cases. (Even before I heard it referred to as a pattern as such: I just considered it to be a kind of tree structure.) My custom GUI for my game is built this way, with a base class GUIObject and several descendants such as Screen (the root node), Button, ListBox, Picture (etc...) to provide the various degrees of functionality. The Factory Method pattern is commonly employed for objects that I always need to be stored in some main list. For example, in my game, every Character needs to be stored in the allCharacters list. So instead of having to worry about that every time I create character, I implemented Character::Create() which allocates the Character and adds it to the list before returning the pointer. I am planning to use the Proxy pattern soon to save me some memory usage: currently I load a lot of small files that represent hints and tips for the player. I expect some of these to be used much more than others. They are also used fairly infrequently. So it makes little sense to keep them all in memory, all the time. In future, I will use the proxy object to create my own cache: load them on demand, up to a predetermined maximum, and deallocate the least commonly used when we load a new one. I can see how this could be used for texture management, too. Anybody wish to add their experiences here?

Share this post

Link to post
Share on other sites

During the development of a sound library, I learned that the usage of the FactoryMethod-Pattern was very useful. It was possible to implement a soundbuffer-duplication, in the base class, where I just called the clone()-Method (as it is called in Scott Meyers "More effectice C++") of the derived classes to handle the duplication of the buffer of correct type itself, but logic still stays in the base class.
Also, the Singleton is a great pattern to avoid multiple instances. There is just one active sound listener, and other listener objects are different only by other data, they don´t need an other listener-object. So it is possible to share one (DirectSound-)Listener object with more (C++)-Listener objects.


...and he went forth conquering, and to conquer...

Share this post

Link to post
Share on other sites
For the wrappers for the 3D engine I use, I use the Adapter pattern.

To support undoing of operations client side I use the Command pattern. This is mostly important if you are having a networked game with dead reckoning on the client.

But yes you are right. YOu are not supposed to use Design Patterns for everything. They are just one extra tool for you toolbox of methods you can use to get your things done. Design patterns are not the only right way to do things, but are a handy tool in some cases.

Jacob Marner

Share this post

Link to post
Share on other sites
I use something similiar to a facade/manager pattern often times to manage shared resources (usually files, such as textures). For instance, I have a class called TextureManager which looks like this:

#define TextureHandle int

class TextureManager
//At first, it might seem somewhat dumb just to have

//a fixed size array, but I did it this way mainly so that

//the handles could be direct indices into this list.

//I could imagine this being implemented as a stl map,

//with some modifications to what a TextureHandle is.

Texture* TextureList[MAX_TEXTURES]

//user of the class doesn''t need to work with these, since

//GetTextureHandle does the job of both of these.

TextureHandle AddTexture( char* filename );
TextureHandle TextureExists ( char* filename );

//Finds the handle of the texture if it''s loaded,

//otherwise, it will load the texture and give you a

//handle (in other words, it transparently determines

//whether a file needs to be loaded or not).

TextureHandle GetTextureHandle( char* filename );
void RemoveTexture( TextureHandle );

//Gives you a pointer to a texture in the case that you

//need to modify the texture somehow.

Texture* GetTextureInterface( TextureHandle Handle);


This could probably be extended to work with many other things besides just files, although that''s what I''ve found it to be most usefull for so far. The only real problem with this is the RemoveTexture function, since it invalidates all the handles. Luckily, my engine just draws a blank texture in that case, but it should be handled in a better way (probably with some sort of reference counting scheme).

Share this post

Link to post
Share on other sites

  • Advertisement