单例的创建的各种方法

单例设计模式:

单例的写法:

 

1. GCD 实现单例:

@interface Singleton : NSObject

+ (Singleton *)sharedSingleton; <1>

@end

 

/***************************************************************/

 

//Singleton.m

#import "Singleton.h"

@implementation Singleton   

static Singleton *sharedSingleton = nil;<2>

 

+ (Singleton *)sharedSingleton{

    static dispatch_once_t once;<3>

    dispatch_once(&once,^{

        sharedSingleton = [[self alloc] init];<4>

        //dosometing

    });

    return sharedSingleton;<5>

}

 

51. 2. static3. 4. dispatch_once5. 返回在整个应用的生命周期中只会被实例化一次的变量

 

2.标准单例

/*

 开发中 一般写单例类 没有必要写标准单例 只需要写一个 非标准的函数+ sharedSingleton就够了,因为我们创建/获取单例 都是调用函数sharedSingleton

 

 有些时候我项目要求 调用alloc 也要创建出单例 而且 可能会让单例调用 retain/copy、release autorelease 那么这个时候我们就必须要重写 官方的这些方法 保证 不管调用什么函数 始终程序只有一个对象

 */

 

 

//定义静态全局变量

static Singleton * single = nil;

 

+ (Singleton *)sharedSingleton{

    //考虑线程安全

    @synchronized(self){

        if (single == nil) {

            single = [[self alloc] init];

        }

    }

    return single;

}

 

//调用 alloc的时候 会 调用allocWithZone函数

+ (id)allocWithZone:(NSZone *)zone

{

    @synchronized(self) {

        if (single == nil) {

            //创建 对象

            single = [super allocWithZone:zone];

            return single;

        }////确保使用同一块内存地址

    }

    return single; //

}

- (id)copyWithZone:(NSZone *)zone

{

    return self;//返回自己

}

- (id)retain

{

    return self;//确保计数唯一

}

- (NSUInteger)retainCount

{

    return UINT_MAX;      //返回最大值

 

}

//oneway这一般是线程之间通信的接口定义。表示单向的调用

//使用oneway 异步调用 不使用那么是同步调用 可能会阻塞

- (oneway void)release

{

    //do nothing

}

- (id)autorelease

{

    return self;

}

 

高级宏实现单例?

#define DEFINE_SINGLETON_FOR_HEADER(className) \

\

+ (className *)shared##className;

 

DEFINE_SINGLETON_FOR_HEADER(Singleton2)

————————————————————————————————————————————

#define SYNTHESIZE_SINGLETON_FOR_CLASS(classname) \

\

static classname *shared##classname = nil; \

\

+ (classname *)shared##classname \

{ \

@synchronized(self) \

{ \

    if (shared##classname == nil) \

    { \

    shared##classname = [[self alloc] init]; \

    } \

} \

\

return shared##classname; \

} \

\

+ (id)allocWithZone:(NSZone *)zone \

{ \

@synchronized(self) \

{ \

if (shared##classname == nil) \

{ \

    shared##classname = [super allocWithZone:zone]; \

    return shared##classname; \

} \

\

} \

\

    return nil; \

} \

\

- (id)copyWithZone:(NSZone *)zone \

{ \

    return self; \

} \

\

- (id)retain \

{ \

    return self; \

} \

\

- (NSUInteger)retainCount \

{ \

    return UINT_MAX; \

} \

\

- (oneway void)release \

{ \

} \

\

- (id)autorelease \

{ \

return self; \

}

 

SYNTHESIZE_SINGLETON_FOR_CLASS(Singleton2)

ARC 和MRC 的怎么创建单例?

//ARC下或者 mrc  gcd

//单例函数写法

 

+ (id)sharedInstance

{

    static dispatch_once_t once = 0;//保证其block块在应⽤用中只执⾏行⼀一次

    static id _sharedObject = nil;

    dispatch_once(&once, ^{

        _sharedObject = [[self alloc] init];

    });

    return _sharedObject;

}

 

//非arc

+ (Singleton5 *)sharedSingleton{

    static Singleton5 *single = nil;

    //考虑线程安全

    @synchronized(self){

        if (single == nil) {

            single = [[self alloc] init];

        }

    }

    return single;

}

 

posted @ 2015-08-04 21:33  heyuan123  阅读(196)  评论(0编辑  收藏  举报