C# デザイン パターン --- Flyweight パターン

フライウェイトパターン

フライウェイト パターンは、共有テクノロジーを使用して、多数のきめの細かいオブジェクトを効果的にサポートする構造パターンです。共有オブジェクトを使用してメモリ使用量を最小限に抑え、できるだけ多くの同様のオブジェクトと情報を共有します。重複により許容できないほど大量のメモリを使用する多数のオブジェクトに適しています。通常、オブジェクト内の状態の一部は共有できます。一般的な方法は、それらを外部データ構造に置き、必要に応じてフライウェイトに渡すことです。フライウェイト モードでは、処理されるオブジェクトの数を減らすためにオブジェクトを調整する方法を慎重に検討する必要があります。これにより、メモリまたはその他のストレージ デバイス内の永続オブジェクトの量が削減されます。文字列定数プール、データベース接続プール、バッファ プールなどはすべて Flyweight モードのアプリケーションです。

using System;
using System.Text;
using System.Collections.Generic;
using System.Collections;
namespace ConsoleApplication
{
    public interface IDbConnectionPool
    {
        //设定连接池中存放连接的数目
        void SetMaxConns(int numConnections);
        //设定打开或者关闭连接池
        void SetConnWitch(string Switch);
        //产生连接池
        void InitConnPool();
        //从连接池获取连接
        Connection GetConnection();
        //将连接返回给连接池
        void ReturnConnection();
        //销毁连接池
        void DestroyConnPool();
    }
    public class Connection
    {
        public int Id { get; set; }
        public string Database { get; set; }
        public int ConnectionTimeout { get; set; }
        public string DataSource { get; set; }
        public string Provider { get; set; }
    }
    public class GdDbConnectionPool : IDbConnectionPool
    {
        private static string connectionString =
            @"server=(local);Trusted Connection=yes;database=myDB";
        //默认连接池的大小
        const int defaultMaxConnections = 10;
        //存放目前空闲的连接,空闲池
        private List<Connection> freeConnections;
        //存放目前正在使用的连接
        private List<Connection> busyConnections;
        //设定连接池的大小
        private int maxConnections;
        //构造函数
        public GdDbConnectionPool(int numConnections)
        {
            maxConnections = numConnections;
            freeConnections = null;
            busyConnections = null;
        }

        #region 实现获取数据库连接的操作
        /// <summary>
        /// 实现获取数据库连接的操作
        /// </summary>
        /// <returns></returns>
        public Connection GetConnection()
        {
            if (freeConnections == null)
            {
                throw new Exception("连接池还没有创建");
            }
            Console.WriteLine("------获取数据库连接的操作------");
            //获取空闲池连接
            Connection conn = (Connection)freeConnections[0];
            freeConnections.Remove(conn);
            busyConnections.Add(conn);
            return conn;
        }
        #endregion
        #region 产生连接池
        /// <summary>
        /// 产生连接池
        /// </summary>
        public void InitConnPool()
        {
            Console.WriteLine("------数据库连接的初始化------");
            freeConnections = new List<Connection>(maxConnections);
            busyConnections = new List<Connection>(maxConnections);
            for (int i = 0; i < maxConnections; i++)
            {
                freeConnections.Add(new Connection() { Id = i });
            }
        }
        #endregion
        #region 从繁忙池中销毁已经返回的连接
        /// <summary>
        /// 从繁忙池中销毁已经返回的连接
        /// </summary>
        public void ReturnConnection()
        {
            Connection conn = (Connection)busyConnections[0];
            if (conn == null)
                throw new Exception("没有发现繁忙池中有连接");
            busyConnections.Remove(conn);
            freeConnections.Add(conn);
            Console.WriteLine("------从繁忙池中销毁已经返回的连接------");
        }
        public void SetMaxConns(int numConnections)
        {
            throw new NotImplementedException();
        }
        public void SetConnWitch(string Switch)
        {
            throw new NotImplementedException();
        }
        public void DestroyConnPool()
        {
            throw new NotImplementedException();
        }
        #endregion
    }
    class Program
    {
        static void Main(string[] args)
        {
            GdDbConnectionPool cpl = new GdDbConnectionPool(10);
            cpl.InitConnPool();
            cpl.GetConnection();
            cpl.ReturnConnection();
            Console.ReadKey();
        }
    }
}

 

おすすめ

転載: blog.csdn.net/lwf3115841/article/details/131756118