美文网首页
iOS 单例模式

iOS 单例模式

作者: 印林泉 | 来源:发表于2017-03-01 21:14 被阅读19次
    • 单例模式
      保证程序的整个生命周期中,只会创建一个单例类的实例。

    • 应用,适用场景
      轻松实现两个不同类之间的数据通信。

    创建

    //
    //  User.h
    //  LearnSingleton
    //
    //  Created by 印林泉 on 2017/3/1.
    //  Copyright © 2017年 yiq. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface User : NSObject
    
    @property (nonatomic, copy) NSString *username;
    @property (nonatomic, assign) NSInteger age;
    
    + (instancetype)shareUser;
    
    @end
    
    //
    //  User.m
    //  LearnSingleton
    //
    //  Created by 印林泉 on 2017/3/1.
    //  Copyright © 2017年 yiq. All rights reserved.
    //
    
    #import "User.h"
    
    @implementation User
    
    static User *user = nil;
    
    + (instancetype)shareUser {
    //    //互斥锁,消耗资源
    //    @synchronized (self) {
    //        if (!user) {
    //            user = [[self alloc]init];
    //        }
    //    }
        
        //GCD创建单例
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            if (!user) {
                user = [[self alloc]init];
            }
        });
        return user;
    }
    
    + (instancetype)allocWithZone:(struct _NSZone *)zone {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            if (!user) {
                user = [[self alloc]init];
            }
        });
        return user;
    }
    
    - (id)copy {
        return user;
    }
    
    - (id)mutableCopy {
        return user;
    }
    
    - (NSString *)description {
        return [NSString stringWithFormat:@"username: %@, age: %zd", _username, _age];
    }
    @end
    

    存储

    //
    //  LoginView.m
    //  LearnSingleton
    //
    //  Created by 印林泉 on 2017/3/1.
    //  Copyright © 2017年 yiq. All rights reserved.
    //
    
    #import "LoginView.h"
    #import "User.h"
    
    @implementation LoginView
    
    - (void)login {
        User *user = [User shareUser];
        user.username = @"yinlinqvan";
        user.age = 25;
    }
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect {
        // Drawing code
    }
    */
    
    @end
    

    读取

    //
    //  SettingView.m
    //  LearnSingleton
    //
    //  Created by 印林泉 on 2017/3/1.
    //  Copyright © 2017年 yiq. All rights reserved.
    //
    
    #import "SettingView.h"
    #import "User.h"
    
    @implementation SettingView
    
    - (void)about {
        User *user = [User shareUser];
        NSLog(@"%@", user);
    }
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect {
        // Drawing code
    }
    */
    
    @end
    

    应用

    //
    //  ViewController.m
    //  LearnSingleton
    //
    //  Created by 印林泉 on 2017/3/1.
    //  Copyright © 2017年 yiq. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "User.h"
    #import "LoginView.h"
    #import "SettingView.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        LoginView *loginView = [[LoginView alloc] init];
        [loginView login];
        
        SettingView *settingView = [[SettingView alloc]init];
        [settingView about];
    
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    

    程序不被杀死,该对象不变释放。通过这样的单例使用,让所有类共享使用这个单例。用户数据共享,轻松实现两个不同类之间的数据通信。
    可以保证单例类只有一个实例存在,整个系统只需要一个全局对象,有利于协调系统行为。
    用户信息存储,在登录时将整个用户信息获取,存放在本地文件,这些配置数据,由单例对象统一读取,将服务进程中的其他对象通过单例获取配置信息。简化在复杂环境下的配置管理。

    相关文章

      网友评论

          本文标题:iOS 单例模式

          本文链接:https://www.haomeiwen.com/subject/ymrcgttx.html