对象工厂创建--- Singleton Object,SingleCall Object,Pool Object
对象分类
单例对象 Singleton Object,单例对象意指对象一旦产生就负责服务所有的系统要求,不会因为要求数量而产生一个以上的对象,这种对象通常要处理Thread同步化的问题。
SingCall Object 意指该对象为了服务单一要求所产生,当要求完成后,该对象就会被释放,在SingCall的模式下的要求数量和对象的数量成正比,在这种模式中,因为每个要求都由独立的对象处理,所以不需要考虑Thread同步问题。
Pool Object是由SingleCall演变而来,与SingleCall不同的是Pool并不会无限制地针对 要求数量而产生对等数量的对象,而取代的是一个Pool池,在其中保持一定的数量。一般来说,池对象会创建特定数量的对象放在池中,当某要求到达时,Pool由池中取出一个对象来服务此要求,当要求完成后,对象并不会立即消灭,而是重新放回Pool池中,通过这种机制来减少对对象数量及加快对象的创建操作,由于维持了一个对象服务一个要求的行为,所以此模式下也不需要处理Thread同步的问题。
对象工厂设计类图
定义对象工厂接口IObjectFactory
public interface IObjectFactory
{
/// <summary>
/// acquire a object.创建一个对象
/// </summary>
/// <param name="typeName">object type name. 对象类型名称</param>
/// <returns>object 对象</returns>
object AcquireObject(string typeName);
/// <summary>
/// acquire a object.创建一个对象
/// </summary>
/// <param name="type">object Type 对象类型</param>
/// <returns>object对象</returns>
object AcquireObject(Type type);
/// <summary>
/// release a object.释放一个对象
/// </summary>
/// <param name="obj">a object to releasing 要释放的对象</param>
void ReleaseObject(object obj);
}
单例对象工厂:继承对象工厂接口
/// <summary>
/// 单例对象工厂:继承对象工厂接口
/// </summary>
public sealed class SingletonObjectFactory : IObjectFactory
{
/// <summary>
/// 对象存储体(hashtable)
/// </summary>
private static Hashtable _cachedObjects = null;
/// <summary>
/// 对象锁
/// </summary>
private static object _lock = new object();
/// <summary>
/// 对象私有属性
/// </summary>
private Hashtable CachedObjects
{
get
{
lock (_lock)
{
if (_cachedObjects == null)
{
_cachedObjects = new Hashtable();
}
return _cachedObjects;
}
}
}
#region IObjectFactory 成员
/// <summary>
/// 根据类型名创建一个单例对象
/// </summary>
/// <param name="typeName">类型名</param>
/// <returns>对象</returns>
public object AcquireObject(string typeName)
{
return AcquireObject(TypeResolverHelper.ResolveType(typeName));
}
/// <summary>
/// 创建一个对象
/// </summary>
/// <param name="type">对象类型</param>
/// <returns>对象</returns>
public object AcquireObject(Type type)
{
if (CachedObjects.Contains(type))
return CachedObjects[type];
lock (_lock)
{
CachedObjects.Add(type, Activator.CreateInstance(type, false));
return CachedObjects[type];
}
}
/// <summary>
/// 回收对象资源
/// </summary>
/// <param name="obj">对象</param>
public void ReleaseObject(object obj)
{
//do nothing because .net对托管对象自动回收
}
#endregion
}
SingleCallObject工厂
public class SingleCallObjectFactory : IObjectFactory
{
#region IObjectFactory 成员
/// <summary>
/// 根据类型名创建一个普通对象
/// </summary>
/// <param name="typeName">类型名</param>
/// <returns>对象</returns>
public object AcquireObject(string typeName)
{
return AcquireObject(TypeResolverHelper.ResolveType(typeName));
}
/// <summary>
/// 创建一个对象
/// </summary>
/// <param name="type">对象类型</param>
/// <returns>对象</returns>
public object AcquireObject(Type type)
{
object obj = Activator.CreateInstance(type, false);
return obj;
}
/// <summary>
/// 回收对象资源
/// </summary>
/// <param name="obj">对象</param>
public void ReleaseObject(object obj)
{
if (obj != null)
{
if (obj is IDisposable)
((IDisposable)obj).Dispose();
}
}
#endregion
}
池对象工厂
public sealed class PoolObjectFactory : IObjectFactory, IDisposable
{
/// <summary>
/// 池数据
/// </summary>
class PoolData
{
/// <summary>
/// 是否在使用
/// </summary>
public bool InUse = false;
/// <summary>
/// 对象
/// </summary>
public object obj;
}
/// <summary>
/// 存储体
/// </summary>
private IList _storage;
/// <summary>
/// 最大限制
/// </summary>
private int _max = 100;
/// <summary>
/// 是否限制
/// </summary>
private bool _limit = false;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="max">最大数</param>
/// <param name="limit">是否限制</param>
/// <param name="storage"></param>
public PoolObjectFactory(int max, bool limit, IList storage)
{
_max = max;
_limit = limit;
_storage = storage;
}
/// <summary>
/// 构造函数
/// </summary>
public PoolObjectFactory()
{
}
/// <summary>
/// 对象数据属性
/// </summary>
/// <param name="obj">对象</param>
/// <returns>对象数据</returns>
private PoolData GetPoolData(object obj)
{
lock (_storage.SyncRoot)
{
for (int i = 0; i < _storage.Count; i++)
{
PoolData p = (PoolData)_storage[i];
if (p.obj == obj)
return p;
}
}
return null;
}
/// <summary>
/// 对象属性
/// </summary>
/// <param name="type">对象类型</param>
/// <returns>对象</returns>
private object GetObject(Type type)
{
lock (_storage.SyncRoot)
{
if (_storage.Count > 0)
{
if (((PoolData)_storage[0]).obj.GetType() != type)
throw new Exception(
string.Format("the Pool Factory only for Type :{0}", _storage[0].GetType().Name));
}
for (int i = 0; i < _storage.Count; i++)
{
PoolData p = (PoolData)_storage[i];
if (!p.InUse)
{
p.InUse = true;
return p.obj;
}
}
if (_storage.Count > _max && _limit)
throw new Exception("max limit is arrived.");
object obj = TypeFactory.CreateInstance(type);
PoolData p1 = new PoolData();
p1.InUse = true;
p1.obj = obj;
_storage.Add(p1);
return obj;
}
}
/// <summary>
/// 设置对象数据为不可用
/// </summary>
/// <param name="obj">对象</param>
private void PutObject(object obj)
{
PoolData p = GetPoolData(obj);
if (p != null)
p.InUse = false;
}
#region IObjectFactory 成员
/// <summary>
/// 根据对象名称在对象池创建一个对象池
/// </summary>
/// <param name="typeName">对象名</param>
/// <returns>对象</returns>
public object AcquireObject(string typeName)
{
return GetObject(TypeResolverHelper.ResolveType(typeName));
}
/// <summary>
/// 根据对象类型在对象池创建一个对象池
/// </summary>
/// <param name="type">对象类型</param>
/// <returns>对象</returns>
public object AcquireObject(Type type)
{
return GetObject(type);
}
/// <summary>
/// 释放对象
/// </summary>
/// <param name="obj">对象</param>
public void ReleaseObject(object obj)
{
if (_storage.Count > _max)
{
if (obj is IDisposable)
((IDisposable)obj).Dispose();
PoolData p = GetPoolData(obj);
lock (_storage.SyncRoot)
_storage.Remove(p);
return;
}
PutObject(obj);
}
#endregion
#region IDisposable 成员
/// <summary>
/// 释放非托管资源
/// </summary>
public void Dispose()
{
lock (_storage.SyncRoot)
{
for (int i = 0; i < _storage.Count; i++)
{
PoolData p = (PoolData)_storage[i];
if (p.obj is IDisposable)
((IDisposable)p.obj).Dispose();
}
}
}
#endregion
}