Object Pool Design Pattern

What is Object Pool Design Pattern?

  • It falls under the Creational Design Pattern.
  • What is Object Pool?
  • Pool is a collection of something. If we consider swimming pool, it’s a collection of water and N number of people can swim at a particular time. Similarly Object Pool is a collection of objects. 
  • It is used when the cost of initialization is very high.
  • It is a container which holds some amount of objects.
  • When the agent requires an object, then it will be used from that pool, and then it is reverted to that pool.

Real Life Examples of Object Pool Design Pattern

Example 1:

books photo

Let’s suppose; you are an engineering student. You’d have several exams for eight semesters. For each semester there would be N number of exams. For the preparation of those exams, you need books. Some people will find it difficult to buy those books because those books are very expensive. So, those people will borrow books from the library, and after the semester they will return those books to the library.Library uses those books to serve other people. Here, the books are the objects. The books are getting reused. Instead of buying the books we are trying to get books from the library. After the books are returned, the library representative will reuse those books. The library representative will serve many people.

Example 2:

database photo

If there are N number of clients, nearly thousand clients who try to create a database connection pool.

If you create one database connection pool itself is a costly object because it involves an N number of resources. If those thousand clients are creating a similar kind of object means, then the expense would be 1000x.

So instead we’ll reuse the object that is already created by someone. That’s called “Reusable Pool”. This reusable pool will try to create some object, some limited object, it will try to manage the clients.

Let’s consider if suppose if four clients ask for that object, this “Reusable Pool” will try to serve those four clients.

This reusable object pool acts as the manager for the objects.

It will try to get the used objects from the client, and then it will pass those objects to the clients who are expected to get those objects, that’s the power of this reusable tool.

How Object Pool Design Pattern implemented in Java?

  • It uses the concept called “Reusable objects”.
  • This reusable pool will try to create some limited objects and it will try to manage the clients.
  • Let’s consider if suppose if four clients are asking the object means this Reusable Pool will try to serve those four clients. 
  • This reusable object pool act as a manager for the objects. 
  • So it will try to reuse, and it will try to get the used objects from the client, and then it will try to give those objects to the clients who are expected to get those objects, that’s the power of this reusable pool.

Advantages of Object Pool Design Pattern

  • It will boost the performance because it reuses the already created objects instead of creating new ones.
  • Example: Creating a database connection is a costly operation. So, it will try to use the already existing database connections.
  • Let’s consider if suppose N number of clients are using N number of objects means that N number of objects would occupy some memory. Since we are reusing the objects, it would implicitly save some memory.

Tutorial Videos of Object Pool Design Pattern

Play Video