Files
aibis-dream/Assets/Scripts/Framework/PoolKit/Pool/Pool.cs
T
2024-09-19 01:12:10 +08:00

66 lines
1.5 KiB
C#

/*
* 基础对象池
*/
using System;
using System.Collections.Generic;
namespace AibisDream.Kit
{
public abstract class Pool<T> : IPool<T>
{
#region ICountObserverable
/// <summary>
/// Gets the current count.
/// </summary>
/// <value>The current count.</value>
public int CurCount => mCacheStack.Count;
#endregion
protected IObjectFactory<T> mFactory;
public void SetObjectFactory(IObjectFactory<T> factory)
{
mFactory = factory;
}
public void SetFactoryMethod(Func<T> factoryMethod)
{
mFactory = new CustomObjectFactory<T>(factoryMethod);
}
/// <summary>
/// 存储相关数据的栈
/// </summary>
protected readonly Stack<T> mCacheStack = new Stack<T>();
public void Clear(Action<T> onClearItem = null)
{
if (onClearItem != null)
{
foreach (var poolObject in mCacheStack)
{
onClearItem(poolObject);
}
}
mCacheStack.Clear();
}
/// <summary>
/// default is 5
/// </summary>
protected int mMaxCount = 12;
public virtual T Allocate()
{
return mCacheStack.Count == 0
? mFactory.Create()
: mCacheStack.Pop();
}
public abstract bool Recycle(T obj);
}
}