一、单例模式
1.1 概述
单例模式可以保证App在程序运行中,一个类只有唯一个实例,从而做到节约内存。
在整个App程序中,这一份资源是共享的。
提供一个固定的实例创建方法。
1.2 系统为我们提供的单例类有哪些?
UIApplication(应用程序实例类)
NSNotificationCenter(消息中心类)
NSFileManager(文件管理类)
NSUserDefaults(应用程序设置)
NSURLCache(请求缓存类)
NSHTTPCookieStorage(应用程序cookies池)
1.3 在哪些地方会用到单例模式
一般在程序中,经常调用的类,如工具类、公共跳转类等,会采用单例模式
例如:登陆控制器,网络数据请求,音乐播放器等一个工程需要使用多次的控制器或方法。
1.4 单例的初始化
.h声明方法
+ (SingleCase *)shareSingleCase;
.m实现
+ (SingleCase *)shareSingleCase {
static SingleCase *single = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
single = [[self alloc] init];
});
//dispatch_once 作用是整个程序的生命周期中,仅执行一次某一个block对象。
return single;
}
二、代理模式
2.1 概述
代理模式是一种消息传递方式,一个完整的代理模式包括:委托对象、代理对象和协议。
2.2 名称解释
协议:用来指定代理双方可以做什么,必须做什么。
委托对象:根据协议指定代理对象需要完成的事,即调用协议中的方法。
代理对象:根据协议实现委托方需要完成的事,即实现协议中的方法。
2.3 应用场景
当一个类的某些功能需要由别的类来实现,但是又不确定具体会是哪个类实现。
2.4 具体实现
//委托对象 NextViewController.h
#import <UIKit/UIKit.h>
@protocol SayDadDelegate <NSObject>
//1.声明协议 @required表示必须实现 @optional表示非必须 若没有关键词表示必须实现
@required
- (void)sayDad:(NSString *)str;
@optional
- (void)saySon:(NSString *)str;
@end
@interface NextViewController : UIViewController
//2.定义代理属性 ARC下使用weak; MRC下使用assign 防止循环引用
@property (nonatomic, weak) id <SayDadDelegate> delegate;
@end
//委托对象 NextViewController.m
//6.委托方通知代理来执行任务
//分别判断是否设置代理 和 实现协议方法
if (self.delegate && [self.delegate respondsToSelector:@selector(sayDad:)]) {
[self.delegate sayDad:@"我就是你爸爸"];
if ([self.delegate respondsToSelector:@selector(saySon:)]) {
[self.delegate saySon:@"我就是你儿子"];
}
}
//代理对象 ViewController.m
#import "ViewController.h"
#import "NextViewController.h"
@interface ViewController ()<SayDadDelegate>//4.服从协议
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NextViewController *next = [[NextViewController alloc] init];
next.modalPresentationStyle = UIModalPresentationFullScreen;
//3.设置代理
next.delegate = self;
[self presentViewController:next animated:YES completion:nil];
}
//5.实现协议中的方法
- (void)sayDad:(NSString *)str {
NSLog(@"主界面--- %@", str);
}
- (void)saySon:(NSString *)str {
NSLog(@"主界面--- %@", str);
}
@end
三、观察者模式
3.1 概述
定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态发生变化时,会通知所有观察者对象,使它们能够自动更新自己。
3.2 应用场景
iOS中的KVO、NSNotication都是观察者模式。
3.3 通知机制
通知机制与委托机制不同,前者是”一对多“的对象之间的通信,后者是“一对一”的对象之间的通信。在通知机制中,对某个通知感兴趣的所有对象都可以成为接收者。
//实现方式
//发送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshData" object:nil];
//注册成为观察者
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData)name:@"RefreshData" object:nil];
//具体实现
-(void)loadData{
}
//移除通知
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"RefreshData" object:nil];
}
3.4 KVO机制
KVO机制不像通知机制那样通过一个通知中心观察所有对象,而是在对象属性变化时将通知直接发送给观察者对象
//监测应用程序状态变化,appStatus是我们要观察的对象的属性
在AppStatusObserver类中的代码
@implementation AppStatusObersver
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
NSLog(@"%@ %@",keyPath,change[NSKeyValueChangeNewKey]);
}
在appDelagate类中的代码
@interface AppDelegate ()
@property (nonatomic,strong)NSString *appStatus;
@property (nonatomic,strong)AppStatusObersver *observer;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.observer = [[AppStatusObersver alloc] init];
[self addObserver:self.observer forKeyPath:@"appStatus" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:nil];
self.appStatus = @"launch";
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
self.appStatus = @"inactive";
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
self.appStatus = @"background";
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
self.appStatus = @"foreground";
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
self.appStatus = @"active";
}
- (void)applicationWillTerminate:(UIApplication *)application {
self.appStatus = @"terminate";
}
@end