OS开发常用设计模式之单例设计模式

    今天主要讲的是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()
// Do any additional setup after loading the view, typically from a nib.
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()
// Dispose of any resources that can be recreated.
}
}

调试栏输出的图如下:
Swift

一、使用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;
//通过GCD实现对象只创建一次
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[super allocWithZone:NULL] init];
});
return instance;
}
//确保唯一性,防止通过【[Singleton alloc] init]创建对象,因此需要重写这个方法,因为调用alloc时会自动调用allocWithZone这个方法
+(instancetype)allocWithZone:(struct _NSZone *)zone {
return [Singleton shardSingleton];
}
//确保唯一性,防止通过copy和mutableCopy创建对象,安全起见,重写以下两个方法
- (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];
// Do any additional setup after loading the view, typically from a nib.
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];
// Dispose of any resources that can be recreated.
}
@end

调试栏输出的图如下:
Objetive-C



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

文章目录
  1. 1. 一、使用Swift语言创建单例
    1. 1.1. 创建单例类,起名叫做Singleton,继承于NSObject
    2. 1.2. 在ViewController中进行测试
  2. 2. 一、使用OC语言创建单例
    1. 2.1. Singleton.h
    2. 2.2. Singleton.m
|