zoukankan      html  css  js  c++  java
  • IOS 单例模式

     单例模式:
     1.永远只分配一块内存来创建对象
     2.提供一个类方法, 返回内部唯一的一个对象(一个实例)
     3.最好保证init方法也只初始化一次
     

    单例模式 :定义一份变量(整个程序运行过程中, 只有1份)

    //static id _instance;

    ARC 与非ARC 控制内存内存

    // ## : 连接字符串和参数
    #define singleton_h(name) + (instancetype)shared##name;
    
    #if __has_feature(objc_arc) // ARC
    
    #define singleton_m(name) 
    static id _instance; 
    + (id)allocWithZone:(struct _NSZone *)zone 
    { 
        static dispatch_once_t onceToken; 
        dispatch_once(&onceToken, ^{ 
            _instance = [super allocWithZone:zone]; 
        }); 
        return _instance; 
    } 
     
    + (instancetype)shared##name 
    { 
        static dispatch_once_t onceToken; 
        dispatch_once(&onceToken, ^{ 
            _instance = [[self alloc] init]; 
        })
        return _instance; 
    } 
    + (id)copyWithZone:(struct _NSZone *)zone 
    { 
        return _instance; 
    }
    
    #else // 非ARC
    
    #define singleton_m(name) 
    static id _instance; 
    + (id)allocWithZone:(struct _NSZone *)zone 
    { 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
    _instance = [super allocWithZone:zone]; 
    }); 
    return _instance; 
    } 
    
    + (instancetype)shared##name 
    { 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
    _instance = [[self alloc] init]; 
    }); 
    return _instance; 
    } 
    
    - (oneway void)release 
    { 
    
    } 
    
    - (id)autorelease 
    { 
    return _instance; 
    } 
    
    - (id)retain 
    { 
    return _instance; 
    } 
    
    - (NSUInteger)retainCount 
    { 
    return 1; 
    } 
    
    + (id)copyWithZone:(struct _NSZone *)zone 
    { 
    return _instance; 
    }
    
    #endif
    View Code

    调用

    #import "HMAudioTool.h"
    
    @interface HMAudioTool()
    @end
    
    @implementation HMAudioTool
    //// 定义一份变量(整个程序运行过程中, 只有1份)
    //static id _instance;
    
    - (id)init
    {
        if (self = [super init]) {
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                // 加载资源
                
            });
        }
        return self;
    }
    singleton_m(AudioTool)
    
    ///**
    // *  重写这个方法 : 控制内存内存
    // */
    //+ (id)allocWithZone:(struct _NSZone *)zone
    //{
    //    // 里面的代码永远只执行1次
    //    static dispatch_once_t onceToken;
    //    dispatch_once(&onceToken, ^{
    //        _instance = [super allocWithZone:zone];
    //    });
    //    
    //    // 返回对象
    //    return _instance;
    //}
    //
    //+ (instancetype)sharedAudioTool
    //{
    //    // 里面的代码永远只执行1次
    //    static dispatch_once_t onceToken;
    //    dispatch_once(&onceToken, ^{
    //        _instance = [[self alloc] init];
    //    });
    //    
    //    // 返回对象
    //    return _instance;
    //}
    //
    //+ (id)copyWithZone:(struct _NSZone *)zone
    //{
    //    return _instance;
    //}

    调用 HMAudioTool

    @interface HMViewController ()
    
    @end
    
    @implementation HMViewController
    
    /**
     单例模式:
     1.永远只分配一块内存来创建对象
     2.提供一个类方法, 返回内部唯一的一个对象(一个实例)
     3.最好保证init方法也只初始化一次
     */
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
    //    NSObject *obj = [[NSObject alloc] init];
    //#if ! __has_feature(objc_arc)
    //    [obj release];
    //#endif
    //    
    //    NSObject *obj1 = [[NSObject alloc] init];
    //#if ! __has_feature(objc_arc)
    //    [obj1 release];
    //#endif
    //    
    //    NSObject *obj2 = [[NSObject alloc] init];
    //#if ! __has_feature(objc_arc)
    //    [obj2 release];
    //#endif
        
        HMAudioTool *tool1 = [HMAudioTool sharedAudioTool];
        HMAudioTool *tool2 = [HMAudioTool sharedAudioTool];
        HMAudioTool *tool3 = [[HMAudioTool alloc] init];
        HMAudioTool *tool4 = [[HMAudioTool alloc] init];
        HMAudioTool *tool5 = [[HMAudioTool alloc] init];
        HMAudioTool *tool6 = [[HMAudioTool alloc] init];
    
        
    //    [tool1 play:@"win.mp3"];
        
        NSLog(@"%p %p %p %p", tool1, tool2, tool3, tool4);
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    View Code
  • 相关阅读:
    网页嵌入视频常用方式
    2.4 对字母数字的混合排序
    VC操作Image的三种方法(收集)
    VC 窗口出现白屏闪烁的解决办法
    Invalidate(TRUE)与Invalidate(FALSE)区别(前者会发送WM_ERASEBKGND消息全部刷新,然后使用WM_PAINT消息绘制,而后者只发送WM_PAINT消息)
    QT 文件拖放事件dropEvent和dragEnterEvent
    百用随身系统 Veket Linux
    C#通过属性名称获取(读取)属性值的方法
    搭建一个完整的Java开发环境
    XSD实例
  • 原文地址:https://www.cnblogs.com/liuwj/p/6602355.html
Copyright © 2011-2022 走看看