您的位置:首页 > 移动开发 > Objective-C

Objective-C的单例模式如何新建和测试?

2015-12-17 16:42 399 查看


所谓单例模式是一种最简单的设计模式之一,就是程序中一个类只对应着一个实例,在很多编程语言中都有这种模式,比如objective-c编程语言、Java语言。对于objective-c来说,其单例模式是怎样的呢?如何表现呢?今天小编围绕这个问题,整理了一篇相关文章,分享给大家,一起来get新技能吧。

 

单例模式一般用全局静态对象来实现。下面我们通过建立一个生成单例的类SingletonClass,在实现文件中定义各种方法来实现我们的单例模式。

 

1、单例模式一般用全局静态对象来实现,所以我们在SingletonClass.m中定义一个静态全局变量是少不了的:

 

//定义静态全局变量

staticSingletonClass *single = nil;

//定义静态全局变量

staticSingletonClass *single = nil;

 

2、上面的静态变量是定义在实现文件中的,所以是私有的,要想获取该类的实例得有个getInstance方法来获取实例,在给静态变量分配内存空间之前,首先要判断是否已经分配过啦,确保单例,如果分配过了就不分配了。

 

//获取静态全局对象

+(id)getInstance

{

    //如果没有生成对象,则为静态全局变量分配内存

    if (single == nil) {

        single = [[SingletonClass alloc] init];

    }

    return single;

}

//获取静态全局对象

+(id)getInstance

{

    //如果没有生成对象,则为静态全局变量分配内存

    if (single == nil) {

        single = [[SingletonClass alloc]init];

    }

    return single;

}

 

3、为了防止用户通过alloc和new来实例化对象,因此我们要对类方法allcoWithZone进行重写

 

//防止通过alloc或者new来创建新的对象我们要重写allocWithZone

+(id)allocWithZone:(NSZone*)zone

{

    if (single == nil) {

        single = [[super allocWithZone:zone]init];

    }

    return single;

}

//防止通过alloc或者new来创建新的对象我们要重写allocWithZone

+(id)allocWithZone:(NSZone*)zone

{

    if (single == nil) {

        single =[[superallocWithZone:zone]init];

    }

    return single;

}

 

4、为了防止用户把单例进行深浅拷贝,我们需要重写copyWithZone方法和mutableCopyWithZone方法,在重写方法之前我们的单例类必须遵循协议NSCoping和NSMutableCoping协议

 

遵循协议代码如下:

 

@interfaceSingletonClass : NSObject

 

//单例中获取单例对象的方法

+(id)getInstance;

 

//单例测试方法

-(void)singletonFunction;

 

@end

@interfaceSingletonClass:NSObject

 

//单例中获取单例对象的方法

+(id)getInstance;

 

//单例测试方法

-(void)singletonFunction;

 

@end

重写copyWithZone方法

 

 

//为了防止通过copy来创建新的实例我们要重写copyWithZone;

-(id)copyWithZone:(NSZone*)zone

{

    return self;

}

//为了防止通过copy来创建新的实例我们要重写copyWithZone;

-(id)copyWithZone:(NSZone*)zone

{

    return self;

}

重写mutableCopyWithZone方法

 

 

-(id)mutableCopyWithZone:(NSZone*)zone

{

    return self;

}

-(id)mutableCopyWithZone:(NSZone*)zone

{

    return self;

}

 

5、防止用户把创建的单例dealloc,我们需要重写retainCount方法

 

//重写retainCount方法,防止被dealloc,返回最大值

-(NSUInteger)retainCount

{

    return NSUIntegerMax;

}

//重写retainCount方法,防止被dealloc,返回最大值

-(NSUInteger)retainCount

{

    return NSUIntegerMax;

}

 

6、重写release,autorelease, retain方法

 

//重写retain,引用计数不变

-(id) retain

{

    return self;

}

 

//重写release

-(oneway void)release

{

}

 

//重写autorelease

-(id)autorelease

{

    return self;

}

//重写retain,引用计数不变

-(id) retain

{

    return self;

}

 

//重写release

-(oneway void)release

{

}

 

//重写autorelease

-(id)autorelease

{

    return self;

}

 

至此我们的单例模式基本创建完毕,下面开始我们的测试吧;

 

在main函数中的代码如下:

 

//单例模式的测试

SingletonClass*single1 = [SingletonClass getInstance];

SingletonClass*single2 = [SingletonClass new];

SingletonClass*single3 = [[SingletonClass alloc] init];

SingletonClass*single4 = [single1 copy];

SingletonClass*single5 = [single1 mutableCopy];

SingletonClass*single6 = [single1 retain];

[single1release];

 

[single1singletonFunction];

NSLog(@"single_retainCount= %lu", single1.retainCount);

 

//输出地址

NSLog(@"getInstance     single1_P = %p", single1);

NSLog(@"new             single2_P = %p", single2);

NSLog(@"allo            single3_P = %p", single3);

NSLog(@"copy            single4_P = %p", single4);

NSLog(@"mutableCopy     single5_P = %p", single5);

NSLog(@"retain          single6_P = %p", single6);

//单例模式的测试

SingletonClass*single1 = [SingletonClass getInstance];

SingletonClass*single2 = [SingletonClass new];

SingletonClass*single3 = [[SingletonClass alloc]init];

SingletonClass*single4 = [single1copy];

SingletonClass*single5 = [single1mutableCopy];

SingletonClass*single6 = [single1retain];

[single1release];

 

[single1singletonFunction];

NSLog(@"single_retainCount= %lu", single1.retainCount);

 

//输出地址

NSLog(@"getInstance     single1_P = %p", single1);

NSLog(@"new             single2_P = %p", single2);

NSLog(@"allo            single3_P = %p", single3);

NSLog(@"copy            single4_P = %p", single4);

NSLog(@"mutableCopy     single5_P = %p", single5);

NSLog(@"retain          single6_P = %p", single6);

运行结果如下:

 

 

2014-08-0716:04:44.207 Memory[20664:303] singleton Ps: 我是单例模式中得测试方法!!

2014-08-0716:04:44.207 Memory[20664:303] single_retainCount = 18446744073709551615

2014-08-0716:04:44.207 Memory[20664:303] getInstance    single1_P = 0x100204690

2014-08-0716:04:44.208 Memory[20664:303] new            single2_P = 0x100204690

2014-08-0716:04:44.208 Memory[20664:303] alloC           single3_P = 0x100204690

2014-08-0716:04:44.208 Memory[20664:303] copy           single4_P = 0x100204690

2014-08-0716:04:44.209 Memory[20664:303] mutableCopy    single5_P = 0x100204690

2014-08-0716:04:44.209 Memory[20664:303] retain         single6_P = 0x100204690

2014-08-0716:04:44.207 Memory[20664:303]singleton Ps: 我是单例模式中得测试方法!!

2014-08-0716:04:44.207 Memory[20664:303]single_retainCount = 18446744073709551615

2014-08-0716:04:44.207 Memory[20664:303]getInstance    single1_P = 0x100204690

2014-08-0716:04:44.208 Memory[20664:303] new            single2_P = 0x100204690

2014-08-0716:04:44.208 Memory[20664:303]alloC           single3_P = 0x100204690

2014-08-0716:04:44.208 Memory[20664:303]copy           single4_P = 0x100204690

2014-08-0716:04:44.209 Memory[20664:303]mutableCopy    single5_P = 0x100204690

2014-08-0716:04:44.209 Memory[20664:303]retain         single6_P = 0x100204690

单例的地址是不变的。

 

上面是在非ARC模式下得单例模式,那么在ARC模式下我们应如何实现我们的单例模式呢,我们下面就会给出ARC下的单例模式,用下面的方法,因没有重写alloc,copy等方法,通过alloc还是可以给该对象分配一个新对象的,上面是线程不安全的,下面是线程安全的:

 

+(id)sharedSingleton {

     static MySingleton *sharedSingleton = nil;

     static dispatch_once_t onceToken;

     dispatch_once(&onceToken, ^{

         sharedSingleton = [[self alloc] init];

     });

     return sharedSingleton;

 }

 + (id)sharedSingleton {

    static MySingleton *sharedSingleton = nil;

    static dispatch_once_tonceToken;

    dispatch_once(&onceToken, ^{

        sharedSingleton = [[self alloc] init];

    });

    return sharedSingleton;

 }

 

以上就是objective-c编程语言中,单例模式的建立及实现方法,有兴趣的童鞋可以尝试自己实现一下。

相关文章:《Objective-C中NSMutableArray的创建及使用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息