当前位置:网站首页>Singleton mode
Singleton mode
2022-07-25 09:37:00 【Hills and valleys】
List of articles
Chapter one OC The singleton model
Chapter two OC Object initialization of
The third chapter OC And agreements
Chapter four OC And main Function operation and some basic concepts
Preface
Tips : In this article, we talk about singleton mode
- Concept
- establish
One 、 Some concepts about singleton
Definition :
If a class can always create only one instance , Then this class is called a singleton class
Why use singleton mode ?
Sometimes we need a global object , And we should ensure that there is only one copy of the overall situation , At this time, we need to use the singleton design pattern , We need to pay attention to : Protect threads in a multithreaded environment .
The structure of singleton pattern :
Singleton class : A class that contains an instance and can create it by itself
Access class : Classes that use singletons
Two 、static
Before creating the singleton pattern , So let's see static keyword
static Keywords can only modify local variables, global variables and functions
static Decorating a local variable means that the local variable is stored in the static area
Decorating a global variable means that the global variable can only be accessed in the current file
Decorating functions are used to restrict functions to be used only in the current source file
characteristic
Memory storage area : The static memory storage area exists during the operation of the project engineering program
advantage :
Global variables
Will not be remotely accessed by other branch files , modify
And... Can be used in other documents static The same fields decorated by keywords , No conflict
local variable
Scope is still local , When the static local variable symbol or local function statement block is defined , The scope then ends 、
function
Static functions cannot be called by other files in the project
3、 ... and 、 establish
Common creation
Here is the definition of class
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface FKSingleton : NSObject
+ (id)instance;
@end
NS_ASSUME_NONNULL_END
test :
#import<Foundation/Foundation.h>
#import"FKSingleton.h"
@implementation FKSingleton
static id instance = nil;
+ (id)instance {
if (!instance) {
instance = [[super alloc]init];
}
return instance;
}
@end
int main(){
@autoreleasepool {
if([FKSingleton instance] == [FKSingleton instance]){
NSLog(@"1");
} else{
NSLog(@"0");
}
}
}
Code run results
return 1 It indicates that the instances created twice are equal
We control instance Class method to get FKSingleton When an instance , The program can only produce one instance at most , stay main When this instance is called in the function , You can see that the object generated twice is an object .
Use dispatch_once
The realization of the class
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface FKSingleton : NSObject
+ (id)instance;
@end
NS_ASSUME_NONNULL_END
test :
#import<Foundation/Foundation.h>
#import"FKSingleton.h"
@implementation FKSingleton
static id instance = nil;
+ (id)instance2{
static dispatch_once_t onceToken;
dispatch_once(&onceToken,^{
instance = [[self alloc]init];
});
return instance;
}
@end
int main(){
@autoreleasepool {
if([FKSingleton instance2] == [FKSingleton instance2]){
NSLog(@"1");
} else{
NSLog(@"0");
}
}
}
result :
边栏推荐
猜你喜欢

为什么要使用JSON.stringify()和JSON.parse()

OC--Foundation--字符串+日期和时间

文件--初识

How to obtain location information (longitude and latitude) by uni app

main函数的一些操作

Publish Yum private server using nexus3 (offline intranet)

What are stand-alone, cluster and distributed?

语音聊天app源码-钠斯网络源码出品

【代码源】每日一题 算的我头都大啦

matplotlib数据可视化三分钟入门,半小时入魔?
随机推荐
Go foundation 4
【代码源】每日一题 分割(nlogn&n解法)
Go基础3
Redis list structure command
[GYCTF2020]Node Game
TCP network application development process
[GKCTF 2021]easynode
关于C和OC
换电脑后如何配置SSH
How to deploy the jar package to the server? Note: whether the startup command has nohup or not has a lot to do with it
What are stand-alone, cluster and distributed?
*6-2 CCF 2015-03-3 节日
[GYCTF2020]Ez_ Express
Week summary
自定义 view 实现兑奖券背景[初级]
Jar包在阿里云服务器起起来了,安全组也开通了,但postman仍跑不通怎么办
Android 如何使用adb命令查看应用本地数据库
How to configure SSH after changing the computer
学习 Redis linux 安装Redis
Thick willow dustpan, thin willow bucket, who hates reptile man? Asynchronous synergism, half a second to strip away a novel