OC中的单例模式

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

单例模式

单例模式就是,类的对象成为系统中唯一的实例,并提供一个访问点,供客户共享资源。就是在内存中开辟一块空间,大家都能通过访问点进行访问。

单例模式设计的重点

1)某个类只能有一个实例

2)必须自行创建这个对象

3)必须自行向整个系统提供实例

4)这个方法必须是一个类方法(静态方法)

5)为保证实例的唯一性,必须实现以下方法

	-(id)copyWithZone:(NSZone *)zone;//(需要遵守NSCopying协议)
   	+(id)allocWithZone:(struct _NSZone *)zone;
   	-(id)retain;
   	-(NSUInteger)retainCount;
  	-(oneway void)release;
   	-(id)autorelease;

单例模式代码实现

声明单例类,并声明访问点

#import <Foundation/Foundation.h>

@interface SingletonTools : NSObject <NSCopying>

//声明属性
@property(nonatomic,assign) int num;
@property(nonatomic,copy) NSString * str;

//声明访问点
+(instancetype)shareInstances;

@end
实现单例类,并实现访问点,在访问点类方法中,判断当前单例对象是否为空,如果为空则创建单例对象,如果不为空,则返回当前单例对象,从而保证单例对象的唯一性。

//定义一个全局变量
static SingletonTools *instances = nil;

@implementation SingletonTools

//实现访问点
+(instancetype)shareInstances{

    //保证对象的唯一
    if (instances == nil) {
        
        //创建对象
        instances = [[SingletonTools alloc] init];
        return instances;
    }
    return instances;
    
}

@end
在Person类的对象方法 run 中调用访问点创建单例并给成员变量赋值

#import "Person.h"
#import "SingletonTools.h"
@implementation Person
-(void)run{

    //创建单例对象
    SingletonTools *tools = [SingletonTools shareInstances];
    
    //赋值
    tools.num = 250;
    tools.str = @"我不是字符串";
    
}
@end

在Student类的对象方法run中,调用访问点创建单例对象,并且打印单例对象的成员变量的值
#import "Student.h"
#import "SingletonTools.h"
@implementation Student
-(void)run{

    //定义单例对象
    SingletonTools *tools = [SingletonTools shareInstances];
    
    //获取单例对象的属性值
    NSLog(@"%d\t\t%@",tools.num,tools.str);
}
@end

然后我们在main函数中进行调用

#import <Foundation/Foundation.h>
#import "Student.h"
#import "Person.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
       
        Person *p = [[Person alloc]init];
        
        //赋值
        [p run];
        
        Student *s = [[Student alloc]init];
        
        //取值(获取p对象的run方法中赋的值)
        [s run]; //250		我不是字符串
        
    }
    return 0;
}

逻辑解析

p 调用 run 方法时,在 run 方法的内部,调用了 SingletonTools类的shareInstances 类方法,创建对象

此时全局变量 instances==nil 所以创建了一个 instances对象并返回

然后给返回的对象的成员变量赋值

s 调用 run 方法时,在 run 方法内部,也调用  SingletonTools 类的 shareInstances 类方法,创建对象

但是,此时的全局变量 instances 不等于 nil 了,所以直接返回当前的 instances

然后打印出返回的对象的成员变量的值

备注

单例模式必须实现的方法的代码实现

   -(id)copyWithZone:(NSZone *)zone;(需要遵守NSCopying协议)

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

    return self;
}

   +(id)allocWithZone:(struct _NSZone *)zone;

+(id)allocWithZone:(struct _NSZone *)zone{

    @synchronized(self){
    
        if (instances == nil) {
            
            instances = [super allocWithZone:zone];
            return instances;
        }
    }
    return instances;

}

  -(id)retain;

-(id)retain{

    return self;
}

   -(NSUInteger)retainCount;

-(NSUInteger)retainCount{

    return NSUIntegerMax;
}

   -(oneway void)release;

-(oneway void)release{

    
}

   -(id)autorelease;

-(id)autorelease{

    return self;
}















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值