Jump to content

View more

Image of the Day

The night is still, but the invasion brings chaos. #screenshotsaturday #hanako #indiegame #gameart #ue4 #samurai https://t.co/cgILXuokoS
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

Examples of common Design Patterns in games

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Kylotan   Moderators   


Posted 14 December 2000 - 09:45 AM

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?

#2 LordBravery1   Members   


Posted 15 December 2000 - 12:03 AM


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...

#3 felonius   Members   


Posted 02 January 2001 - 05:58 PM

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

#4 Facehat   Members   


Posted 03 January 2001 - 06:42 AM

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).

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.