zoukankan      html  css  js  c++  java
  • 单例模式简单实现

     1 @interface HMMusicTool : NSObject
     2 + (instancetype)sharedMusicTool;
     3 @end
     4 
     5 @implementation HMMusicTool
     6 static id _instance;
     7 
     8 /**
     9  *  alloc方法内部会调用这个方法
    10  */
    11 + (id)allocWithZone:(struct _NSZone *)zone
    12 {
    13     if (_instance == nil) { // 防止频繁加锁
    14         @synchronized(self) {
    15             if (_instance == nil) { // 防止创建多次
    16                 _instance = [super allocWithZone:zone];
    17             }
    18         }
    19     }
    20     return _instance;
    21 }
    22 
    23 + (instancetype)sharedMusicTool
    24 {
    25     if (_instance == nil) { // 防止频繁加锁
    26         @synchronized(self) {
    27             if (_instance == nil) { // 防止创建多次
    28                 _instance = [[self alloc] init];
    29             }
    30         }
    31     }
    32     return _instance;
    33 }
    34 
    35 - (id)copyWithZone:(NSZone *)zone
    36 {
    37     return _instance;
    38 }
    39 @end
  • 相关阅读:
    Redis详解(一)——RDB
    十、行锁
    go 上下文context
    go log
    go 结构体取代类
    go select
    go channel
    go 协程(Goroutine)
    go 学习之fmt包
    go 学习之bufio
  • 原文地址:https://www.cnblogs.com/H7N9/p/4896430.html
Copyright © 2011-2022 走看看