Unity parent templates in a single embodiment

Reproduced

Finally found the problem before on a single case of parent solution is to use reflection to solve.
Reproduced here to share with you: Unity game framework formwork (b) a single case

text

How to design a single case of this template?

Analysis needs to achieve when designing a manager, we want the whole program has only one object instance of the manager, the general immediately to mind is this:

public class XXXManager 
{
    private static XXXManager instance = null;
 
    private XXXManager
     {
        // to do ...
    }
 
    public static XXXManager()
 {
        if (instance == null)
        {
            instance = new XXXManager();
        }
        return instance;
    }
}

If a player needs 10 kinds of manager, then the above code to copy and paste a lot of times. Repeat the code too much !!! want to duplicate code pulled out, how to do? The answer is the introduction of generics. To achieve the following:

using System;  
using System.Collections.Generic;  
using System.Text;  
using System.Reflection;
 
namespace QFramework 
{  
    public abstract class QSingleton<T> where T : QSingleton<T>
    {
        protected static T instance = null;
 
        protected QSingleton()
        {
        }
 
        public static T Instance()
        {
            if (instance == null)
            {
                // 如何new 一个T???
            }
 
            return instance;
        }
    }
}

To be inherited, static and instance constructors use protect modifier. The above problems are obvious, it is not a new generic (March 9 Added: not new is not a generic reference: new instance of a generic compiler fails, why -CSDN forum -CSDN.NET? - China's largest IT technology community ), (April 5 added: there may be new students say a generic example, but the time required to change generics provide public constructors, well, the reason here is not new, not calling the constructor's private show). Because generics itself is not a type, then how to do it? The answer is to use reflection. To achieve the following:

using System;
using System.Reflection;

namespace QFramework
{
    /// <summary>
    /// 1.泛型
    /// 2.反射
    /// 3.抽象类
    /// 4.命名空间
    /// </summary>
    public abstract class QSingleton<T> where T : QSingleton<T>
    {
        protected QSingleton()
        {

        }

        protected static T instance = null;

        public static T Instance()
        {
            if (instance == null)
            {
                // 先获取所有非public的构造方法
                ConstructorInfo[] ctors = typeof(T).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic);
                // 从ctors中获取无参的构造方法
                ConstructorInfo ctor = Array.Find(ctors, c => c.GetParameters().Length == 0);
                if (ctor == null)
                    throw new Exception("Non-public ctor() not found!");
                // 调用构造方法
                instance = ctor.Invoke(null) as T;
            }
            return instance;
        }
    }
}

Above is finally achieved. This implementation is in any C # programs are common. Which test is as follows:

using QFramework;  
// 1.需要继承QSingleton。
// 2.需要实现非public的构造方法。
public class XXXManager : QSingleton<XXXManager> 
{  
    private XXXManager() 
    {
        // to do ...
    }
}
 
 
public static void main(string[] args)  
{
    XXXManager.Instance().xxxyyyzzz();
}

to sum up

This template singleton is usually used more handy tools, and its implementation is found in other frameworks, brought directly used. Partially reflective it might consume some performance, but only once. In Unity might need to inherit singleton MonoBehaviour, because a lot of games may create only a GameObject, MonoBehaviour used to obtain the life cycle of these elements will be introduced again next lecture :).

PS: My frame QFramework address

Singleton library QSingleton address

Address reprint please specify: sandals notes

Micro-channel public number: liangxiegame

Published 20 original articles · won praise 1 · views 927

Guess you like

Origin blog.csdn.net/f_957995490/article/details/103719818