zoukankan      html  css  js  c++  java
  • 对Singleton Pattern的一点修改

    重温经典

    Singleton模式要求一个类有且仅有一个实例,并且提供了一个全局的访问点。

    保证一个类仅有一个实例,并提供一个访问它的全局访问点!

    简单实现

     1public sealed class Singleton
     2{
     3    static Singleton instance=null;
     4
     5    Singleton()
     6    {
     7    }

     8
     9    public static Singleton Instance
    10    {
    11        get
    12        {
    13            if (instance==null)
    14            {
    15                instance = new Singleton();
    16            }

    17            return instance;
    18        }

    19    }

    20}

    为了共享数据我对Singleton做了简单修改:

    (在这里怎么做才能实现上面的折叠效果?)

     

    namespace MyDream.Data
    {
        public sealed class DataListInstance
        {
            public static volatile List<ThreeDGameInfo> _dataList = null;

            public static volatile List<ThreeDGameInfo> _halfDataList = null;

            public static readonly object obj = new object();

            public static void ReSetInstance()
            {
                _dataList = null;
                _halfDataList = null;
            }

            protected static void InitDataList()
            {
                string xmlFile = ConfigurationManager.AppSettings["XmlDataConfigPath"]+DreamConst.xmlDataSourceFile;

                _dataList = ThreeDGame.GetModeList(xmlFile) as List<ThreeDGameInfo>;//GetModelListFromXML(xmlFile);
               
            }

            public static void InitHalfDataList()
            {
                _halfDataList = ThreeDGame.GetHelfModelList(_dataList) as List<ThreeDGameInfo>;
            }

            public static List<ThreeDGameInfo> Instance()
            {
                if (_dataList == null)
                {
                    lock (obj)
                    {
                        if (_dataList == null)
                        {
                            InitDataList();
                        }

                    }

                }

                return _dataList;
            }

            public static List<ThreeDGameInfo> HalfInstance()
            {
                if (_halfDataList == null)
                {
                    lock (obj)
                    {
                        if (_halfDataList == null)
                        {
                            InitHalfDataList();
                        }

                    }

                }

                return _halfDataList;
            }


            public static List<ThreeDGameInfo> Get()
            {
                return Instance();
            }

            public static List<ThreeDGameInfo> GetHalf()
            {
                return HalfInstance();
            }

        }

    }

     

  • 相关阅读:
    在sql语句中使用 xml for path 格式化字符串的方法总结
    Android handler的使用简单示例
    easyui datagrid中 多表头方法总结
    使用ICSharpCode.SharpZipLib.Zip类库解压zip文件的方法
    ThreadPoolExecutor 优雅关闭线程池的原理.md
    ThreadPoolExecutor 几个疑惑与解答
    如何在运行时(Runtime)获得泛型的真正类型
    为什么 EXISTS(NOT EXIST) 与 JOIN(LEFT JOIN) 的性能会比 IN(NOT IN) 好
    Spring MVC 上下文(ApplicationContext)初始化入口
    Tomcat生成的session持久化到MySQL
  • 原文地址:https://www.cnblogs.com/zlddtt/p/1504929.html
Copyright © 2011-2022 走看看