标签:style class blog code http tar
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
多线程练习 -- 单例设计模式,布布扣,bubuko.com
标签:style class blog code http tar
原文地址:http://www.cnblogs.com/wentianblog/p/3808822.html