Click here to Skip to main content
15,884,473 members
Articles / Programming Languages / C#

Buffered Pool Manager Class

Rate me:
Please Sign up or sign in to vote.
2.13/5 (4 votes)
20 Jun 2008CPOL 19.2K   151   12   2
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)


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAw crap... Pin
PIEBALDconsult12-Jun-08 8:07
mvePIEBALDconsult12-Jun-08 8:07 
GeneralRe: Aw crap... Pin
wuzhiqiang12-Jun-08 17:59
wuzhiqiang12-Jun-08 17:59 

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.