C#简单写法如下:

public class Singleton
{
    private static Singleton _instance = null;
    private Singleton(){}
    public static Singleton CreateInstance()
    {
        if(_instance == null)
        {
            _instance = new Singleton();
        }
        return _instance;
    }
}
 

单例模式特点:

单例类只能有一个实例。

单例类必须自己创建自己的唯一实例。

单例类必须给所有其它对象提供这一实例。

相关文章:

  • 2022-12-23
  • 2021-12-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-12
  • 2022-12-23
猜你喜欢
  • 2021-11-29
  • 2021-10-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案