Friday 22 November 2013

Lecture 2-2 Design Patterns

Since exam time is approaching I wanted to do a blog on some of the earlier lessons in game engines. I thought a good place to start would be on design patterns. Design patterns seemingly are the solution for making your programming less complex. They can make long lines of code more reusable to save the programmer lots of time. I remember reviewing object oriented programming during this lesson. The lessons learned in the object oriented programming class go hand in hand with the new information from lecture 2-2. Using inheritances, classes and polymorphisms can solve the problem of having way too much code in your game. It is a good way ease up the load in your programming by using design patterns. There are four new shortcuts that we can use to help with long lines of reusable code. They are the singleton, facades, state, and factory. Below is a short understanding of all of them.


Singleton

In C++, a singleton is used to return control to the programmer by isolating the unpredictability of the order of dynamic initialization. A singleton pattern is a class that restricts the instantiation of a class to one object. It's commonly used instead of using a globally defined object. There are many uses for singletons like if your game requires a single renderer or a single keyboard is needed.


Facade

A facade is an object that provides a simpler version of a large amount of code like a class library. It is used when one wants an easier interface to an underlying implementation object. It links a lot of related classes which are usually sets of singletons. Facades make communication between subsystems much easier because only one class exchanges data with another class that is within a subsystem which contains interactive classes.

State

State patterns use objects to represent logical states. They can be used for A.I. state switching in behavioural control. The best example would be in the game Pac-man. The A.I. ghosts have different states like, flee or chase depending on what the player does. It allows for easy addition of classes to already existing  systems without having to modify too much code. It helps get rid of switch statements. Below is an easy to understand graphic of how the states belong to a system and how it might work.
 

Factory

A factory is a place where all instances of all types of objects are created. It acts similar to a factory in real life that manufactures objects in the same way. Once the objects have been created, they are shipped off. The same thing is with a factory pattern (as seen below in the figure), it creates many objects  for specific function. The factory pattern is a powerful tool for extensibility and its also good for organizing memory allocation.


In conclusion, design patterns are a very good practice to use in object oriented programming. They help make code tidy and neat. These type of patterns will help improve programming styles and get you prepared for a professional coding environment.

No comments:

Post a Comment