Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Buffered Pool Manager Class

2.13/5 (4 votes)
21 Jun 2008CPOL 1   151  
In this article, I'm going to talk about a buffered pool of any Object and an implementation I have written in C#.
test.jpg.jpg

Introduction

In this article, I'm going to talk about a buffered pool of any Object and an implementation I have written in C#.

Background

Using C# for just one year helped me so much, so I hope this code can help somebody.

Before using C#, I used C++ since I was 15 years old. The segment code is part of a stock program, and I had to rewrite to generic code — you can use it in anywhere if you need it. I come from China and my English is very poor, so, sorry about the bad reading.

Using the Code

The C# code is here:

C#
using System;
namespace Wuzhiqiang
{
    interface IKnowByKey<T>
    {
        bool ThatIsMe(T key);
    }
    class PoolManagerGeneric<T,U>  where T: class, IKnowByKey<U>
    {
        #region Variable...
        private int __length = 10; 
        private int __count = 0;
        T[] __objectPool = null;
        #endregion
        #region Properties...
        public int Length
        {
            get { return __length; }
        }
        #endregion
        #region Construct ...
        public PoolManagerGeneric()
        {
            init();
        }
        public PoolManagerGeneric(int max)
        {
            if (max >= 5 && max <= 1000)
                __length = max;
            init();
        }
        #endregion
        #region Functions ...
        private void init()
        {
            __objectPool = new T[__length];
            __count = 0;
        }
        public void SaveObject(T obj)
        {
            if (__count >= __length)
                __count = 0;
            __objectPool[__count++] = obj;
        }
        public T GetObjectByKey( U key )
        {
            for(int i= 0; i< __length; i++ )
            {
                if( __objectPool[i] != null && __objectPool[i].ThatIsMe(key))
                    return __objectPool[i];
            }
            return null;
        }
        #endregion
    }
}

// this is the program code.....
   
    public partial class Form1 : Form
    {
        KLine currentkline = null;
        Wuzhiqiang.PoolManagerGeneric<KLine, String> klPoolManager =
            new Wuzhiqiang.PoolManagerGeneric<KLine, String>(20);
        public Form1()
        {
            InitializeComponent();
        }
        private KLine LoadKLineData(String path, String code, String name, String city)
        {
            KLine temp = klPoolManager.GetObjectByKey(code);
            if (temp == null)
            {
                temp = new KLine();
                temp.Load(path, code, name, city);
                klPoolManager.SaveObject(temp);
            }
            return temp;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            KLine find = klPoolManager.GetObjectByKey( this.textBox1.Text);
            if (find != null)
            {
                MessageBox.Show(" the object get from pool!"); 
            }
            currentkline = LoadKLineData( "", this.textBox1.Text, "name", "");    
    
            // user
        }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)