Click here to Skip to main content
15,888,461 members
Articles / Desktop Programming / Windows Forms
Article

Implementing Object Pool in C#

Rate me:
Please Sign up or sign in to vote.
3.37/5 (10 votes)
2 Dec 2005CPOL4 min read 62.6K   1.3K   25   7
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
I have been working in software industry for the past 5 years now. Enjoy working on complex and new technologies. Worked on Microsoft technologies Like VC++, COM, XML, SQL. Currently working on .Net, C#.

Comments and Discussions

 
GeneralBetter article found about Object Pool Pin
Ivan.vv6-Aug-10 6:51
Ivan.vv6-Aug-10 6:51 
QuestionIs there another example for c# 2.0 ? Pin
brsky5-Aug-08 15:58
brsky5-Aug-08 15:58 
AnswerRe: Is there another example for c# 2.0 ? Pin
brsky5-Aug-08 16:07
brsky5-Aug-08 16:07 
Questionhow to call object from one class to the other class Pin
Sankar Komma 19-Oct-06 23:32
Sankar Komma 19-Oct-06 23:32 
AnswerRe: how to call object from one class to the other class Pin
Madhu Raykar22-Oct-06 23:00
Madhu Raykar22-Oct-06 23:00 
Hi I could not get the question. can you please explain with example.
Generaldownload links not working Pin
Preky2-Dec-05 3:28
Preky2-Dec-05 3:28 
GeneralRe: download links not working Pin
Madhu Raykar2-Dec-05 4:37
Madhu Raykar2-Dec-05 4:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.