Game programming

Object Pool
Also known as Resource Pool Intent When objects are expensive to create and they are needed only for short periods of time it is advantageous to utilize the Object Pool pattern. The Object Pool provides a cache for instantiated objects tracking which ones are in use and which are available. Explanation Real world example In our war game we need to use oliphaunts, massive and mythic beasts, but the problem is that they are extremely expensive to create.
Service Locator
Intent Encapsulate the processes involved in obtaining a service with a strong abstraction layer. Class diagram Applicability The service locator pattern is applicable whenever we want to locate/fetch various services using JNDI which, typically, is a redundant and expensive lookup. The service Locator pattern addresses this expensive lookup by making use of caching techniques ie. for the very first time a particular service is requested, the service Locator looks up in JNDI, fetched the relevant service and then finally caches this service object.
Event Queue
Intent The intent of the event queue design pattern, also known as message queues, is to decouple the relationship between the sender and receiver of events within a system. By decoupling the two parties, they do not interact with the event queue simultaneously. Essentially, the event queue handles and processes requests in an asynchronous manner, therefore, this system can be described as a first in, first out design pattern model. Event Queue is a suitable pattern if there is a resource with limited accessibility (i.
Bytecode
Intent Allows encoding behavior as instructions for a virtual machine. Explanation Real world example A team is working on a new game where wizards battle against each other. The wizard behavior needs to be carefully adjusted and iterated hundreds of times through playtesting. It’s not optimal to ask the programmer to make changes each time the game designer wants to vary the behavior, so the wizard behavior is implemented as a data-driven virtual machine.
Data Locality
Intent Accelerate memory access by arranging data to take advantage of CPU caching. Modern CPUs have caches to speed up memory access. These can access memory adjacent to recently accessed memory much quicker. Take advantage of that to improve performance by increasing data locality keeping data in contiguous memory in the order that you process it. Class diagram Applicability Like most optimizations, the first guideline for using the Data Locality pattern is when you have a performance problem.
Dirty Flag
Also known as IsDirty pattern Intent To avoid expensive re-acquisition of resources. The resources retain their identity, are kept in some fast-access storage, and are re-used to avoid having to acquire them again. Class diagram Applicability Use the Dirty Flag pattern when Repetitious acquisition, initialization, and release of the same resource causes unnecessary performance overhead. Credits Design Patterns: Dirty Flag J2EE Design Patterns
Spatial Partition
Intent As explained in the book Game Programming Patterns by Bob Nystrom, spatial partition pattern helps to efficiently locate objects by storing them in a data structure organized by their positions. Explanation Say, you are building a war game with hundreds, or maybe even thousands of players, who are clashing on the battle field. Each player’s position is getting updated every frame. The simple way to handle all interactions taking place on the field is to check each player’s position against every other player’s position:
Type-Object
Intent As explained in the book Game Programming Patterns by Robert Nystrom, type object pattern helps in Allowing flexible creation of new “classes” by creating a single class, each instance of which represents a different type of object Explanation Say, we are working on a game which has a hero and many monsters which are going to attack the hero. These monsters have certain attributes like attack, points etc. and come in different ‘breeds’ like zombie or ogres.