• 多线程练习 -- 单例设计模式


    Singleton.h

    #define singleton_h(name) + (instancetype)shared##name;
    
    #if __has_feature (objc_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)sharedOnlyoneClass 
    { 
        static dispatch_once_t onceToken; 
        dispatch_once(&onceToken, ^{ 
            _instance = [[self alloc] init]; 
        }); 
        return _instance; 
    }
    
    
    #else
    
    #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)retain 
    {
        return _instance; 
    } 
    
    - (NSUInteger)retainCount 
    { 
        return 1; 
    } 
    
    - (id)autorelease 
    { 
        return _instance; 
    } 
    
    + (id)copyWithZone:(struct _NSZone *)zone 
    { 
        return _instance; 
    } 
    
    + (id)mutableCopyWithZone:(struct _NSZone *)zone 
    { 
        return _instance; 
    }
    
    #endif
    View Code

    LWTOnlyoneClass.h

    #import <Foundation/Foundation.h>
    #import "Singleton.h"
    
    @interface LWTOnlyoneClass : NSObject
    
    @property (nonatomic, copy) NSString *name;
    
    singleton_h(OnlyoneClass)
    
    @end
    View Code

    LWTOnlyoneClass.m

    //
    //  LWTOnlyoneClass.m
    //  多线程练习 -- 单例设计模式
    //
    //  Created by apple on 14-6-24.
    //  Copyright (c) 2014年 lwt. All rights reserved.
    //
    
    #import "LWTOnlyoneClass.h"
    
    @implementation LWTOnlyoneClass
    
    - (instancetype)init
    {
        self = [super init];
        if (self) {
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                self.name = @"jack";
            });
        }
        return self;
    }
    
    singleton_m(OnlyoneClass)
    
    @end
    View Code

    LWTViewController.h

    #import <UIKit/UIKit.h>
    
    @interface LWTViewController : UIViewController
    
    @end
    View Code

    LWTViewController.m

    //
    //  LWTViewController.m
    //  多线程练习 -- 单例设计模式
    //
    //  Created by apple on 14-6-24.
    //  Copyright (c) 2014年 lwt. All rights reserved.
    //
    
    #import "LWTViewController.h"
    #import "LWTOnlyoneClass.h"
    
    @interface LWTViewController ()
    
    @end
    
    @implementation LWTViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        LWTOnlyoneClass *onlyone1 = [[LWTOnlyoneClass alloc] init];
        LWTOnlyoneClass *onlyone2 = [LWTOnlyoneClass sharedOnlyoneClass];
        onlyone2.name = @"Tom";
        LWTOnlyoneClass *onlyone3 = [[LWTOnlyoneClass alloc] init];
        onlyone3.name = @"rose";
        LWTOnlyoneClass *onlyone4 = [LWTOnlyoneClass sharedOnlyoneClass];
        onlyone4.name = @"jim";
        
    //#if __has_feature (objc_arc)
    //    LWTOnlyoneClass *onlyone5 = [LWTOnlyoneClass sharedOnlyoneClass];
    //    LWTOnlyoneClass *onlyone6 = [LWTOnlyoneClass sharedOnlyoneClass];
    ////#else
    //    LWTOnlyoneClass *onlyone5 = [onlyone2 copy];
    //    LWTOnlyoneClass *onlyone6 = [onlyone3 mutableCopy];
    //#endif
        
        
        
        NSLog(@"%p -- %p -- %p -- %p", onlyone1, onlyone2, onlyone3, onlyone4);
        NSLog(@"%@ -- %@ -- %@ -- %@", onlyone1.name, onlyone2.name, onlyone3.name, onlyone4.name);
    }
    
    
    @end
    View Code
  • 相关阅读:
    thymeleaf 模板引擎
    thymeleaf 内联语法
    thymeleaf 局部变量、属性优先级、注释
    PYNQ上手笔记 | ① 启动Pynq
    MSP430F5529时钟系统深究
    C#上位机开发(四)—— SerialAssistant功能完善
    C#上位机开发(三)—— 构建SerialAssistant雏形
    C#上位机开发(二)—— Hello,World
    C#上位机开发(一)—— 了解上位机
    FPGA学习笔记(八)—— 状态机设计实例之独立按键消抖
  • 原文地址:https://www.cnblogs.com/wentianblog/p/3808822.html
Copyright © 2020-2023  润新知