• 对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();
            }

        }

    }

     

  • 相关阅读:
    C# 集合类 :(Array、 Arraylist、List、Hashtable、Dictionary、Stack、Queue)
    "Isa"与"Hasa"
    Access、SQLite、HSQLDB、Sybase、MySQL、DB4O比较
    C#反射(二)
    跳出语句
    C#反射(一)
    返回集合使用IEnumerable<>还是IList<>
    理解C#值类型与引用类型
    WF4 Beta2 工作原理
    Interesting thing with WF4 Activity Scheduling
  • 原文地址:https://www.cnblogs.com/zlddtt/p/1504929.html
Copyright © 2020-2023  润新知