Object pool design pattern in python

Definition: It is one of the methods of creational design patterns. object creation is sometimes the major expensive step that we have to avoid to make our software reliable and the object pool design resolves the problem of creating objects multiple times. performance is a major concern of any software, in this method object is created during the creation period, and then it is put inside a common pool, so whenever the user needs that particular object of that particular class, then we can access it and use it rather than instantiating a new object. when the operation is completed then the object is returned to the pool again, if the object is not returned, there are two possibilities either the object is still in use or it is deleted for some reason. If the object is deleted then we have to create it again and if it is not deleted then it will be returned to the pool if required then we will have to instantiate the object again, so make sure that the object is returned to the pool safely, this is an important step in order to implement object pool design pattern properly. It will boost the performance of the software. you might think that it seems similar to the factory method but there is a major difference between the factory design pattern and the object pool design pattern,  the factory design pattern only creates the objects and uses them, maintenance of objects is not done, but in the object pool design pattern we are managing the created objects for efficient workflow and to improve the performance. The singleton method is used normally to implement this method.

When to use object pool design pattern :

We have to use it under these circumstances -

  •  The cost of object creation is high.
  • Initializing cost of any class instance is high.
  • The total number of used instantiation is low.
  • The class instantiation rate is high.
  • When there is a need for allocation and deallocation of objects.

Real-life example:

Example 1

suppose Mr. Vikash has a company named “subhkamna”, his company lends chairs and tables to occasion, whenever there is an occasion someone contacts him and he supplies the chair, suppose he has just started the company so he just have 100 chairs and 40 tables and somebody contacted him for 150 chairs and 50 tables so Mr. Vikash buys some extra tables and chair, and then supplied to that occasion, and then he takes his chair and tables back but think about one thing did he return all extra table and chair that is bought from the store? Or he will keep them with him, obviously, he will keep those with him as extra resources whenever it is needed he will use them or they will be reserved in the storehouse for future use.

This is what happens in object pool design patterns, if needed then we instantiate new objects but then they will be stored inside the pool, the unused object of some classes will be kept at the pool whenever they are required, they will be used and then after the process, it is returned back to the pool, so we do not have to instantiate the objects again and again.

Example - 2

Suppose a company named “XYZ” hired 10 employees so now the manager has to make workspace for them, there can be two conditions - the first one is - there is available extra space for 10 people so the manager does not have to worry and can assign them respective task easily, the second scenario is there is only 6 extra workspace so manager now has to think about remaining 4 people that how they will work. so the manager decided to buy 4 new tables, chairs and laptops for the remaining 4 employees now he can assign tasks to all of them.

But after some time 2 employees left, so what will the manager do now will he sell the table, chairs, and laptop of the two people? The answer is no, it will be kept as an extra resource and when the new employees will join it will be used, till then it will be kept idle.

So this is similar to the working object pool design pattern where unused objects are kept in the pool and when required will be used else they will be returned back to the pool.

I hope now you have a basic idea about the concept of object pool design patterns.

Diagram -

Object pool design pattern in python

Explanation :

So here we have three classes in the pool and each class consists of an object. we should keep all the unused objects in a single pool to govern them with the same coherent polity it makes our work simple, to do this we have to make our the pool using the singleton method, and we have to make the constructor private so that it will force the other classes to get the instances of this reusable pool.

We can see a client object in the diagram it will be calling the required objects, suppose the client calls hispool() object of get my obj class, from the reusable pool. So as you see it is available in the pool, and it will be delivered to the client. After the process, the client returns it back to the resource pool to its particular class, and now the object is again added to the collection.

There are some scenarios where the client does not returns the object back so we have to create the object again in order to add it to the pool otherwise it will not be accessible.

Advantages of object pool design pattern

  • It improves the performance of the software in a remarkable manner.
  • Reusability of objects becomes simple.
  • We can share objects in an easy way.

Disadvantages of object pool design pattern

  • It can contain a limited number of objects.
  • Sometimes we need to create an object again if it is not returned back by the client.