首页 > 代码库 > 多线程练习 -- 单例设计模式
多线程练习 -- 单例设计模式
Singleton.h
#define singleton_h(name) + (instancetype)shared##name;#if __has_feature (objc_arc)#define singleton_m(name) static id _instance; + (id)allocWithZone:(struct _NSZone *)zone { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [super allocWithZone:zone]; }); return _instance; } + (instancetype)sharedOnlyoneClass { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [[self alloc] init]; }); return _instance; }#else#define singleton_m(name) static id _instance; + (id)allocWithZone:(struct _NSZone *)zone { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [super allocWithZone:zone]; }); return _instance; } + (instancetype)shared##name { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [[self alloc] init]; }); return _instance; } - (oneway void)release { } - (id)retain { return _instance; } - (NSUInteger)retainCount { return 1; } - (id)autorelease { return _instance; } + (id)copyWithZone:(struct _NSZone *)zone { return _instance; } + (id)mutableCopyWithZone:(struct _NSZone *)zone { return _instance; }#endif
LWTOnlyoneClass.h
#import <Foundation/Foundation.h>#import "Singleton.h"@interface LWTOnlyoneClass : NSObject@property (nonatomic, copy) NSString *name;singleton_h(OnlyoneClass)@end
LWTOnlyoneClass.m
//// LWTOnlyoneClass.m// 多线程练习 -- 单例设计模式//// Created by apple on 14-6-24.// Copyright (c) 2014年 lwt. All rights reserved.//#import "LWTOnlyoneClass.h"@implementation LWTOnlyoneClass- (instancetype)init{ self = [super init]; if (self) { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ self.name = @"jack"; }); } return self;}singleton_m(OnlyoneClass)@end
LWTViewController.h
#import <UIKit/UIKit.h>@interface LWTViewController : UIViewController@end
LWTViewController.m
//// LWTViewController.m// 多线程练习 -- 单例设计模式//// Created by apple on 14-6-24.// Copyright (c) 2014年 lwt. All rights reserved.//#import "LWTViewController.h"#import "LWTOnlyoneClass.h"@interface LWTViewController ()@end@implementation LWTViewController- (void)viewDidLoad{ [super viewDidLoad]; LWTOnlyoneClass *onlyone1 = [[LWTOnlyoneClass alloc] init]; LWTOnlyoneClass *onlyone2 = [LWTOnlyoneClass sharedOnlyoneClass]; onlyone2.name = @"Tom"; LWTOnlyoneClass *onlyone3 = [[LWTOnlyoneClass alloc] init]; onlyone3.name = @"rose"; LWTOnlyoneClass *onlyone4 = [LWTOnlyoneClass sharedOnlyoneClass]; onlyone4.name = @"jim"; //#if __has_feature (objc_arc)// LWTOnlyoneClass *onlyone5 = [LWTOnlyoneClass sharedOnlyoneClass];// LWTOnlyoneClass *onlyone6 = [LWTOnlyoneClass sharedOnlyoneClass];////#else// LWTOnlyoneClass *onlyone5 = [onlyone2 copy];// LWTOnlyoneClass *onlyone6 = [onlyone3 mutableCopy];//#endif NSLog(@"%p -- %p -- %p -- %p", onlyone1, onlyone2, onlyone3, onlyone4); NSLog(@"%@ -- %@ -- %@ -- %@", onlyone1.name, onlyone2.name, onlyone3.name, onlyone4.name);}@end
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。