Click here to Skip to main content
Click here to Skip to main content

Buffered Pool Manager Class

, 20 Jun 2008
Rate this:
Please Sign up or sign in to vote.
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:

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)

About the Author

wuzhiqiang

China China
No Biography provided

Comments and Discussions

 
GeneralAw crap... PinmemberPIEBALDconsult12-Jun-08 8:07 
GeneralRe: Aw crap... Pinmemberwuzhiqiang12-Jun-08 17:59 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 21 Jun 2008
Article Copyright 2008 by wuzhiqiang
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid