Click here to Skip to main content
Licence CPOL
First Posted 12 Jun 2008
Views 10,684
Downloads 33
Bookmarked 12 times

Buffered Pool Manager Class

By | 20 Jun 2008 | Article
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

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralAw crap... PinmemberPIEBALDconsult8:07 12 Jun '08  
GeneralRe: Aw crap... Pinmemberwuzhiqiang17:59 12 Jun '08  
because the reason of efficiency, I rewrite part of code, add a var __fullFlag, your GenericPool class, do you mind show it with me? thanks.
 
I used VC++ & C# in my job.

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 21 Jun 2008
Article Copyright 2008 by wuzhiqiang
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid