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