Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Implementing Object Pool in C#

0.00/5 (No votes)
2 Dec 2005 2  
Object pooling patterns are very useful object-oriented patterns. They work well for large or heavy weight objects that are expensive to create.
Sample Image - ObjectPool.jpg

Introduction

Object pooling patterns are very useful object-oriented patterns. They work well for large or heavyweight objects that are expensive to create. For example, you can store GDI objects in some Object Pool, and reuse them when required. This pattern greatly improves the speed of the code without adding to the object heap's overhead.

Here the object creation and caching responsibility is dedicated to one class which is generally a singleton class.

Background

Object pools (otherwise known as resource pools) are used to manage the object caching. A client with access to an Object pool can avoid creating a new Objects by simply asking the pool for one that has already been instantiated instead. Generally the pool will be a growing pool, i.e. the pool itself will create new objects if the pool is empty, or we can have a pool, which restricts the number of objects created.

It is desirable to keep all reusable objects that are not currently in use in the same object pool so that they can be managed by one coherent policy. To achieve this, the Reusable Pool class is designed to be a singleton class.

Implementation

To start out, let's have some heavy weight object requirement. Let us assume that we are developing a text editor application, which supports formatting of text.

Now the formatting will allow me to change the font, color, size, etc. of each character in the text. If this amount of granularity of formatting has to be supported, then I can consider each character as an individual object. These objects can be displayed on the screen; hence they will have all sorts of GDI objects within it. Hence the creation of these objects are expensive for me and are heavyweight objects.

I have created a class by name Character which represents an individual character within a text which has to be displayed. Since this class has to be displayed on the screen, it implements the interface IDrawable which defines the responsibility for drawing an object. Now here we find the need of a pool for Character objects because characters can be added to the text and removed. When we remove the characters we will not destroy them, instead we will send them to the pool, so that once we insert some more characters we don't have to create a new object, instead we can reuse the object in the pool.

So I have created a class by the name CharPool which is an object pool for my Character objects. This class is designed as a singleton class (explained earlier), hence its constructor is private, which forces other classes to call its static Instance property to get the one instance of the CharPool class. The pool will initially create 15 'Character' objects and maintain them, whenever there are no objects in the pool it will create a new object and return.

Now I have extended the Panel class to Implement a Canvas class, which is the text editor for me and will hold the Text (set of Character objects). In order to keep the sample simple, I have created a Keyboard panel on the mainform to provide the input rather than providing it from standard keyboard.

The MainForm will contain the Canvas (in which the text is displayed) and a CharPool (which contains Character objects). Now the MainForm is responsible for getting the Character objects from the pool and adding to the canvas and removing the objects from the canvas to replacing it back to the pool.

Design

Sample Image - ObjectPool.jpg

Using the Sample

Run the sample and you will be able to see an application with a canvas and a keyboard panel below. Note that the pool count indicates 15 (15 objects were created initially in the pool). Now press any of the characters on the keyboard panel Example 'A', what do you see?? Character 'A' is displayed on the screen, did you notice the pool count, yes it has reduced by one, the object which is there on the canvas came from the pool, try inserting few more objects and see that when there are no objects in the pool, it will create new objects. Now delete one of the objects by pressing the 'Backspace' button on the keyboard panel, the object will be sent back to the pool and reused once you add one more character.

Hope this explains the use of Object pooling.

History

  • 2nd December, 2005: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here