Oct
18
Creational Design Pattern: The Objectpool
During the courses that I have followed on The Hague University of Applied Sciences, Design Patterns have lead a dominant role in many of the projects that I have completed. Design Patterns are tested solutions to common problems, and the beauty of these patterns is that they are extendable, maintainable and re- useable. In an effort to better understand the different Design Patterns that exist today, I’ve started a series of blogs that elaborate on the different patterns. This is mostly to support my studying, since I would be putting my knowledge into words, which helps me to learn and understand. However, I am more than happy to share my findings with whomever is interested. Every Pattern will be supported with code examples, mostly done in Java. You don’t need to be a Java master, but it is handy to know about OOP programming and Java in general.
In this article, we’re going to take a closer look at the Object Pool.
The Object Pool falls under the category Creational Design Pattern and helps with the performance of your application, by controlling and re-using heavy-duty objects. As the name implies, the Object pool is a pool of limited objects. Your clients will ask the Object pool for a reusable object, which is given as long as there’s a stock of them. The moment all the reusable objects are in use, the client’ll have to wait until one becomes available. When the client gives back a reusable object, the Object-pool will return the object to its original state and make it available for use. The objects within the pool are created once, and then reused over and over untill the application shuts down. As you can already guess, this increases performance of your application, since it doesn’t have to create these objects over and over.

So how do we create an object pool? I shall show below:
//Reusable public class Reusable { private String name;
public Reusable(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}
In this example, our client will use a Reusable with a name, given by the Object pool, so that we know which reusable is used.
// Object Pool public class Objectpool { private static Objectpool instance; private ArrayList<Reusable> reusables = new ArrayList<Reusable>(); private ArrayList<Reusable> userdReusables = new ArrayList<Reusable>(); private FINAL int NUMBEROFREUSABLES = 2;
private Objectpool() { for(int i = 0; i < NUMBEROFREUSABLES; i++) { Reusable r = new Reusable("r" + i); reusables.add(r); } }
public static Objectpool getInstance() { if(instance == null) { instance = new Objectpool(); } return instance; } publicReusable requestReusable() { Iterator it = reusables.iterator(); if(it.hasNext()) { Reusable availableReusable = (Reusable)it.next(); reusables.remove(availableReusable); usedReusables.add(availableReusable);
return availableReusable;
}
else
{
return null;
}
}
public void returnReusable(Reusable reusable)
{
if(usedReusables.contains(reusable))
{
usedReusables.remove(reusable);
reusables.add(reusable);
}
}
}
Here we have the Objectpool. As you can see, it is pretty straightforward. In this class we dictate the number of reusables the pool might have, which are then created in the constructor and stored in the ArrayList. As you can see, the Singleton Design Pattern is also implemented here, since the purpose of the Object pool is defeated if you can create multiple instances of it. The client will use the requestReusable() function to get a reusable. If one is available, it’ll be removed from the reusables array and added into the usedReusables array. The client will return the reusable via the returnReusable function, from which the reusable is returned to our list of reusables if the reusable is in the usedReusables array. Now we’ll tie all of this together in the client:
// The Client
public static void main(String[] args)
{
Reusable reusable;
reusable = Objectpool.getInstance().requestReusable();
System.our.println("The client is using the reusable: "
+ reusable.getName());
Objectpool.getInstance.returnReusable(reusable);
reusable = Objectpool.getInstance().requestReusable();
System.our.println("The client is using the reusable: "
+ reusable.getName());
reusable = null;
}
The output of the printline would be: “The client is using the reusable: r0″, and then ”The client is using the reusable: r1″. This is because when you return the first reusable, it’s put at the end of the ArrayList, so first we used the r0 reusable, then returned it, then requested another reusable. R1 was next in line, so the Objectpool returned that one. If you would use both reusables, and then request another one, the Objectpool will return a false, since we only told it to have two reusables available for us.
Related posts:
