当前位置:网站首页>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

4. Sequence diagram

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
边栏推荐
- 20n10-asemi medium and low voltage MOS tube 20n10
- MySQL-DQL
- 这3个后生,比马化腾、张一鸣还狠
- 理解RESTful架构
- 版本号命名规范
- Anchor of SSD_ Box calculation
- Centos7 upgrade MySQL 5.6.40 to enterprise 5.6.49
- Introduction to common source oscilloscope software and RIGOL oscilloscope upper computer software ns-scope
- Illustrated with pictures and texts -- wechat applet to obtain the user's geographic location information and call Tencent map API to obtain the user's specific location
- ACM. HJ36 字符串加密 ●●
猜你喜欢

MySQL 5.6.49 enterprise version setting password complexity policy

Introduction to common source oscilloscope software and RIGOL oscilloscope upper computer software ns-scope

Is 100W data table faster than 1000W data table query?

i.MX - RT1052 脉宽调制(PWM)

【无标题】
![SSD [target detection]](/img/f5/1a4a9868cddb24fb08db9fae4db290.png)
SSD [target detection]

Quantitative research on heterogeneous communities 4 rate of change with bands

创建型模式 - 单例模式

Knowledge points: several special wiring methods for PCB

【yolov5s目标检测】opencv加载onnx模型在GPU上进行推理
随机推荐
WPF 使用 MAUI 的自绘制逻辑
uniapp中常用到的方法(部分) - 时间戳问题及富文本解析图片问题
Guangdong issues product testing coupons, and consumers also share
Tensorflower使用指定的GPU和GPU显存
Vs code + GCC environment compilation for STM32 development
ACM. HJ36 字符串加密 ●●
请问各位大佬,flink cdc在抽取oracle全量数据之前会加表级排他锁
[Harbin Institute of technology] information sharing for the first and second examinations of postgraduate entrance examination
华为云发布桌面IDE-CodeArts
Anchor of SSD_ Box calculation
STM32開發之 VS Code + gcc環境編譯
matrial3d参数分析
Simulated 100 questions of 2022 safety officer-a certificate examination and online simulated examination
蜜雪冰城(已黑化)
Is 100W data table faster than 1000W data table query?
【综合笔试题】剑指 Offer II 114. 外星文字典
I would like to ask you guys, the flick CDC will add a table level exclusive lock before extracting the full amount of Oracle data
i.MX - RT1052 SPI和 I2C接口
Corrigendum to 138 sets of reference solutions to the real problems of Higher Algebra in 2022
站在数字化风口,工装企业如何“飞起来”