1. 构造函数带来的唯一性问题指什么?
- 对于不继承MonoBehaviour的单例模式基类
我们要避免在外部 new 单例模式类对象
例如 (完整单例模式定义在上一节)
public class Main : MonoBehaviour
{
void Start()
{
TestMgr t = new TestMgr();
BaseManager<TestMgr> baseManager = new BaseManager<TestMgr>();
}
}
- 对于继承MonoBehaviour的单例模式基类
由于继承MonoBehaviour的脚本不能通过new创建,因此不用过多考虑
2. 解决构造函数带来的安全问题
解决BaseManager baseManager = new BaseManager();问题
- 父类变成抽象类
解决 TestMgr t = new TestMgr();问题
- 规定继承单例模式基类的类必须显示实现私有无参构造函数
- 但是会出现单例模式基类不能通过new问题创建实例
- 解决办法:在基类中通过反射来调用私有构造函数实例化对象
public abstract class BaseManager<T> where T : class
{
public static T instance;
public static T Instance
{
get
{
if(instance == null)
{
Type type = typeof(T);
ConstructorInfo info = type.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null,Type.EmptyTypes,null);
if(info != null)
{
instance = info.Invoke(null) as T;
}
else
{
Debug.LogError("没有得到对应的无参构造函数");
}
}
return instance;
}
}
}
- 这样就实现了外部不能new单例模式的对象,同时,单例模式的基类通过反射获取到构造函数创建实例