美文网首页
iOS单例的应用

iOS单例的应用

作者: 随心随缘不随便 | 来源:发表于2018-01-13 18:29 被阅读9次

单例设计模式的基本步骤:

  • 声明一个单件对象的静态实例,并初始化为nil
  • 创建一个类的类工厂方法,当且仅当这个类的实例为nil时生成一个该类的实例
  • 实现 NScopying 协议, 覆盖 allocWithZone: 方法,确保用户在直接分配和初始化对象时,不会产 生另一个对象。
  • 覆盖 releaseautoreleaseretainretainCount 方法, 以此确保单例的状态。
  • 在多线程的环境中,注意使用 @synchronized 关键字或 GCD,确保静态实例被正确的创建和初始化。

MRC写法

    #import "SoundTool.h"
    
    static SoundTool * _instance = nil;
    
    @implementation SoundTool
    /**
     * alloc方法内部会调用allocWithZone
     * zone:系统分配给app的内存
     */
    + (instancetype)allocWithZone:(struct _NSZone *)zone{
        if (_instance == nil) {
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                _instance = [super allocWithZone:zone];
            });
        }
        return _instance;
    }
    
    - (instancetype)init{
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _instance = [super init];
        });
        return _instance;
    }
    
    + (instancetype)shareSoundTool{
        return [[self alloc] init];
    }
    
    - (oneway void)release{
        
    }
    
    - (instancetype)retain{
        return self;
    }
    
    - (NSUInteger)retainCount{
        return 1;
    }
    
    + (id)copyWithZone:(struct _NSZone *)zone{
        return _instance;
    }
    
    + (id)mutableCopyWithZone:(struct _NSZone *)zone{
        return _instance;
    }
    
    @end

ARC写法

    #import "SoundTool.h"
    
    static SoundTool * _instance = nil;
    
    @implementation SoundTool
    /**
     * alloc方法内部会调用allocWithZone
     * zone:系统分配给app的内存
     */
    + (instancetype)allocWithZone:(struct _NSZone *)zone{
        if (_instance == nil) {
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                _instance = [super allocWithZone:zone];
            });
        }
        return _instance;
    }
    
    - (instancetype)init{
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _instance = [super init];
        });
        return _instance;
    }
    
    + (instancetype)shareSoundTool{
        return [[self alloc] init];
    }
        
    + (id)copyWithZone:(struct _NSZone *)zone{
        return _instance;
    }
    
    + (id)mutableCopyWithZone:(struct _NSZone *)zone{
        return _instance;
    }
    
    @end

若需要创建多个单例可将其写成宏

使用以下语法来区分 ARCMRC

#if __has_feature(objc_arc) //ARC
#else //MRC
#endif

代码如下:

    //
    //  Singleton.h
    //  LantaiyuanBus
    //
    //  Created by lantaiyuan on 16/9/7.
    //  Copyright © 2016年 youmy. All rights reserved.
    //
    
    // .h文件的实现
    #define SingletonH(methodName) + (instancetype)shared##methodName;
    
    // .m文件的实现
    #if __has_feature(objc_arc) // 是ARC
    #define SingletonM(methodName) \
    static id _instace = nil; \
    + (id)allocWithZone:(struct _NSZone *)zone \
    { \
    if (_instace == nil) { \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
    _instace = [super allocWithZone:zone]; \
    }); \
    } \
    return _instace; \
    } \
    \
    - (id)init \
    { \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
    _instace = [super init]; \
    }); \
    return _instace; \
    } \
    \
    + (instancetype)shared##methodName \
    { \
    return [[self alloc] init]; \
    } \
    + (id)copyWithZone:(struct _NSZone *)zone \
    { \
    return _instace; \
    } \
    \
    + (id)mutableCopyWithZone:(struct _NSZone *)zone \
    { \
    return _instace; \
    }
    
    #else // 不是ARC
    
    #define SingletonM(methodName) \
    static id _instace = nil; \
    + (id)allocWithZone:(struct _NSZone *)zone \
    { \
    if (_instace == nil) { \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
    _instace = [super allocWithZone:zone]; \
    }); \
    } \
    return _instace; \
    } \
    \
    - (id)init \
    { \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
    _instace = [super init]; \
    }); \
    return _instace; \
    } \
    \
    + (instancetype)shared##methodName \
    { \
    return [[self alloc] init]; \
    } \
    \
    - (oneway void)release \
    { \
    \
    } \
    \
    - (id)retain \
    { \
    return self; \
    } \
    \
    - (NSUInteger)retainCount \
    { \
    return 1; \
    } \
    + (id)copyWithZone:(struct _NSZone *)zone \
    { \
    return _instace; \
    } \
    \
    + (id)mutableCopyWithZone:(struct _NSZone *)zone \
    { \
    return _instace; \
    }
    
    #endif

用法:

.h 文件

#import <Foundation/Foundation.h>
#import "Singleton.h"
@interface VideoTool : NSObject
SingletonH(VideoTool)
@end

.m 文件

#import "VideoTool.h"
@implementation VideoTool
SingletonM(VideoTool)
@end

相关文章

  • 单例

    iOS单例模式iOS之单例模式初探iOS单例详解

  • iOS 基础02--单例、属性修饰符、深浅拷贝

    iOS 基础02--单例、属性修饰符、深浅拷贝 单例 讲单例就必须得先讲讲单例这种设计模式的作用和最可能出现的应用...

  • iOS 单例模式

    关于单例模式的详解,看完这几篇,就完全了然了。iOS 单例模式iOS中的单例模式iOS单例的写法

  • 学习笔记:iOS系统的一些单例类

    iOS系统的单例类:UIApplication(应用程序实例)NSNotificationCenter(消息中心)...

  • (IOS)单例

    概述 单例模式在IOS中的应用非常广泛,如[NSNotificationCenter defaultCenter]...

  • iOS知识梳理3:设计模式

    iOS有哪些常见的设计模式?单例模式/委托模式/观察者模式/MVC模式 单例模式 单例保证了应用程序的生命周期内仅...

  • iOS单例的应用

    单例设计模式的基本步骤: 声明一个单件对象的静态实例,并初始化为nil。创建一个类的类工厂方法,当且仅当这个类的实...

  • iOS开发 单例使用问题

    iOS开发 单例使用问题 iOS开发 单例使用问题

  • iOS开发 单例使用问题

    iOS开发 单例使用问题 iOS开发 单例使用问题

  • iOS单例

    1.单例的概念 在iOS中,单例的概念指的是在整个应用程序的生命周期内,单例对象的类必须保证只有一个实例对象存在。...

网友评论

      本文标题:iOS单例的应用

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