Useful Design Patterns for Games

Would anyone care to share which design patterns they found useful in game development and what problem those patterns solved in their games?

For example, the Template Method design pattern could be useful for a reusable game loop template because game loops are so similar between games.

If you have access to the ACM Digital Library (e.g. through a university library), then you should read this paper, “Computer Games as Motivation for Design Patterns”:
http://portal.acm.org/citation.cfm?id=1227391

It’s one of the best papers on the topic, if I do say so myself. :slight_smile: If you cannot get the paper through the DL, contact me and maybe I can help.

You happen to mention Template Method in your post. This is one pattern that I recommend to completely avoid. Delegation is almost always preferable to inheritance. My recommendation regarding the game loop is to make a “Game” interface with the update and render methods in it, and then have your main loop call update and render through that interface. To use the template method introduces very strong coupling between the superclass and subclass. It also introduces problems like Fragile Base Class. Another great article to read is “Why extends is evil”: http://www.javaworld.com/javaworld/jw-08-2003/jw-0801-toolbox.html. Holub’s writings are excellent for getting a better understanding of OOP and patterns.

Thanks for your responses.

purpleguitar, I found that javaworld article very interesting, even came with examples. :slight_smile: I’m going to make some adjustments to my code to reflect what I’ve learned. I don’t have access to the paper you mentioned and so no joy there.

JonathanC, I’m trying to learn more about game implementations and design patterns at the same time. For me, it’s sometimes difficult to realize the benefits of a design pattern from dry descriptions of the design patterns themselves, as you might find in some books. I was also hoping that I could narrow my focus for which patterns to better study. I understand what you are saying about not trying to force patterns into a game, but I think there must be a common set of design patterns that are naturals for the various parts of common game types.

JonathanC makes a good point that sometimes the best way to learn is to get your hands dirty. However, I believe that one of the advantages of design patterns is that they represent best practices, so that you don’t have to recreate the same mistakes that the rest of us have in the past. If you want to gain a deep understanding of patterns and how they are really used to build well-designed software, I highly recommend “Holub on Patterns: Learning Design Patterns by Looking at Code”, by Allen Holub (APress, 2004). Holub also wrote the article to which I linked earlier. The majority of the book comprises two case studies: Conway’s Game of Life and an embedded SQL interpreter. He presents the patterns in the natural context of software design, and the interaction of patters makes this a much better learning source than, say, the classic Gang of Four book (Gamma, et al.). There’s nothing in the book particular to games; if you don’t know the Game of Life, it’s not a game like Space Invaders is a game (http://en.wikipedia.org/wiki/Conway’s_Game_of_Life). However, game software is still just software, and I think the lessons presented in this book are transcended of their applications, as patterns should be.

Here’s an amazon link for your convenience: http://www.amazon.com/Holub-Patterns-Learning-Design-Looking/dp/159059388X/

Good luck!