S
sloan
I have
1. An interface (generic). I simplified it to have one method.
2. A class which implements this interface. This class is also a
Singleton.
3. A factory, (which right now only returns 1 concrete class, from #2
above)
I can't figure out if I can get the factory to work correctly, with
Generics.
#1 and #2 will compile.
#3 (GetAConcrete ) will not.
Ahhhh. I can't figure it out. Any help?
//The generic interface
public interface IDataStoreT<T>
{
void Clear();
}
//Singleton
[Serializable]
public class MemoryDataStoreT<T> : IDataStoreT<T>
{
private static MemoryDataStoreT<T> _singletonInstance;//= null;
private Dictionary<string, T> _memoryStore; // key (string), generic
T Type
/// <summary>
/// Initializes a new instance of the <see cref="MemoryDataStoreT"/>
class.
/// </summary>
private MemoryDataStoreT()
{
_memoryStore = new Dictionary<string, T>();
}
/// <summary>
/// Gets the singleton instance of the MemoryDataStoreT.
/// </summary>
/// <returns></returns>
public static MemoryDataStoreT<T> GetInstance()
{
if (null == _singletonInstance)
{
_singletonInstance = new MemoryDataStoreT<T>();
}
return _singletonInstance;
}
public void Clear()
{
if(null!=_memoryStore)
{
_memoryStore.Clear();
}
}
}
public class DataStoreTFactory
{
//static, simple factory class
public static IDataStoreT GetAConcrete () //<< does not compile
{
return MemoryDataStoreT<T>.GetInstance(); << yeah, I have no idea how to
give it an actual T
}
}
//Test Code
IDataStoreT ids = DataStoreTFactory.GetAConcrete ();
ids.Clear();
1. An interface (generic). I simplified it to have one method.
2. A class which implements this interface. This class is also a
Singleton.
3. A factory, (which right now only returns 1 concrete class, from #2
above)
I can't figure out if I can get the factory to work correctly, with
Generics.
#1 and #2 will compile.
#3 (GetAConcrete ) will not.
Ahhhh. I can't figure it out. Any help?
//The generic interface
public interface IDataStoreT<T>
{
void Clear();
}
//Singleton
[Serializable]
public class MemoryDataStoreT<T> : IDataStoreT<T>
{
private static MemoryDataStoreT<T> _singletonInstance;//= null;
private Dictionary<string, T> _memoryStore; // key (string), generic
T Type
/// <summary>
/// Initializes a new instance of the <see cref="MemoryDataStoreT"/>
class.
/// </summary>
private MemoryDataStoreT()
{
_memoryStore = new Dictionary<string, T>();
}
/// <summary>
/// Gets the singleton instance of the MemoryDataStoreT.
/// </summary>
/// <returns></returns>
public static MemoryDataStoreT<T> GetInstance()
{
if (null == _singletonInstance)
{
_singletonInstance = new MemoryDataStoreT<T>();
}
return _singletonInstance;
}
public void Clear()
{
if(null!=_memoryStore)
{
_memoryStore.Clear();
}
}
}
public class DataStoreTFactory
{
//static, simple factory class
public static IDataStoreT GetAConcrete () //<< does not compile
{
return MemoryDataStoreT<T>.GetInstance(); << yeah, I have no idea how to
give it an actual T
}
}
//Test Code
IDataStoreT ids = DataStoreTFactory.GetAConcrete ();
ids.Clear();