今天主要讲的是iOS中经典的设计模式-单例设计模式。这一模式的意图就是使得类中的一个对象成为系统中的唯一实例。它提供了对类的对象所提供的资源的全局访问点。因此需要用一种只允许生成对象类的唯一实例的机制。今天我将用一个demo通过OC与Swift两种语言来讲述单例模式的应用,我的邮箱是KenenCS@163.com,欢迎多多交流!
单例模式三准则:
1. 单例必须是唯一的,在程序生命周期中只能存在一个这样的实例,单例的存在使我们可以全局访问状态。
2. 为保证单例的唯一性,单例类的初始化方法必须是私有的,这样就可以避免其他对象通过单例类创建额外的实例。
3. 保证单例的线程安全性,才可以保证其唯一性,通过调用dispatch_once,即可保证实例化代码只运行一次。
一、使用Swift语言创建单例
Swift中创建单例的方法我知道的从Swift1.0~Swift4.0一共有四种,我今天说的这一种是标准用法,因为是Swift官方推荐的,比较简单,代码简洁。
创建单例类,起名叫做Singleton,继承于NSObject
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Singleton:NSObject { private static let instance = Singleton(); class var shardSingleton: Singleton { return instance; } func testSingletonColor() -> UIColor { let color = UIColor.init(colorLiteralRed: 100/255.0, green: 20/255.0, blue: 30/255.0, alpha: 1); return color; } }
|
在ViewController中进行测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let sing1 = Singleton.init(); let sing2 = Singleton.shardSingleton; print("---Swift的sing1:\(sing1)\n---Swift的sing2:\(sing2)\n"); self.view.backgroundColor = Singleton.shardSingleton.testSingletonColor(); } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
|
调试栏输出的图如下:

一、使用OC语言创建单例
创建单例类,起名叫做Singleton,继承于NSObject,里面单例创建的方法是我们大家常用的GCD创建手法。
Singleton.h
1 2 3 4 5 6 7 8 9 10 11
| #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface Singleton : NSObject +(instancetype)shardSingleton; - (UIColor *)testSingletonColor; @end
|
Singleton.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| #import "Singleton.h" @implementation Singleton +(instancetype)shardSingleton { static Singleton *instance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [[super allocWithZone:NULL] init]; }); return instance; } +(instancetype)allocWithZone:(struct _NSZone *)zone { return [Singleton shardSingleton]; } - (id)copy { return self; } - (id)mutableCopy { return self; } - (UIColor *)testSingletonColor { UIColor *color = [UIColor colorWithRed:100/255.0 green:20/255.0 blue:30/255.0 alpha:1]; return color; } @end
|
在ViewController中进行测试,因为.h文件中没有什么变动,我直接粘贴.m文件的代码了,如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| #import "ViewController.h" #import "Singleton.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; Singleton *sing1 = [[Singleton alloc] init]; Singleton *sing2 = [Singleton new]; Singleton *sing3 = [Singleton shardSingleton]; Singleton *sing4 = [sing1 copy]; Singleton *sing5 = [sing1 mutableCopy]; NSLog(@"\n---sing1:%p\n---sing2:%p\n---sing3:%p\n---sing4:%p\n---sing5:%p\n",sing1,sing2,sing3,sing4,sing5); self.view.backgroundColor = [[Singleton shardSingleton] testSingletonColor]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
|
调试栏输出的图如下:

以上就是此次讲解,欢迎大家下载交流!Demo地址如下:
Swift版本:SingletonDemo_Swift
OC版本:SingletonDemo