zoukankan      html  css  js  c++  java
  • 单例模式-用其他方式实现

     1 - (void)viewDidLoad
     2 {
     3     [super viewDidLoad];
     4     
     5     [self test];  
     6 }
     7 
     8 - (void)test
     9 {
    10         XZMusicTool *tool = [[XZMusicTool alloc] init];
    11         XZMusicTool *tool2 = [[XZMusicTool alloc] init];
    12         XZMusicTool *tool3 = [XZMusicTool sharedMusicTool];
    13         XZMusicTool *tool4 = [XZMusicTool sharedMusicTool];
    14     
    15         // copy 有可能会产生新的对象
    16         // copy方法内部会调用- copyWithZone:
    17         XZMusicTool *tool5 = [tool4 copy];
    18     
    19         NSLog(@"%@ %@ %@ %@ %@", tool, tool2, tool3, tool4, tool5);
    20 }        

    [[XZMusicTool alloc] init];

    [XZMusicTool sharedMusicTool];

    [tool4 copy];

    以上三种方式都能保证创建出来的对象是同一个.

    1 /**
    2  * 懒汉式
    3  */
    4 #import <Foundation/Foundation.h>
    5 
    6 @interface XZMusicTool : NSObject
    7 + (instancetype)sharedMusicTool;
    8 @end
     1 .
     2 //  懒汉式
     3 
     4 #import "XZMusicTool.h"
     5 
     6 @implementation XZMusicTool
     7 static id _instance;
     8 
     9 /**
    10  *  alloc方法内部会调用这个方法
    11  */
    12 + (id)allocWithZone:(struct _NSZone *)zone
    13 {
    14     if (_instance == nil) { // 防止频繁加锁
    15         @synchronized(self) {
    16             if (_instance == nil) { // 防止创建多次
    17                 _instance = [super allocWithZone:zone];
    18             }
    19         }
    20     }
    21     return _instance;
    22 }
    23 
    24 + (instancetype)sharedMusicTool
    25 {
    26     if (_instance == nil) { // 防止频繁加锁
    27         @synchronized(self) {
    28             if (_instance == nil) { // 防止创建多次
    29                 _instance = [[self alloc] init];
    30             }
    31         }
    32     }
    33     return _instance;
    34 }
    35 
    36 - (id)copyWithZone:(NSZone *)zone
    37 {
    38     return _instance;  // 既然可以copy,说明是已经创建了对象。
    39 }
    40 @end
    1 /**
    2  * 饿汉式
    3  */
    4 #import <Foundation/Foundation.h>
    5 
    6 @interface XZSoundTool : NSObject
    7 + (instancetype)sharedSoundTool;
    8 @end
     1 //  饿汉式
     2 
     3 #import "XZSoundTool.h"
     4 
     5 @implementation XZSoundTool
     6 static id _instance;
     7 
     8 /**
     9  *  当类加载到OC运行时环境中(内存),就会调用一次(一个类只会加载1次)
    10  */
    11 + (void)load
    12 {
    13     _instance = [[self alloc] init];
    14 }
    15 
    16 + (id)allocWithZone:(struct _NSZone *)zone
    17 {
    18     if (_instance == nil) { // 防止创建多次
    19         _instance = [super allocWithZone:zone];
    20     }
    21     return _instance;
    22 }
    23 
    24 + (instancetype)sharedSoundTool
    25 {
    26     return _instance;
    27 }
    28 
    29 - (id)copyWithZone:(NSZone *)zone
    30 {
    31     return _instance;
    32 }
    33 
    34 ///**
    35 // *  当第一次使用这个类的时候才会调用
    36 // */
    37 //+ (void)initialize
    38 //{
    39 //    NSLog(@"HMSoundTool---initialize");
    40 //}
    41 @end
  • 相关阅读:
    团队项目-个人博客-4.25
    团队项目-个人博客-4.24 学习进度08
    评价使用的输入法
    个人工作总结08
    个人工作总结07
    第八周学习进度条
    个人工作总结06
    构建之法阅读笔记04
    个人工作总结05
    个人工作总结04
  • 原文地址:https://www.cnblogs.com/nxz-diy/p/5366163.html
Copyright © 2011-2022 走看看