iOS之单例模式
2015-11-07 00:13
399 查看
单例模式的意思就是只有一个实例。单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例。这个类称为单例类。
1.单例模式的要点:
显然单例模式的要点有三个;一是某个类只能有一个实例;二是它必须自行创建这个实例;三是它必须自行向整个系统提供这个实例。
2.单例模式的优点:
1.实例控制:Singleton
会阻止其他对象实例化其自己的 Singleton
对象的副本,从而确保所有对象都访问唯一实例。
2.灵活性:因为类控制了实例化过程,所以类可以更加灵活修改实例化过程
IOS中的单例模式
在objective-c中要实现一个单例类,至少需要做以下四个步骤:
1、为单例对象实现一个静态实例,并初始化,然后设置成nil,
2、实现一个实例构造方法检查上面声明的静态实例是否为nil,如果是则新建并返回一个本类的实例,
3、重写allocWithZone方法,用来保证其他人直接使用alloc和init试图获得一个新实力的时候不产生一个新实例,
4、适当实现allocWitheZone,copyWithZone,release和autorelease。
以下转载自:http://www.galloway.me.uk/tutorials/singleton-classes/
One of my most used design patterns when developing for iOS is the singleton pattern. It’s an extremely powerful way to share data between different parts of code without
having to pass the data around manually. More about the singleton pattern and other patterns can be found in this excellent book:
Singleton classes are an important concept to understand because they exhibit an extremely useful design pattern. This idea is used throughout the iPhone SDK, for example, UIApplication has a method called sharedApplication which when called from anywhere will
return the UIApplication instance which relates to the currently running application.
You can implement a singleton class in Objective-C using the following code:
MyManager.h
MyManager.m
What this does is it defines a static variable (but only global to this translation unit)) called
is then initialised once and only once in
The way we ensure that it’s only created once is by using the
from Grand Central Dispatch (GCD). This is thread safe and handled entirely by the OS for you so
that you don’t have to worry about it at all.
However, if you would rather not use GCD then you should use the following code for
Non-GCD based code
Then you can reference the singleton from anywhere by calling the following function:
I’ve used this extensively throughout my code for things such as creating a singleton to handle CoreLocation or CoreData functions.
Not that I recommend it, but if you are not using Automatic Reference Counting (ARC), then you should use the following code:
MyManager.h non-ARC
EDIT: Added property to MyManager.
EDIT: Updated as per Apple’s
guidelines to pass static analysis.
EDIT: Updated to support ARC.
EDIT: Switched to use the more common GCD approach.
1.单例模式的要点:
显然单例模式的要点有三个;一是某个类只能有一个实例;二是它必须自行创建这个实例;三是它必须自行向整个系统提供这个实例。
2.单例模式的优点:
1.实例控制:Singleton
会阻止其他对象实例化其自己的 Singleton
对象的副本,从而确保所有对象都访问唯一实例。
2.灵活性:因为类控制了实例化过程,所以类可以更加灵活修改实例化过程
IOS中的单例模式
在objective-c中要实现一个单例类,至少需要做以下四个步骤:
1、为单例对象实现一个静态实例,并初始化,然后设置成nil,
2、实现一个实例构造方法检查上面声明的静态实例是否为nil,如果是则新建并返回一个本类的实例,
3、重写allocWithZone方法,用来保证其他人直接使用alloc和init试图获得一个新实力的时候不产生一个新实例,
4、适当实现allocWitheZone,copyWithZone,release和autorelease。
以下转载自:http://www.galloway.me.uk/tutorials/singleton-classes/
One of my most used design patterns when developing for iOS is the singleton pattern. It’s an extremely powerful way to share data between different parts of code without
having to pass the data around manually. More about the singleton pattern and other patterns can be found in this excellent book:
Background
Singleton classes are an important concept to understand because they exhibit an extremely useful design pattern. This idea is used throughout the iPhone SDK, for example, UIApplication has a method called sharedApplication which when called from anywhere willreturn the UIApplication instance which relates to the currently running application.
How to implement
You can implement a singleton class in Objective-C using the following code:MyManager.h
1 2 3 4 5 6 7 8 9 10 11 | #import <foundation/Foundation.h> @interface MyManager : NSObject { NSString *someProperty; } @property (nonatomic, retain) NSString *someProperty; + (id)sharedManager; @end |
1 2 3 4 5 6 7 8 9 10 1112 | #import "MyManager.h" @implementation MyManager @synthesize someProperty; #pragma mark Singleton Methods + (id)sharedManager { static MyManager *sharedMyManager = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedMyManager = [[self alloc] init]; }); return sharedMyManager; } - (id)init { if (self = [super init]) { someProperty = [[NSString alloc] initWithString:@"Default Property Value"]; } return self; } - (void)dealloc { // Should never be called, but just here for clarity really. } @end |
sharedMyManagerwhich
is then initialised once and only once in
sharedManager.
The way we ensure that it’s only created once is by using the
dispatch_oncemethod
from Grand Central Dispatch (GCD). This is thread safe and handled entirely by the OS for you so
that you don’t have to worry about it at all.
However, if you would rather not use GCD then you should use the following code for
sharedManager:
Non-GCD based code
1 2 3 4 5 6 7 8 | + (id)sharedManager { static MyManager *sharedMyManager = nil; @synchronized(self) { if (sharedMyManager == nil) sharedMyManager = [[self alloc] init]; } return sharedMyManager; } |
MyManager *sharedManager = [MyManager sharedManager];
I’ve used this extensively throughout my code for things such as creating a singleton to handle CoreLocation or CoreData functions.
Non-ARC code
Not that I recommend it, but if you are not using Automatic Reference Counting (ARC), then you should use the following code:MyManager.h non-ARC
1 2 3 4 5 6 7 8 9 10 1112 | #import "MyManager.h" static MyManager *sharedMyManager = nil; @implementation MyManager @synthesize someProperty; #pragma mark Singleton Methods + (id)sharedManager { @synchronized(self) { if(sharedMyManager == nil) sharedMyManager = [[super allocWithZone:NULL] init]; } return sharedMyManager; } + (id)allocWithZone:(NSZone *)zone { return [[self sharedManager] retain]; } - (id)copyWithZone:(NSZone *)zone { return self; } - (id)retain { return self; } - (unsigned)retainCount { return UINT_MAX; //denotes an object that cannot be released } - (oneway void)release { // never release } - (id)autorelease { return self; } - (id)init { if (self = [super init]) { someProperty = [[NSString alloc] initWithString:@"Default Property Value"]; } return self; } - (void)dealloc { // Should never be called, but just here for clarity really. [someProperty release]; [super dealloc]; } @end |
Updates
EDIT: Added property to MyManager.EDIT: Updated as per Apple’s
guidelines to pass static analysis.
EDIT: Updated to support ARC.
EDIT: Switched to use the more common GCD approach.
相关文章推荐
- 蜂窝教育iOS:我的月薪万元的逆袭之路
- iOS 闪光灯
- 【转】iOS:堆(heap)和栈(stack)的理解--简介
- 谈谈ios内存管理--持续更新
- iOS:KVO的概述与使用
- ios2048
- ios2048
- iOS开发中的IBAction中的IB是什么意思
- ios基础训练(一)
- iOS 9 适配系列教程
- iOS支付宝集成的小坑--集成支付宝集成总结(1)
- 蓝懿ios 技术内容和心得交流分享 11.6
- iOS开发——高德地图调研之显示地图、定位与地理编码等
- iOS——支付
- ios笔试题
- 疯狂iOS讲义(下)——多线程第二天学习笔记
- Nagios监控搭建与配置详细步骤
- ios 原生二维码扫描以及需要注意事项
- iOS 自己开发SDK必须要注意的几点
- #在蓝懿学习iOS的日子#Day13