zoukankan      html  css  js  c++  java
  • iOS设计模式

    iOS设计模式 - 单例

    原理图

    源码

    https://github.com/YouXianMing/iOS-Design-Patterns

    //
    //  Singleton.h
    //  SingletonPattern
    //
    //  Created by YouXianMing on 15/8/6.
    //  Copyright (c) 2015年 YouXianMing. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface Singleton : NSObject
    
    + (Singleton *)sharedInstance;
    
    @end
    //
    //  Singleton.m
    //  SingletonPattern
    //
    //  Created by YouXianMing on 15/8/6.
    //  Copyright (c) 2015年 YouXianMing. All rights reserved.
    //
    
    #import "Singleton.h"
    
    static Singleton *_sharedSingleton = nil;
    
    @implementation Singleton
    
    - (instancetype)init {
        
        [NSException raise:@"SingletonPattern"
                    format:@"Cannot instantiate singleton using init method, sharedInstance must be used."];
        
        return nil;
    }
    
    - (id)copyWithZone:(NSZone *)zone {
    
        [NSException raise:@"SingletonPattern"
                    format:@"Cannot copy singleton using copy method, sharedInstance must be used."];
        
        return nil;
    }
    
    + (Singleton *)sharedInstance {
    
        if (self != [Singleton class]) {
            
            [NSException raise:@"SingletonPattern"
                        format:@"Cannot use sharedInstance method from subclass."];
        }
        
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            
            _sharedSingleton = [[Singleton alloc] initInstance];
        });
    
        return _sharedSingleton;
    }
    
    #pragma mark - private method
    
    - (id)initInstance {
    
        return [super init];
    }
    
    @end
  • 相关阅读:
    Hive安装
    hbase安装
    Spring boot 出现的时间
    RESTful Web API 实践
    Java的进阶之道
    Spring boot 注解简单备忘
    使用 xshell 登录 Windows 的 linux 子系统
    Nginx 实用配置
    跟着大彬读源码
    跟着大彬读源码
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4709209.html
Copyright © 2011-2022 走看看