谈一谈iOS单例模式

2025-05-29 0 14

单例模式是一种常用的软件设计模式。在它的核心结构中只包含一个被称为单例的特殊类。通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源。如果希望在系统中某个类的对象只能存在一个,单例模式是最好的解决方案。

1、书写步骤

1)、创建类方法,返回对象实例.以shared default current开头。
2)、创建一个全局变量用来保存对象的引用
3)、判断对象是否存在,若不存在,创建对象

2、具体单例模式的几种模式

第一种单例模式

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
//非线程安全写法

static UserHelper * helper = nil;

+ (UserHelper *)sharedUserHelper {

if (helper == nil) {

helper = [[UserHelper alloc] init];

}

return helper;

}

第二种单例模式

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21
//线程安全写法1

static UserHelper * helper = nil;

+ (UserHelper *)sharedUserHelper {

@synchronized(self) {

if (helper == nil) {

helper = [[UserHelper alloc] init];

}

}

return helper;

}

第三种单例模式

?

1

2

3

4

5

6

7

8

9

10

11
+ (void)initialize {

if ([self class] == [UserHelper class]) {

helper = [[UserHelper alloc] init];

}

}

第四种单例模式

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21
//线程安全写法3(苹果推荐,主要用这个)

static UserHelper * helper = nil;

+ (UserHelper *)sharedUserHelper {

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

helper = [[UserHelper alloc] init];

});

return helper;

}

MRC全面实现单例写法(了解)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325
#import <Foundation/Foundation.h>

#import "UserHelper.h"

void func() {

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

NSLog(@"haha");

});

}

int main(int argc, const char * argv[]) {

@autoreleasepool {

// [UserHelper logout];

if ([UserHelper isLogin]) {

UserHelper * helper = [UserHelper sharedUserHelper];

NSLog(@"username = %@ password = %@",helper.userName,helper.password);

} else {

char name[20];

char pwd[20];

NSLog(@"请输入用户名");

scanf("%s",name);

NSLog(@"请输入密码");

scanf("%s",pwd);

NSString * userName = [[NSString alloc] initWithUTF8String:name];

NSString * password = [[NSString alloc] initWithUTF8String:pwd];

if (userName && password) {

[UserHelper loginWithUserName:userName password:password];

UserHelper * helper = [UserHelper sharedUserHelper];

NSLog(@"username = %@ password = %@",helper.userName,helper.password);

}

}

// UserHelper * help1 = [UserHelper sharedUserHelper];

// help1.userName = @"dahuan";

// help1.password = @"123456";

// NSLog(@"%p",help1);

// NSLog(@"%@",help1.userName);

// NSLog(@"%@",help1.password);

//

//

// UserHelper * help2 = [UserHelper sharedUserHelper];

// help2.password = @"zxc";

// NSLog(@"%p",help2);

// NSLog(@"%@",help1.userName);

// NSLog(@"%@",help1.password);

}

return 0;

}

//class.h

#import <Foundation/Foundation.h>

@interface UserHelper : NSObject

//1、创建类方法,返回对象实例 shared default current

+ (UserHelper *)sharedUserHelper;

@property (nonatomic, copy) NSString * userName;

@property (nonatomic, copy) NSString * password;

+ (BOOL)isLogin;

+ (void)loginWithUserName:(NSString *)userName password:(NSString *)password;

+ (void)logout;

@end

// class.m

#import "UserHelper.h"

//2、创建一个全局变量

#define Path @"/Users/dahuan/Desktop/data"

static UserHelper * helper = nil;

@implementation UserHelper

//+ (void)initialize {

//

// if ([self class] == [UserHelper class]) {

// helper = [[UserHelper alloc] init];

// }

//}

+ (UserHelper *)sharedUserHelper {

//3、判断对象是否存在,若不存在,创建对象

//线程安全

// @synchronized(self) {

//

// if (helper == nil) {

// helper = [[UserHelper alloc] init];

// }

// }

//gcd 线程安全

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

helper = [[UserHelper alloc] init];

});

return helper;

}

- (instancetype)init {

if (self = [super init]) {

NSString * data = [NSString stringWithContentsOfFile:Path encoding:NSUTF8StringEncoding error:nil];

if (data) {

NSArray * array = [data componentsSeparatedByString:@"-"];

_userName = array[0];

_password = array[1];

}

}

return self;

}

+ (BOOL)isLogin {

UserHelper * helper = [UserHelper sharedUserHelper];

if (helper.userName && helper.password) {

return YES;

}

return NO;

}

+ (void)loginWithUserName:(NSString *)userName password:(NSString *)password {

UserHelper * helper = [UserHelper sharedUserHelper];

helper.userName = userName;

helper.password = password;

NSArray * array = @[userName,password];

NSString * data = [array componentsJoinedByString:@"-"];

[data writeToFile:Path atomically:YES encoding:NSUTF8StringEncoding error:nil];

}

+ (void)logout {

NSFileManager * fm = [NSFileManager defaultManager];

if ([fm fileExistsAtPath:Path]) {

[fm removeItemAtPath:Path error:nil];

}

}

@end

以上就是关于iOS单例模式的全部内容,希望对大家的学习有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 谈一谈iOS单例模式 https://www.kuaiidc.com/93408.html

相关文章

发表评论
暂无评论