(原创)C#中的单例模式-笔记

来源:互联网 发布:淘宝定时开售怎么设置 编辑:程序博客网 时间:2024/06/03 21:43

在C++中,单例模式很容易控制,知道在何时进行析构,但是在C#中,由于所有的实例都是在托管堆中,所以,我们不知道单例在何时进行释放。

通常,一些配置实例被实例化成单例,以便在配置界面,还是程序在运行时,都会统一配置;在实例析构的时候,进行保存文件,如果程序异常推出,将不能正确保存文件,所以,我们可以在修改完毕就保存文件即可。  



    public class MouseCommandSetTable    {        public static MouseCommandSetTable Instance        {            get            {                lock (myObject)                {                    if (_instance == null)                    {                        _instance = new MouseCommandSetTable();                    }                }                return _instance;            }        }        public void Save()        {        }        public Dictionary<CommandKey, MouseCommandSetPermitCell> Dictionary        {            get            {                return _dictionary;            }        }        static object myObject = new object();         private Dictionary<CommandKey, MouseCommandSetPermitCell> _dictionary = null;        private MouseCommandSetTable()        {            //Load            //if invalidate            _dictionary = new Dictionary<CommandKey, MouseCommandSetPermitCell>()            {                {                    CommandKey.WL,                     new MouseCommandSetPermitCell()                    {                        CommandPermitType = EPermitType.DragDrop,                        CommandPermitMouseButton = EPermitMouseBt.Right                    }                },                {                    CommandKey.ROI,                     new MouseCommandSetPermitCell()                    {                        CommandPermitType = EPermitType.DragDrop,                        CommandPermitMouseButton = EPermitMouseBt.Left                    }                },                {                    CommandKey.Move,                     new MouseCommandSetPermitCell()                    {                        CommandPermitType = EPermitType.DragDrop,                        CommandPermitMouseButton = EPermitMouseBt.Left                    }                },                {                    CommandKey.Zoom,                     new MouseCommandSetPermitCell()                    {                        CommandPermitType = EPermitType.Wheel,                        CommandPermitMouseButton = EPermitMouseBt.Middle                    }                },                {                    CommandKey.NextPage, //只能在Middle button上进行定义                    new MouseCommandSetPermitCell()                    {                        CommandPermitType = EPermitType.Wheel,                        CommandPermitMouseButton = EPermitMouseBt.Middle                    }                },                {                    CommandKey.AnnoLine, //只能在Middle button上进行定义                    new MouseCommandSetPermitCell()                    {                        CommandPermitType = EPermitType.DragDrop,                        CommandPermitMouseButton = EPermitMouseBt.Left                    }                }            };            //else        }        private static MouseCommandSetTable _instance = null;        ~MouseCommandSetTable()        {            //保存            this.Save();        }    }

原创粉丝点击