开发手册 欢迎您!
软件开发者资料库

.NET(C#) 设计模式 享元模式

设计模式(Design pattern)是代码设计经验的总结。设计模式主要分三个类型:创建型、结构型和行为型。创建型是对象实例化的模式,创建型模式用于解耦对象的实例化过程,主要用于创建对象。结构型是把类或对象结合在一起形成一个更大的结构,主要用于优化不同类、对象、接口之间的结构关系。行为型是类和对象如何交互,及划分责任和算法。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性。本文主要介绍.NET(C#) 设计模式 享元模式。

享元模式(Flyweight Pattern)

享元模式(Flyweight Pattern)是一种结构型模式,是运用共享技术有效的支持大量细粒度的对象。它使用共享对象,用来尽可能减少内存使用量以及分享资讯给尽可能多的相似对象;它适合用于只是因重复而导致使用无法令人接受的大量内存的大量对象。通常对象中的部分状态是可以分享。常见做法是把它们放在外部数据结构,当需要使用时再将它们传递给享元。Flyweight模式需要认真考虑如何能细化对象,以减少处理的对象数量,从而减少存留对象在内存或其他存储设备中的占用量。String常量池、数据库连接池、缓冲池等等都是享元模式的应用。

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 freeConnections;        //存放目前正在使用的连接        private List busyConnections;        //设定连接池的大小        private int maxConnections;        //构造函数        public GdDbConnectionPool(int numConnections)        {            maxConnections = numConnections;            freeConnections = null;            busyConnections = null;        }        #region 实现获取数据库连接的操作        ///         /// 实现获取数据库连接的操作        ///         ///         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 产生连接池        ///         /// 产生连接池        ///         public void InitConnPool()        {            Console.WriteLine("------数据库连接的初始化------");            freeConnections = new List(maxConnections);            busyConnections = new List(maxConnections);            for (int i = 0; i < maxConnections; i++)            {                freeConnections.Add(new Connection() { Id = i });            }        }        #endregion        #region 从繁忙池中销毁已经返回的连接        ///         /// 从繁忙池中销毁已经返回的连接        ///         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();        }    }}