首页 > 编程语言 > 详细

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

时间:2014-06-26 16:39:44      阅读:320      评论:0      收藏:0      [点我收藏+]

Singleton.h

bubuko.com,布布扣
#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

bubuko.com,布布扣
#import <Foundation/Foundation.h>
#import "Singleton.h"

@interface LWTOnlyoneClass : NSObject

@property (nonatomic, copy) NSString *name;

singleton_h(OnlyoneClass)

@end
View Code

LWTOnlyoneClass.m

bubuko.com,布布扣
//
//  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

bubuko.com,布布扣
#import <UIKit/UIKit.h>

@interface LWTViewController : UIViewController

@end
View Code

LWTViewController.m

bubuko.com,布布扣
//
//  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

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

原文:http://www.cnblogs.com/wentianblog/p/3808822.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!