多线程练习 -- 单例设计模式

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
View Code

LWTOnlyoneClass.h

多线程练习 -- 单例设计模式
#import <Foundation/Foundation.h>
#import "Singleton.h"

@interface LWTOnlyoneClass : NSObject

@property (nonatomic, copy) NSString *name;

singleton_h(OnlyoneClass)

@end
View Code

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
View Code

LWTViewController.h

多线程练习 -- 单例设计模式
#import <UIKit/UIKit.h>

@interface LWTViewController : UIViewController

@end
View Code

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
View Code

 

多线程练习 -- 单例设计模式,布布扣,bubuko.com

多线程练习 -- 单例设计模式

上一篇:用spring boot 来创建第一个application


下一篇:在linux下编译opencv for android