Design pattern - singleton mode (SingLeton)

Singleton mode-creational mode
definition:
ensure that there is only one instance of a class, and provide a global access point for that instance.

Singleton: Constructor access level control realizes the encapsulation of new.

Base implementation (for single thread only)

public class SingLeton
{
    
    
   private static SingLeton instance;//private不希望被外界访问,static是由于下方Instance()静态方法
   private SingLeton(){
    
    }//私有空的实例构造器。类的实例构造器若没有编写编译器会默认添加,
   //若写了会使用已写的。单例中该句作用:屏蔽编译器提供的默认构造器,并将构造器封装,让类的使用者不能调用构造器创建实例
   public static SingLeton Instance()//静态实例属性(只读),为了让外界访问一个类中的方法,第一种方法由于构造器被隐蔽无法用,所以只能用第二个。
    {
    
    
     get
      {
    
    
       if(instance==null)
       {
    
    instance=new SingLeton();//在编译器编译中,上一句和该句中会多出许多指令,如果一个线程中此时处于if判断后的中间语句但还没到创建实例位置,此时另一个线程也在判断,此时还是为空的,所以它也会进入创建实例,这样会导致出现多个实例,不符合单例模式。
       return instance;
      }
    }
}

Why make Instance() a static method?
There are two ways to access the methods in the class
1. New an object of a class, and call the member method through object. method name ().
For example, a class People, now need to call the ShowName method
People student=new People();
student.ShowName();
2. Call the corresponding class method through the method of class name.method name()
In the singleton, because The constructor is encapsulated and cannot be called. So you can't use the first method to call the method in the class, you can only use the second method.

Parameterized SingLeton, multi-threaded parameter implementation is the same as single-threaded

public class SingLeton
{
    
    
    private static SingLeton instance;
    private SingLeton(int x,int y)
    {
    
    this.x=x;this.y=y;}
   public static SingLeton GetInstace(int x,int y)//方法传参
   {
    
    
       if(Instance==null)
         instace=new SingLeton(x,y);
       else//可以实现参数更新
        {
    
    instance.x=x;instance.y=y;}
       return instance;
   }
   int x;int y;
}

Use
SingLeton t1=SingLeton.GetInstance(100,200);

insert image description here
ICloneable instance cloning

Singleton in multi-threading [Double Check DoubleCheck]
insert image description here
volatile: Guarantee that the compiler will not fine-tune the code here [when the compiler compiles, the code order will be fine-tuned]

Multi-threaded Singleton [concise version] - suitable for multi-threaded no-parameter singleton mode

class Singleton
{
    
    
   public static readonly Singleton Instance=new Singleton();//内联初始化,生成同时进行初始化
   private Singleton(){
    
    };
}
//等价于
//class Singleton
//{
    
    
   //public static readonly Singleton Instance;
   //static Singleton()//静态构造器,保证多线程下,仅有一个线程执行静态构造器。执行时间,只在静态字段初始化之前初始化。静态构造器,不支持参数,不允许外界调用
    //{
    
    
       // Instace=new Singleton();
   // }
  // private Singleton(){}
//}

Summary: Singleton mode
1. Ensure that the constructor is private
2. The only instance in the world, which cannot be rewritten

Extended
insert image description here
static member variables belong to the entire class, are initialized only when the class is first loaded, and are recycled when the class is destroyed.
Similar to static properties, static methods belong to the entire class, and non-static members cannot be accessed in static methods, because non-static members are instantiated with the creation of objects, and when static methods are called, there may be no instances Objects that are non-static cannot access non-static members.
Static classes cannot be inherited, implemented such as interfaces, polymorphism, and cannot be delayed.

Guess you like

Origin blog.csdn.net/weixin_51565051/article/details/131269581