iOS单例模式~~以及单例宏定义



//

//  JYDModelLocator.h

//

//  Created by jyd on 15/3/20.

//  Copyright (c) 2015 jyd. All rights reserved.

//

//  数据共享类

#import "JYDUser.h"


//@class JYDUser;


@interface JYDModelLocator : NSObject


@property (nonatomic, copy) NSString *sessionId; 


/**

 * 单例方法

 */

+ (instancetype)sharedInstance;


@end


//

//  JYDModelLocator.m

//

//  Created by jyd on 15/3/20.

//  Copyright (c) 2015 jyd. All rights reserved.

//

//  数据共享类


#import "JYDModelLocator.h"


@implementation JYDModelLocator


/**

 * 单例对象

 */

static JYDModelLocator *_sharedInstance;


/**

 *  IOS中,所有对象的内存空间的分配,最终都会调用allocWithZone方法

 *  如果需要做单例,需要重写此方法

 *  GCD提供了一个方法,专门用来创建单例的

 */

+(instancetype)allocWithZone:(struct _NSZone *)zone

{

    //dispatch_once是线程安全的

    static dispatch_once_t onceToken;

    

    //dispatch_once宏可以保证代码块中的指令只被执行一次

    //在多线程的环境下,永远也只会被执行一次,_sharedInstance只会被实例化一次

    dispatch_once(&onceToken, ^{

        _sharedInstance = [super allocWithZone:zone];

    });

    

    return _sharedInstance;

}


+ (instancetype)sharedInstance

{

    //如果有两个线程实例化,很可能创建出两个实例

    //    if (_sharedInstance == nil) {

    //        _sharedInstance = [[JSModelLocator alloc] init];

    //    }

    //

    //    return _sharedInstance;

    

    return [[self alloc]init];

}


@end


在实际应用中、可能需要定义很多个单列类、这个时候定义一个单列宏的可能更方便


这里采用BeeFramework中的单列宏模板 拿过来使用即可。


如果你觉得BeeFramework中的复杂些 ~~~可以使用本人的单列宏

如下

//

//  JYDSingleton.h

//  Walker2015

//

//  Created by jyd on 15/4/17.

//  Copyright (c) 2015 jyd. All rights reserved.

//


#ifndef Walker2015_JYDSingleton_h

#define Walker2015_JYDSingleton_h


// 单例宏定义


#undef AS_SINGLETON

#define AS_SINGLETON( __class ) \

    - (__class *)sharedInstance; \

    + (__class *)sharedInstance; \


#undef DEF_SINGLETON

#define DEF_SINGLETON( __class ) \

    - (__class *)sharedInstance \

    { \

        return [__class sharedInstance]; \

    } \

    + (__class *)sharedInstance \

    { \

        static dispatch_once_t once; \

        static __class * __singleton__; \

        dispatch_once( &once, ^{ __singleton__ = [[[self class] alloc] init]; \

                                    if ([__singleton__ respondsToSelector:@selector(singletonInitOnce)]) \

                                    { \

                                        [__singleton__ performSelector:@selector(singletonInitOnce)]; \

                                    } \

                                } ); \

        return __singleton__; \

    } \


#endif



再以下是 BeeFramework的源代码 如下:


//

// ______    ______    ______

// /\  __ \  /\  ___\  /\  ___\

// \ \  __<  \ \  __\_ \ \  __\_

// \ \_____\ \ \_____\ \ \_____\

//   \/_____/  \/_____/  \/_____/

//

//

// Copyright (c) 2014-2015, Geek Zoo Studio

// http://www.bee-framework.com

//

//

// Permission is hereby granted, free of charge, to any person obtaining a

// copy of this software and associated documentation files (the "Software"),

// to deal in the Software without restriction, including without limitation

// the rights to use, copy, modify, merge, publish, distribute, sublicense,

// and/or sell copies of the Software, and to permit persons to whom the

// Software is furnished to do so, subject to the following conditions:

//

// The above copyright notice and this permission notice shall be included in

// all copies or substantial portions of the Software.

//

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING

// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS

// IN THE SOFTWARE.

//


#import "Bee_Precompile.h"


#pragma mark -


#if __has_feature(objc_instancetype)


#undef AS_SINGLETON

#define AS_SINGLETON


#undef AS_SINGLETON

#define AS_SINGLETON( ... ) \

- (instancetype)sharedInstance; \

+ (instancetype)sharedInstance;


#undef DEF_SINGLETON

#define DEF_SINGLETON \

- (instancetype)sharedInstance \

{ \

return [[self class] sharedInstance]; \

} \

+ (instancetype)sharedInstance \

{ \

static dispatch_once_t once; \

static id __singleton__; \

dispatch_once( &once, ^{ __singleton__ = [[self alloc] init]; } ); \

return __singleton__; \

}


#undef DEF_SINGLETON

#define DEF_SINGLETON( ... ) \

- (instancetype)sharedInstance \

{ \

return [[self class] sharedInstance]; \

} \

+ (instancetype)sharedInstance \

{ \

static dispatch_once_t once; \

static id __singleton__; \

dispatch_once( &once, ^{ __singleton__ = [[self alloc] init]; } ); \

return __singleton__; \

}


#else // #if __has_feature(objc_instancetype)


#undef AS_SINGLETON

#define AS_SINGLETON( __class ) \

- (__class *)sharedInstance; \

+ (__class *)sharedInstance;


#undef DEF_SINGLETON

#define DEF_SINGLETON( __class ) \

- (__class *)sharedInstance \

{ \

return [__class sharedInstance]; \

} \

+ (__class *)sharedInstance \

{ \

static dispatch_once_t once; \

static __class * __singleton__; \

dispatch_once( &once, ^{ __singleton__ = [[[self class] alloc] init]; } ); \

return __singleton__; \

}


#endif // #if __has_feature(objc_instancetype)


#undef DEF_SINGLETON_AUTOLOAD

#define DEF_SINGLETON_AUTOLOAD( __class ) \

DEF_SINGLETON( __class ) \

+ (void)load \

{ \

[self sharedInstance]; \

}


#pragma mark -


@interface BeeSingleton : NSObject

@end







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值