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

    }

}
 
========================================================================
这个缓冲是使用的是C#对象数组,当然针对的是引用对象,值对象无法使用。为了方便大家使用,下面我再给出
具体使用的例子。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TestPoolMamaner
{
    public partial class Form1 : Form
    {
        KLine currentkline = null;
        Wuzhiqiang.PoolManagerGeneric<KLine, String> klPoolManager =
            new Wuzhiqiang.PoolManagerGeneric<KLine, String>(20);
        public Form1()
        {
            InitializeComponent();
        }
 
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;
        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        #region Windows 窗体设计器生成的代码
        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(280, 15);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "test";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(104, 12);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 21);
            this.textBox1.TabIndex = 1;
            this.textBox1.Text = "123456";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(21, 15);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(77, 12);
            this.label1.TabIndex = 2;
            this.label1.Text = "Code String:";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(21, 115);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(377, 12);
            this.label2.TabIndex = 3;
            this.label2.Text = "change code and click test, if code is repeat pool is working.";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(437, 146);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();
        }
        #endregion
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        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
        }
    }
 
   static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
   public class KLine : Wuzhiqiang.IKnowByKey<String>
    {
        private String __stockCode;
        private String __stockName;
        // ...............
        public bool ThatIsMe(String key)
        {
            return __stockCode == key;
        }
        public String StockCode
        {
            get { return __stockCode; }
        }
        public String StockName
        {
            get { return __stockName; }
        }
        public void Load(String path, String code, String name, String city)
        {
            __stockCode = code;
            __stockName = name;
            // .................
        }
    } 
}
// 工作愉快
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值