objective c单例设计模式
来源:互联网 发布:pvg网络视频管理平台 编辑:程序博客网 时间:2024/05/17 02:28
什么是单例?
其实就是C中的全局变量,在整个生命周期中,该对象只有一份存在内存中,可以在多个对象之间共享数据。
典型的单例写法:
static id sharedMyManager;
+ (id) sharedThemeManager
{
if (sharedMyManager == nil)
{
sharedMyManager == [[self alloc] init];
}
return sharedMyManager;
}
单例不要使用alloc去创建。
现在有一个单例的写法:
ThemeManager.h
#import <Foundation/Foundation.h>
@interface ThemeManager : NSObject {
NSString *_name;
}
@property (nonatomic,retain) NSString*name;
+ (id) sharedThemeManager; //sharedXXXX defaultXXXX currentXXXX命名方法
@end
#import "ThemeManager.h"
static ThemeManager *s;
@implementation ThemeManager
@synthesize name = _name;
-(void) dealloc {
_name = nil;
}
+ (id) sharedThemeManager {
if (s == nil)
{ //第一次进入调用,把s存于全局变量中
s = [[[self class] alloc] init];
} //第二次第三次都会使用第一次创建好的变量
return s;
}
- (id) init {
self = [super init];
if (self) {
self.name = [NSString stringWithFormat:@"Defulat" ];
}
return self;
}
-(NSString *) description {
return [NSString stringWithString:self.name];
}
@end
单例的各种写法典型的单例写法:
static id sharedMyManager;
+ (id) sharedThemeManager
{
if (sharedMyManager == nil)
{
sharedMyManager == [[self alloc] init];
}
return sharedMyManager;
}
这种方法有一个缺点:如果两个线程同时调用这个方法,会同时执行if语句,这时就会创建两个对象,会内存泄露。
可以改成加锁的写法:
+ (id) sharedThemeManager
{
@synchronized (self){
if (sharedMyManager == nil)
{
sharedMyManager == [[self alloc] init];
}
}
return sharedMyManager;
}
此方法保证在任何时候,花括号中的代码只能一个线程执行它。
第一次实例化创建
+ (void) initlialze
{
static BOOL initlialzed = NO;
if (initlialzed = NO)
{
initlialzed =YES;
sharedMyManager == [[self alloc] init];
}
}
GCD写法:
+ (id) sharedManager
{
static dispatch_once_t once;
dispatch_once (once& ,^{
sharedMyManager == [[self alloc] init];
});//保证block只能执行一次
return sharedManager;
}
单例全面的写法
需要重载下面五个方法:
+ (id)allocWithZone:(struct _NSZone *)zone;+ (id)copyWithZone:(struct _NSZone *)zone;
+ (id) retain;
+ (void) release;
+ (void) autorelease;
在主程序中尽量不要用alloc和copy方法,如果重载以上方法,即使这样调用,也会实现单例模式,但是尽量避免,尽量使用典型方法。
现在有如下代码供你参考:
ThemeManager.h
#import <Foundation/Foundation.h>
@interface ThemeManager : NSObject{
NSString *_name;
}
@property (nonatomic,retain) NSString*name;
+ (id) sharedThemeManager;//sharedXXXX defaultXXXX currentXXXX命名方法
@end
ThemeManager.m#import "ThemeManager.h"
static ThemeManager *s;
@implementation ThemeManager
@synthesize name = _name;
-(void) dealloc {
//[_name release];
_name = nil;
} /*
+ (id) sharedThemeManager {
@synchronized (self){
if (s == nil) { //第一次进入调用,把s存于全局变量中
s = [[[self class] alloc] init];
}
} //第二次第三次都会使用第一次创建好的变量
return s;
}*/
+ (id) sharedThemeManager {
static dispatch_once_t once;
dispatch_once(&once,^{ if (s == nil) { //第一次进入调用,把s存于全局变量中
s = [[[self class] alloc] init];
}
}); //第二次第三次都会使用第一次创建好的变量
return s;
}
+ (id)allocWithZone:(struct _NSZone *)zone{
return [self sharedThemeManager] ;
}
+ (id) copyWithZone:(struct _NSZone *)zone {
return self;
}
+(id) retain {
return self;
}
+ ( oneway void)release {
}
+ (NSUInteger) retainCount {
return UINT8_MAX;
}
+ (id) autorelease {
return self;
}
- (id) init {
self = [super init];
if(self) {
self.name = [NSString stringWithFormat:@"Defulat" ];
}
return self;
}
-(NSString *) description {
return [NSString stringWithString:self.name];
}
+ (void)initialize { }
@end
- Objective-C 设计模式之单例
- Objective-c中的单例设计模式
- 设计模式,objective-c单例
- Objective-c ;单例设计模式
- Objective-c 单例设计模式
- objective c单例设计模式
- Objective-c 单例设计模式
- Objective-C之单例设计模式
- Objective-c 实现单例设计模式
- Objective-C单例设计模式
- Objective-C中的设计模式之单例模式
- Objective-c 单例设计模式(Singleton)
- IOS-3-Objective-C语言:单例设计模式
- objective-C之宏定义实现单例设计模式
- Objective C--单例模式
- Objective C--单例模式
- Objective C 单例模式
- Objective C--单例模式
- 20140913 N2
- Mac OS使用技巧之九:Mission Control和DIY自己的Dashboard
- Struts简介和原理
- 将一个静态库(.a)链接/打包到动态库中。
- 《unix环境高级编程》 读书笔记 (3)
- objective c单例设计模式
- 词法分析器、语法分析器、语义分析器 的区别以及别人编写的程序。
- Why does uploadify.swf request /false URL ?
- Java开发中的23种设计模式详解
- Qt 编程开发随笔,边开发边记
- Spring 注解@Transactional
- Button实现OnClickListener事件的三种方法
- 互联网企业是时候甩掉你的测试部门啦!(How google test software读书笔记)
- ORM 和 Hibernate