Singleton mode of Unity design mode (non-Mono)

When I was writing Unity to read and write Excel plug-ins, I used a Manager class that needed to use a singleton mode, but I almost forgot how to write a singleton, and only remembered the simplest way of writing. Declare a private static field, a public static property, and then in order to prevent the brains in the team from going to the new singleton, we need to privatize the constructor of the class. The code looks like this:

public class Singleton{
    private static Singleton instance = new Singleton();
    publi static Singleton Instace=>instance;
    private Singleton(){}
} 

But this is very low-level, so let's introduce two more commonly used singleton patterns.

 1. Lazy singleton mode:

In order to reflect the thinking of OOP, we should declare an interface first, and there should be only one Init method in the interface

// 单例接口
public interface ISingleton { void Init(); }

Then declare an abstract generic class, constraint T can only inherit from the ISingleton interface, and then one of the private static fields and one public static property. In the get accessor of the attribute, if the instance is empty, an object cannot be obtained by using Lazy<T>new. Although has been declared, the object will only be created when the T object is used. . This is the core of the lazy man's singleton pattern.

using System;
public interface ISingleton { 
    void Init();
}
public abstract class Singleton<T> where T:ISingleton
{
    private static T? instance;
    public static T Instance
    {
        get {
            if (instance==null)
            {
                instance = new Lazy<T>(true).Value;
                instance.Init();
            }
            return instance;
        }
    }
}

public class SingletonTest : ISingleton
{
    public static SingletonTest Single =>Singleton<SingletonTest>.Instance;
    void ISingleton.Init()
    {     
    }
}

2. Hungry Chinese style singleton mode

The core of the hungry man mode is to create it from the beginning. The code is as follows, and the usage is the same as that of the lazy man's singleton mode.

    public abstract class Singleton_Hunger<T> where T:ISingleton,new(){
    private static T instance = new T();
    public static T Instace {
        get { 
            instance.Init();
            return instance;
        }
    }
}

The difference between the lazy style and the hungry style is that the lazy style will only create an instance when it is initialized, while the hungry style will create an instance from the beginning regardless of whether it will be used or not. As for why an interface is inherited instead of an abstract class or something else, the reason is that the C# language does not allow multiple inheritance. If a function is written as a class, a class that needs this function inherits the parent class. If you want to add other functions to it, you can’t add it, so we hope to be able to form a function into an interface, and inherit this interface if necessary, which is more in line with the design of the framework.

The above is the implementation of Unity's non-Mono singleton.

おすすめ

転載: blog.csdn.net/qq_52690206/article/details/127854697