Objective-C中的单例模式(工具类)
单例是iOS开发中经常会用到的一种设计模式,顾名思义,即创建一个类,该类在整个程序的生命周期中只有一个实例对象,无论是通过new,alloc init,copy等方法创建,或者创建多少个对象,自始至终在内存中只会开辟一块空间,直到程序结束,由系统释放.
如下图用不同的方式创建6个对象,但通过打印其内存地址,我们可以发现它们是共享同一块内存空间的.

由于在平时开发中经常用到,所以我将创建单例的方法定义成宏,并封装成一个工具类,提供了一个类方法来快速创建1个单例对象;
并且此类的单例包括了在MRC模式下的创建方式,保证了在MRC模式下,仍能使用该工具类来快速创建1个单例对象;
该工具类使用非常方便,只需在需要用到的类中导入头文件即可,以下是实现代码:
//
// YYSharedModelTool.h
// SharedModel
//
// Created by Arvin on 15/12/21.
// Copyright © 2015年 Arvin. All rights reserved.
// #ifndef YYSharedModelTool_h
#define YYSharedModelTool_h // .h 文件
// ##: 在宏中,表示拼接前后字符串
#define YYSharedModelTool_H(className) + (instancetype)shared##className; #if __has_feature(objc_arc) // ARC 环境 // .m 文件
#define YYSharedModelTool_M(className)\
/****ARC 环境下实现单例的方法****/\
+ (instancetype)shared##className {\
return [[self alloc] init];\
}\
\
- (id)copyWithZone:(nullable NSZone *)zone {\
return self;\
}\
\
+ (instancetype)allocWithZone:(struct _NSZone *)zone {\
static id instance;\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
instance = [super allocWithZone:zone];\
});\
return instance;\
} #else // MRC 环境 // .m 文件
#define YYSharedModelTool_M(className)\
\
+ (instancetype)shared##className {\
return [[self alloc] init];\
}\
\
- (id)copyWithZone:(nullable NSZone *)zone {\
return self;\
}\
\
+ (instancetype)allocWithZone:(struct _NSZone *)zone {\
static id instance;\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
instance = [super allocWithZone:zone];\
});\
return instance;\
}\
/****MRC 环境需要重写下面3个方法****/\
- (oneway void)release {\
\
}\
- (instancetype)retain {\
return self;\
}\
- (instancetype)autorelease {\
return self;\
} #endif #endif /* YYSharedModelTool_h */
END! 欢迎留言交流,一起学习...
Objective-C中的单例模式(工具类)的更多相关文章
- JAVA中封装JSONUtils工具类及使用
在JAVA中用json-lib-2.3-jdk15.jar包中提供了JSONObject和JSONArray基类,用于JSON的序列化和反序列化的操作.但是我们更习惯将其进一步封装,达到更好的重用. ...
- commons-lang3-3.2.jar中的常用工具类的使用
这个包中的很多工具类可以简化我们的操作,在这里简单的研究其中的几个工具类的使用. 1.StringUtils工具类 可以判断是否是空串,是否为null,默认值设置等操作: /** * StringUt ...
- Android 中替代 sharedpreferences 工具类的实现
Android 中替代 sharedpreferences 工具类的实现 背景 想必大家一定用过 sharedpreferences 吧!就我个人而言,特别讨厌每次 put 完数据还要 commit. ...
- 实用篇:说说我在JavaScript项目中使用的工具类
在JavaScript的开发中,我们都会写一些工具类来帮我们简化一些业务操作的逻辑,一下就貼几个我在项目开发过程中常用的工具类.表达能力有限,各位看官还是看源码吧. 一.日期处理工具类. /** * ...
- java编程中的断言工具类(org.springframework.util.Assert)
转自:https://blog.csdn.net/gokeiryou263/article/details/19612471 断言工具类:Assert类, java.lang.Object ---&g ...
- Java 中的并发工具类
Java 中的并发工具类 CountDownLatch public class JoinCountDownLatchTest { public static void main(String[] a ...
- DateHandler日期处理工具(JSP中使用后台工具类)
1.DateHandler.java package Utils.dateHandler; import java.text.ParseException; import java.text.Simp ...
- java中常用的工具类(三)
继续分享java中常用的一些工具类.前两篇的文章中有人评论使用Apache 的lang包和IO包,或者Google的Guava库.后续的我会加上的!谢谢支持IT江湖 一.连接数据库的综合类 ...
- java中常用的工具类(二)
下面继续分享java中常用的一些工具类,希望给大家带来帮助! 1.FtpUtil Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
随机推荐
- python construct文档
The Basics Fields Fields are the most fundamental unit of construction: they parse (read data from t ...
- nsfocus-笔试题
1.描述sendmail原理及通讯机制 sendmail程序接受到待发邮件后,通过关键字@判断邮件的格式是否符合要求,匹配成功后提取邮件后缀域名信息并查询DNS数据库相关MX(邮件专用)记录,若有匹配 ...
- 布局共享(列如所有activity拥有相同的布局部分,比如actionbar,在BaseActivity中写入布局)
有时候界面上会用到统一的布局,比如toolbar,你可能会想到在用到的地方都去加上toobar这样对于程序的开发与维护来说都显得特别麻烦,我们可以将他写在父类中. 首先创建一个BaseActivity ...
- Matlab 图像预处理
%%%%%%%%%%%%%%%%% %%降采样 clear all im={}; %创建字典保存读取的图片 dis=dir('F:\kaggle_data_zip\Sample\*.jpeg');%% ...
- QT creator中使用opencv
最近要用到opencv做图像方面的东西,网上很多是用VS加opencv,但自己对VS不怎么喜欢,想用QT Creator.在网上搜索了很多资料,终于花了一天的时间,在QT Creator上能使用ope ...
- vi使用入门指南
一.Unix编辑器概述 编辑器是使用计算机的重要工具之一,在各种操作系统中,编辑器都是必不可少的部件.Unix及其相似的ix操作系统系列中,为方便各种用户在各个不同的环境中使用,提供了一系列的ex编辑 ...
- iOS设备后台播放音乐方法
iOS设备后台播放音乐方法 1 在设置Capabliites中打开Background Modes,选择Audio And AirPlay 2 在控制viewDidLoad中添加下面代码 AVAudi ...
- LeetCode_Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- Visual Studio中的lib的链接顺序
描述:如果有一个exe工程,它依赖于A.lib,B.lib,A.lib和B.DLL我同样有他们的源码工程.依赖顺序是这样的exe->A.lib->B.DLL.那么如果我改动了B的源码,编译 ...
- http://wiki.apache.org/tomcat/HowTo
http://wiki.apache.org/tomcat/HowTo Contents Meta How do I add a question to this page? How do I con ...