单例类:ApplicationContext.h文件
#import <Foundation/Foundation.h>
@interface ApplicationContext : NSObject<NSCopying>
@property (nonatomic,copy) NSString *userName;
@property (nonatomic,copy) NSString *passWord;
@property (nonatomic,copy) NSMutableDictionary *dictData;
+(id)shareApplicationContext;
@end
单例类:ApplicationContext.m文件
#import "ApplicationContext.h"
static ApplicationContext *appcationContext =nil;@implementation ApplicationContext
//自定义的一个类方法
+(id)shareApplicationContext{
NSLog(@"share:appcationContext = %@",appcationContext);
//判断当前类的实例是否为空,如果为空了,创建
if(appcationContext == nil){
//实例化当前的类的对象
appcationContext = [[[self class] alloc] init];
return appcationContext;
}
//否则直接返回
return appcationContext;
}
//覆盖父类的alloc方法
+ (id)allocWithZone:(struct _NSZone *)zone{
NSLog(@"alloc:appcationContext = %@",appcationContext);
//判断当前类的实例是否为空,如果为空了,创建
if(appcationContext == nil){
NSLog(@"对象第一次被创建~");
//实例化当前的类的对象
appcationContext = [super allocWithZone:zone];
return appcationContext;
}
//否则直接返回
return appcationContext;
}
//覆盖copy方法,防止copy此对象产生新的实例
- (id)copyWithZone:(NSZone *)zone{
NSLog(@"copy");
return self;
}
//也不让修改引用计数
-(id)retain{
return appcationContext;
}
//永远返回最大的整数
-(NSUInteger)retainCount{
//返回最大整数
return UINT_MAX;
}
//覆盖autorelease方法
-(id)autorelease{
return appcationContext;
}
//覆盖release方法
-(oneway void)release{
}
- (void)dealloc
{
    [_userName release];
    [_passWord release];
    [_dictData release];
    [super dealloc];
}
@end
测试类:main.m文件
#import <Foundation/Foundation.h>
#import "ApplicationContext.h"
#import "Person.h"
#import "Student.h"
int main(int argc, const char * argv[])
{
    @autoreleasepool {
        
        //实例化单例的类
        ApplicationContext *appContext = [ApplicationContext shareApplicationContext];
        appContext.userName =@"admin";
        appContext.passWord =@"123456";
        //先定义一个字典
        NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1",@"k1",@"v2",@"k2", nil];
        [dict setValue:@"v3" forKey:@"k3"];
        //设置字典
        appContext.dictData = dict;
        
//        //实例化单例的类
//        ApplicationContext *appContext2 = [ApplicationContext shareApplicationContext];
//        ApplicationContext *appContext3 = [[ApplicationContext alloc] init];
//        ApplicationContext *appContext4 = [appContext copy];
//        
//        NSLog(@"appContext4 %@",appContext3.userName);
        
        Person *p = [[Person alloc] init];
        p.pid = 1234;
        [p test];
        
        NSLog(@"username : %@",appContext.userName);
        
        
        Student *stu = [[Student alloc] init];
        stu.sid = 45678;
        [stu stuTest];
        
        
    }
    return 0;
}
测试类:Person.h文件
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic,assign) int pid;
-(void)test;
@end
测试类:Person.m文件
#import "Person.h"
#import "ApplicationContext.h"
@implementation Person
-(void)test{
    NSLog(@"person id :%d",_pid);
    ApplicationContext *appContext1 = [[ApplicationContext alloc] init];
    NSLog(@"person name :%@,password = %@",appContext1.userName,appContext1.passWord);
    
    //appContext1.dictData 从单例模式的类中获取的
    NSMutableDictionary *dict2 = appContext1.dictData;
    //重新修改字典的内容
    //[dict2 setValue:@"new" forKey:@"k4"];
    appContext1.userName  = @"person";
    
    NSLog(@"dictdata %@",dict2);
}
@end
测试类:Student.h文件
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property int sid;
-(void)stuTest;
@end
测试类:Student.m文件
#import "Student.h"
#import "ApplicationContext.h"
@implementation Student
-(void)stuTest{
    NSLog(@"stu id :%d",_sid);
    ApplicationContext *appContext = [ApplicationContext shareApplicationContext];
    NSLog(@"student username :%@",appContext.userName);
}
@end
                  
                  
                  
                  
                            
本文介绍了一种使用Objective-C实现单例模式的方法,并通过具体的示例代码展示了如何在实际项目中正确使用单例模式来管理全局状态。文章还探讨了如何避免常见的陷阱,如过度共享资源导致的问题。
          
      
          
                
                
                
                
              
                
                
                
                
                
              
                
                
              
            
                  
					171
					
被折叠的  条评论
		 为什么被折叠?
		 
		 
		
    
  
    
  
            


            