单例模式

37 篇文章 0 订阅

1.  防止多线程创建的多个对象争抢资源

class myclass{ 

    public myclass(){}

    public static myclass GetInstance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_lockObject)
                    {
                        if (_instance == null)
                        {
                            _instance = new myclass();
                        }
                    }
                }
                return _getInstance;
            }
        }
}

2. 初始化参数或变量,直接获取静态资源

class Configaration{
    private string _connecString;
        private string _threadhold;
        private string _nextHours;
        private string _archiveDir;
        private string _delFlag;

        public string DelFlag
        {
            get { return _delFlag; }
            set { _delFlag = value; }
        }

        //快捷键:选中private string ...;然后按Ctrl+r+e
        public string ConnecString
        {
            get { return _connecString; }
            set { _connecString = value; }
        }

        public string Threadhold
        {
            get { return _threadhold; }
            set { _threadhold = value; }
        }

        public string NextHours
        {
            get { return _nextHours; }
            set { _nextHours = value; }
        }

        public string ArchiveDir
        {
            get { return _archiveDir; }
            set { _archiveDir = value; }
        }


    public static Configaration GetInstance() { 
            //获取配置文件路径
            string iniPath =         Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory,"Configaration.ini");
            //如果存在该路径,则执行
            if (!File.Exists(iniPath)) {
                throw new FileNotFoundException(iniPath);
            }
            //new Configaration接收参数
            Configaration conf = new Configaration();
            string[] lines = File.ReadAllLines(iniPath);

            //使用字典存值提升效率
            Dictionary<string, string> dict = new Dictionary<string, string>();
            foreach (string l in lines)
            {
                if (string.IsNullOrWhiteSpace(l))
                    continue;
                if (l.Substring(0, 1) == "#")
                    continue;
                if (l.Contains("=")) {
                    string[] arr = l.Split("=".ToCharArray(), 2,StringSplitOptions.RemoveEmptyEntries);
                    if (!(string.IsNullOrWhiteSpace(arr[0]) || string.IsNullOrWhiteSpace(arr[1])))
                    {
                        dict.Add(arr[0], arr[1]);
                    }
                }
            }

            conf.ConnecString = dict["ConnectString"];
            conf.Threadhold = dict["Threadhold"];
            conf.DelFlag = dict["DelFlag"];
            return conf;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值