重温经典
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();
}
}
}