objective-C之宏定义实现单例设计模式
2016-07-28 18:39
701 查看
Singleton.h
// 帮助实现单例设计模式 // .h文件的实现 #define SingletonH(methodName) + (instancetype)shared##methodName; // .m文件的实现 #if __has_feature(objc_arc) // 是ARC #define SingletonM(methodName) \ static id _instace = nil; \ + (id)allocWithZone:(struct _NSZone *)zone \ { \ if (_instace == nil) { \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ _instace = [super allocWithZone:zone]; \ }); \ } \ return _instace; \ } \ \ - (id)init \ { \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ _instace = [super init]; \ }); \ return _instace; \ } \ \ + (instancetype)shared##methodName \ { \ return [[self alloc] init]; \ } \ + (id)copyWithZone:(struct _NSZone *)zone \ { \ return _instace; \ } \ \ + (id)mutableCopyWithZone:(struct _NSZone *)zone \ { \ return _instace; \ } #else // 不是ARC #define SingletonM(methodName) \ static id _instace = nil; \ + (id)allocWithZone:(struct _NSZone *)zone \ { \ if (_instace == nil) { \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ _instace = [super allocWithZone:zone]; \ }); \ } \ return _instace; \ } \ \ - (id)init \ { \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ _instace = [super init]; \ }); \ return _instace; \ } \ \ + (instancetype)shared##methodName \ { \ return [[self alloc] init]; \ } \ \ - (oneway void)release \ { \ \ } \ \ - (id)retain \ { \ return self; \ } \ \ - (NSUInteger)retainCount \ { \ return 1; \ } \ + (id)copyWithZone:(struct _NSZone *)zone \ { \ return _instace; \ } \ \ + (id)mutableCopyWithZone:(struct _NSZone *)zone \ { \ return _instace; \ } #endif
使用方法
创建单例类ToolUntils 继承与NSObject
.h
#import <Foundation/Foundation.h> #import "Singleton.h" @interface ToolUntils : NSObject SingletonH(UserTool); @end.m
#import "ToolUntils.h" @implementation ToolUntils SingletonM(UserTool); @end
测试:
#import "ViewController.h" #import "ToolUntils"
@interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; ToolUntils * userTool = [ToolUntils sharedUserTool];}
优点:面向接口编程, 复用性较强. 但由于使用了宏定义,所以编译时存在替换的情况存在,在给其它类或对象命名时,注意不要相同.
下载:Singleton
相关文章推荐
- Effetive Objective - C 1.Objective 9
- netty 第二章 从client 传递Object 对象到server
- 各种细的知识点(二)
- a different object with the same identifier value was already associated with the session:
- Qt之Threads和QObjects
- Qt之Threads和QObjects
- Qt之Threads和QObjects
- Objective-C中一种消息处理方法performSelector: withObject:
- PyGobject(三十五)布局容器之ShortcutsWindow
- DataInputStream && ObjectInputStream 示例
- PyGobject(三十四)布局容器之Assistant
- The type java.lang.Object cannot be resolved.It is indirectly referenced from required .class files。
- PyGobject(三十三)布局容器之ApplicationWindow
- 关于NSMutableArray 调用 addObject 方法崩溃
- PyGobject(三十二)布局容器之Window
- PyGobject(三十一)布局容器之EventBox
- Objective-C-UI控件学习之UICollectionView详解
- [Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- Assigning retained object to weak property object
- java中object文档