当前位置:网站首页>Creation mode - singleton mode

Creation mode - singleton mode

2022-06-21 12:11:00 attempt_ to_ do

1. Pattern motivation

For some classes in the system , Only one example is important , for example , There can be multiple print tasks in a system , But there can only be one task at work ; A system can only have one window manager or file system ; A system can only have one timing tool or ID( Serial number ) generator .

How to ensure that a class has only one instance and that the instance is easy to access ? Defining a global variable ensures that objects can be accessed at any time , But it doesn't prevent us from instantiating multiple objects .

A better solution is to have the class itself responsible for keeping its only instance . This class ensures that no other instance is created , And it can provide a way to access the instance . This is the motivation of the singleton model .

2. Pattern definition

The singleton pattern (Singleton Pattern): Singleton pattern ensures that a class has only one instance , And instantiate it yourself and provide it to the entire system , This class is called a singleton class , It provides methods for global access .

There are three main points of the singleton model : One is that a class can only have one instance ; Second, it must create this instance by itself ; Third, it must provide the whole system with this instance by itself . The singleton pattern is an object creation pattern . Singleton mode is also known as singleton mode or singleton mode .

3. Pattern structure

 Insert picture description here

4. Sequence diagram

 Insert picture description here

5. The code analysis

5.1.c++ Realization

#include "Singleton.h"
#include <iostream>
using namespace std;
 
Singleton * Singleton::instance = NULL;
Singleton::Singleton(){
    
 
}
 
Singleton::~Singleton(){
    
    delete instance;
}
 
Singleton* Singleton::getInstance(){
    
    if (instance == NULL)
    {
    
        instance = new Singleton();
    }
 
    return  instance;
}
 
 
void Singleton::singletonOperation(){
    
    cout << "singletonOperation" << endl;
}
#include <iostream>
#include "Singleton.h"
using namespace std;
 
int main(int argc, char *argv[])
{
    
    Singleton * sg = Singleton::getInstance();
    sg->singletonOperation();
 
    return 0;
}

5.2.Golang Realization

package singleton

type singleton map[string]string

var (
    once sync.Once

    instance singleton
)

func New() singleton {
    
    once.Do(func() {
    
        instance = make(singleton)
    })

    return instance
}
s := singleton.New()

s["this"] = "that"

s2 := singleton.New()

fmt.Println("This is ", s2["this"])
// This is that

6. Pattern analysis

The purpose of singleton pattern is to ensure that a class has only one instance , And provide a global access point to access it . The singleton pattern contains only one role , It's a singleton class ——Singleton. Singleton classes have a private constructor , Make sure users can't get through new Keyword instantiates it directly . besides , This pattern contains a static private member variable and a static public factory method , The factory method is responsible for verifying the existence of instances and instantiating itself , Then it's stored in a static member variable , To ensure that only one instance is created .

In the implementation of the singleton pattern , Three points need to be noted :

  • The constructor of the singleton class is private ;
  • Provide a static private member variable of its own ;
  • Provide a public static factory method .

7. Apply to the environment

The singleton mode can be used in the following cases :

  • The system only needs one instance object , If the system requires a unique serial number generator , Or you need to consider that the resource consumption is too large and only one object is allowed to be created .
  • Only one common access point is allowed for a single instance of a client call class , In addition to the public access point , The instance cannot be accessed by any other means .
  • The singleton pattern should be used when a class has only one instance in a system . In turn, , If a class can have several instances coexisting , We need to improve the singleton mode , Make it a multiple model
原网站

版权声明
本文为[attempt_ to_ do]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211159570248.html