OC 单例模式


概念

单例模式是一种常用的软件设计模式。在它的核心结构中只包含一个被称为单例的特殊类。通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源。如果希望在系统中某个类的对象只能存在一个,单例模式是最好的解决方案。

书写步骤

  1. 创建类方法,返回对象实例.以shared,default,current开头。
  2. 创建一个全局变量用来保存对象的引用
  3. 判断对象是否存在,若不存在,创建对象

编写方法

非线程安全

static UserHelper * helper = nil;
+ (UserHelper *)sharedUserHelper {
if (helper == nil) {
helper = [[UserHelper alloc] init];
}
return helper;
}

线程安全

方法一

static UserHelper * helper = nil;
+ (UserHelper *)sharedUserHelper {
@synchronized(self) { if (helper == nil) {
helper = [[UserHelper alloc] init];
}
}
return helper;
}

方法二

static UserHelper * helper = nil;
+ (void)initialize { if ([self class] == [UserHelper class]) {
helper = [[UserHelper alloc] init];
}
}

写法三(苹果推荐,主要用这个)

static UserHelper * helper = nil;
+ (UserHelper *)sharedUserHelper { static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
helper = [[UserHelper alloc] init];
}); return helper;
}

OC 单例模式的更多相关文章

  1. OC单例模式的实现

    SingleClass.m #import <Foundation/Foundation.h> @class SingleClass; static SingleClass *instan ...

  2. OC的单例模式的实现

    下面是在ARC,GCD下的单例模式实现: 头文件里申明类方法getInstance: #import <Foundation/Foundation.h> @interface Single ...

  3. OC学习篇之---单例模式

    在之前的一片文章中介绍了对象的拷贝相关知识:http://blog.csdn.net/jiangwei0910410003/article/details/41926531,今天我们来看一下OC中的单 ...

  4. OC编程之道-创建对象之单例模式

    一 何为单例singleton模式?(what) 保证一个类只有一个实例,并提供一个访问它的全局访问点. 二 何时使用单例模式?(where) 1类只能有一个实例,而且必须从一个为人熟知的访问点对其访 ...

  5. OC 设计模式——单例模式

    单例模式的作用:可以保证在程序运行过程,一个类只有一个实例,而且这个实例易于供外界访问.永远只分配一次内存给这个类.由于在调用alloc方法的时候,都会调用allocWithZone,所以要重写这个方 ...

  6. iOS开发——实用技术OC篇&单例模式的实实现(ACR&MRC)

    单例模式的实实现(ACR&MRC) 在iOS开发中单例模式是一种非常常见的模式,虽然我们自己实现的比较少,但是,系统却提供了不少的到来模式给我们用,比如最常见的UIApplication,No ...

  7. OC中使用单例模式

    static Config * instance = nil; +(Config *) Instance { @synchronized(self) { if(nil == instance) { [ ...

  8. 单例模式(oc)

    //主函数 main.m #import <Foundation/Foundation.h> #import "Singleton.h" int main(int ar ...

  9. Swift实现OC中的单例模式

    一.MySingle类 import Foundation class MySingle{ //定义单例的属性 var name:String? var age:Int? var height:Dou ...

随机推荐

  1. AngularJS之Service(四)

    前言 前面我们讲了控制器.过滤器以及指令,这一节我们来讲讲重大内容之一服务和其中涉及到的工厂. 话题 AngularJS中服务可以说是和DI紧密联系在一起,在应用程序中我们可以通过使用服务来共享代码, ...

  2. ModelState.IsValid总为false原因

    总结在开发中遇到的一个问题 ModelState.IsValid 一直是false 且在局部变量中,没有发现有问题啊,Model非常正常有木有,可是为什么 ModelState.IsValid 总是f ...

  3. EasyUI管理后台模板(附源码)

    下载地址:http://files.cnblogs.com/wyguo/easyui_demo.zip

  4. ZOJ Problem Set - 1115 Digital Roots

    水题记录: 注:此题题目并没有限定数值的大小,所以要用字符串进行处理 #include <stdio.h> #include <string.h> int main() { ] ...

  5. Web.Config文件配置小记

    <system.web>  <!--             设置 compilation debug="true" 将调试符号插入            已编译 ...

  6. RDD、DataFrame和DataSet的区别

    原文链接:http://www.jianshu.com/p/c0181667daa0 RDD.DataFrame和DataSet是容易产生混淆的概念,必须对其相互之间对比,才可以知道其中异同. RDD ...

  7. sessionid如何产生?由谁产生?保存在哪里?

    面试问道这个我居然不知道怎么回答,当然也是因为我确实没有研究过.下面就是百度了一篇文章后简单回答这个问题. 参考:http://www.cnblogs.com/sharpxiajun/p/339560 ...

  8. MySQL一个语句查出各种整形占用字节数及最大最小值

    直接上码: as min_num union , union , union , union , union ,) union ,) union ,) union ,) union ,); +---- ...

  9. Nancy之Cache的简单使用

    一.前言 说起缓存,或许大家都不陌生,甚至是天天都在用!当然缓存也被分的越来越细,页面缓存.数据缓存.文件缓存等等. 其实呢,我个人觉得,主要还是两大类:数据的缓存和页面的缓存.数据缓存的话就包括所有 ...

  10. [PHP]Maximum execution time of 30 seconds exceeded

    前言 在使用PHP渲染页面页面的时候,如果程序处理的时间特别久,超过配置文件(php.ini)设置的超时时间,就会出现如下提示: Maximum execution time of 30 second ...