IOS-DesignPattern-Singleton
来源:互联网 发布:win10 64位安装mysql 编辑:程序博客网 时间:2024/05/16 07:51
1、SingletonDemo.h
#import <Foundation/Foundation.h>@interface SingletonDemo : NSObject+(instancetype)shareSingletonDemo;//get instance method@end
2、SingletonDemo.m
#import "SingletonDemo.h"@implementation SingletonDemo//静态变量static SingletonDemo *_instance = nil;//作用:加载类//什么时候调用:每次程序一启动,就会把所有的类加载进内存+(void)load{ NSLog(@"%s",__func__); _instance = [[self alloc] init];}//get instance+(instancetype)shareSingletonDemo{ return _instance;}//reload alloc method+(instancetype)alloc{ //when the alloc method is being called,if the "_instance" is not nil then raise exception. if(_instance) { NSException* excp = [NSException exceptionWithName:@"NSInternalInconsistencyException"reason:@"There can only be one UIApplication instance" userInfo:nil]; [excp raise]; } return [super alloc];}@end
3、测试代码
//[[SingletonDemo alloc] init];//The wrong way to call singleton instance SingletonDemo* single = [SingletonDemo shareSingletonDemo];//the right way to call singleton instance NSLog(@"%@",single);
0 0
- IOS-DesignPattern-Singleton
- DesignPattern-Singleton
- DesignPattern---java---Singleton感想
- [DesignPattern] 单例 Singleton
- DesignPattern
- DesignPattern
- DesignPattern
- DesignPattern-Builder
- [DesignPattern]DecoratorPattern
- [DesignPattern]TemplateMethodPattern
- [DesignPattern]IteratorAndCompositePattern
- [DesignPattern]StatePattern
- [DesignPattern]ProxyPattern
- [DesignPattern]CompoundPattern
- iOS: Singleton模式
- ios单例Singleton
- iOS--单例(Singleton)
- iOS Singleton 单例模式
- 大白话讲解遗传算法
- 安装ansible时,报fatal error: Python.h: No such file or directory
- android如何客制化紧急号码?
- 微信小程序 判断接口是否可用并给出"弹出提示框"提示
- 记录一些makefile 的规则、变量函数使用
- IOS-DesignPattern-Singleton
- 【杂物间】
- 看完这几张图,才知道高手怎么玩。用户画像、精准营销不是事儿!
- 如何解决failed to resolve:com.github.bumptech.glide:glide:3.7.0
- 5-1 表达式转换 (25分)
- 08.Play.libs库包
- 剑指offer:栈的压入弹出顺序
- Java 中int、String的类型转换
- Linux Crontab 定时任务