Object Pool Design Pattern : Fictional Example of “Allocating Battle Royal Game Room to Players or vice versa”

Danyson
5 min readFeb 20, 2021

Have you imagined anytime of how we can allocate players to a game room created in a battle royale game ?

Image Credit : Unsplash

For people who are new to the term battle royale, its a competitive game where 100 people either solo or teaming up duo or 4 player squad, compete in a place with a time limit or a closing circle for survival.

Before getting into concepts, let me show you a picture below .

Image Credit : Wikipedia

The pic above you see is from a “bicycle sharing hotspot” where you can rent a cycle and ride it where you want and later you can leave it where you picked up so that others can pick it up for their use.

The purpose of the “Creational Pattern : Object Pool” is nearly similar to the above bicycle sharing system.

You have a collection of Object instances stored in a container (a class) called Object Pool, from there you can either acquire (like renting the cycle) or release after you use (like returning the cycle back).

Let us try the object oriented code of object pool in Python to understand the concept with a fictional example of creating a game room in a battle royale game where 100 players can join.

Abstract Classes of our Object Pool

Lets import ABC and abstractmethod from abc library initialy, now we have to create three different abstract classes by inheriting the ABC class to them along with syntactic sugar of abstractmethod for the methods of the classes, the classes are namely “Game class” for the game definition, an “InstanceHolder class” to help the object pool to hold instances and finally the “ObjectPool class” for acquiring and releasing objects.

Concrete Classes of our Battle Royale Object Pool

Now Our BattleRoyale class inherits the Game class, for simplicity lets construct our private member variable to gameName (holds the name of the game) only and define the getter called get_name() for accessing the Battle Royale game name.

When you create a Battle Royale game Object, you need a holder class to hold the object instance for reference. So lets inherit the InstanceHolder class, construct the private member variable instance with gameInstance (instance of a battle royale game).

Now when its come in to creating an object pool for Battle Royale game, we inherit two important methods acquire() (acquires object) and release() (releases object) from the object pool abstract class. Upon constructing the class member variable battleRoyaleObjectPool, let that be a list, so that it can hold object instances and can do operations like pop() during object instance acquiring and append() during object instance insertion to our pool of objects. The object pool list size is based on user preference and based on size we do a loop operation on getting our desired object instance of battle royale game to fill the list (we use the getter called get_instance() of the battle royale game to get its instance). Now the list will be ready to serve us with objects, accept objects to and from its pool of objects.

The Player Class

On constructing the Player class we pass arguments only for id and name of the player and others like name of the game, id of the team the player is in and the game room that’s going to hold the games’s instance are set as None so that we can set them later using setter, as we can use the setter to assign team id, game name and game room instance also we can set them to None based on a condition if the team id is None, as you see in a solo or duo or squad match you must be in a team, “even if you are competing solo, you are in your own team of size 1” (in real World may be being solo can’t be a team but for our convenience we can term it as a team of size 1) so it is helpful to understand whether the match or game has been created or not, as you know when a player logins’s to a game he first forms a team and then enters a game room. So its convenient while we release a player’s game room slot back to the pool, simultaneously we can set the player’s game name and game room details to none if he disbands from a team.

Utility function to check how many instances are in our object pool ?

When you want check and compare how many objects are in our pool while acquiring object and releasing object, we can make use of this simple function that counts the number of objects in object pool.

The flow of creating players, battle royale game and room, adding game room to object pool, acquiring, releasing game room objects from pool later by checking whether we doing ok.

Result

when object pool is created there are 100 instances of battle Royale game One

{'id': 1, 'name': 'playerOne', 'game name': 'game one', 'team id': 1, 'game room': <__main__.BattleRoyale object at 0x7fad00455fd0>}

when one object from object pool is given to player one there are 99 instances of battle Royale game One in object pool

{'id': 2, 'name': 'playerTwo', 'game name': 'game one', 'team id': 1, 'game room': <__main__.BattleRoyale object at 0x7fad00455fd0>}

when another one object from object pool is given to player two there are 98 instances of battle Royale game One in object pool

when one object is released from player one to object pool there are 99 instances of battle Royale game One in object pool

when another one object released from player two to object pool there are 100 instances of battle Royale game One in object pool

{'id': 1, 'name': 'playerOne', 'game name': None, 'team id': None, 'game room': None}

{'id': 2, 'name': 'playerTwo', 'game name': None, 'team id': None, 'game room': None}

Hope you all understand the use case of object pool creational pattern from the design pattern and i leave the full code below for your reference.

Visit My Personal Blog @ danyson.github.io

--

--