在iOS里,设计模式有如下几种:
MVC、工厂、代理、kvo、通知(NSNotification)模式、单例模式
下面一一介绍
1.MVC模式:
现在绝大部分项目都是基于MVC模式,但是有一部分开发者采用MVVC和MVP模式
MVC模式 :(Model - View - Controller)
model:主要处理数据,Model中经常含有多个字段,通常来说就是存储数据。但是Model层也会有一部分简单的逻辑处理:(比如与系统重名关键字处理)
View :不应该包含逻辑,应该根据模型直接获取数据
Controller :大部分的交互逻辑都集中在此处,所有View需要的数据,都是通过Controller提取Model,然后再交给View去展示数据
MVVC模式 :(Model - View - ViewModel - Controller)是一个MVC的改进版本。 一般我们在正式连接了视图(View)和控制器(Controller)后,将标示逻辑从Controller中移除,放到一个一个新对象(View Model里面)。 MVVC 的优点:1.可以兼容你当下的MVC模式,2.增加应用的可测试性,3配合一个绑定机制效果更好
MVP模式:(Model - View -protocal) MVP中的Model相当于MVVC中的View Model 层,Model里面既有逻辑层处理,又有提供协议对应的各种属性以及服务。
2.KVC/KVO
有时候需要监听某个类的属性值的变化而做出相应的改变,这时候就会用到KVC和KVO设计模式。比如在项目中,我需要监听model的某个属性值的变化,当变化时候,需要更新UI显示,这时候用KVC/KVO设计模式就很方便
KVC : (Key - Value Conding) 是键值编码。是指间接访问属性,使用字符串来标示属性,而不通过调用存取方法(setter,getter),或是直接实例化变量(如model.name)
KVO:(key - Value Observing) 添加观察者,对某一个对象的某一个属性感兴趣,当这个属性发生变化的时候,会通知观察者
一个对象拥有某些属性。比如说,一个 Person 对象有一个 name 和一个 address 属性。以 KVC 说法,Person 对象分别有一个 value 对应他的 name 和 address 的 key。 key 只是一个字符串,它对应的值可以是任意类型的对象。从最基础的层次上看,KVC 有两个方法:一个是设置 key 的值,另一个是获取 key 的值。如下面的例子:
void changeName(Person *p, NSString *newName)
{
// using the KVC accessor (getter) method
NSString *originalName = [p valueForKey:@"name"];
// using the KVC accessor (setter) method.
[p setValue:newName forKey:@"name"];
NSLog(@"Changed %@'s name to: %@", originalName, newName);
}
现在,如果 Person 有另外一个 key 配偶(spouse),spouse 的 key 值是另一个 Person 对象,用 KVC 可以这样写:
void logMarriage(Person *p)
{
// just using the accessor again, same as example above
NSString *personsName = [p valueForKey:@"name"];
// this line is different, because it is using
// a "key path" instead of a normal "key"
NSString *spousesName = [p valueForKeyPath:@"spouse.name"];
NSLog(@"%@ is happily married to %@", personsName, spousesName);
}
key 与 key path 要区分开来,key 可以从一个对象中获取值,而 key path 可以将多个 key 用点号 "." 分割连接起来,比如:
[p valueForKeyPath:@"spouse.name"];
相当于这样……
[[p valueForKey:@"spouse"] valueForKey:@"name"];
好了,以上是 KVC 的基本知识,接着看看 KVO。
Key-Value Observing (KVO)
Key-Value Observing (KVO) 建立在 KVC 之上,它能够观察一个对象的 KVC key path 值的变化。举个例子,用代码观察一个 person 对象的 address 变化,以下是实现的三个方法:
- watchPersonForChangeOfAddress: 实现观察
- observeValueForKeyPath:ofObject:change:context: 在被观察的 key path 的值变化时调用。
- dealloc 停止观察
static NSString *const KVO_CONTEXT_ADDRESS_CHANGED = @"KVO_CONTEXT_ADDRESS_CHANGED"
@implementation PersonWatcher
-(void) watchPersonForChangeOfAddress:(Person *)p
{
// this begins the observing
[p addObserver:self
forKeyPath:@"address"
options:0
context:KVO_CONTEXT_ADDRESS_CHANGED];
// keep a record of all the people being observed,
// because we need to stop observing them in dealloc
[m_observedPeople addObject:p];
}
// whenever an observed key path changes, this method will be called
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
// use the context to make sure this is a change in the address,
// because we may also be observing other things
if(context == KVO_CONTEXT_ADDRESS_CHANGED) {
NSString *name = [object valueForKey:@"name"];
NSString *address = [object valueForKey:@"address"];
NSLog(@"%@ has a new address: %@", name, address);
}
}
-(void) dealloc;
{
// must stop observing everything before this object is
// deallocated, otherwise it will cause crashes
for(Person *p in m_observedPeople){
[p removeObserver:self forKeyPath:@"address"];
}
[m_observedPeople release];
m_observedPeople = nil;
[super dealloc];
}
-(id) init;
{
if(self = [super init]){
m_observedPeople = [NSMutableArray new];
}
return self;
}
@end
这就是 KVO 的作用,它通过 key path 观察对象的值,当值发生变化的时候会收到通知。
3.通知(NSNotification)模式
通知在开发中必不可少的,对于跨模块的类交互,需要使用通知;对于多对多关系,使用通知更好实现。
//添加 字典,将label的值通过key值设置传递
NSDictionary *dict =[[NSDictionary alloc]initWithObjectsAndKeys:value1,key1,value2,key2,nil];
//创建通知
NSNotification *notification =[NSNotification notificationWithName:@"customNotificate" object:niluserInfo:dict];
//通过通知中心发送通知
[[NSNotificationCenter defaultCenter] postNotification:notification];
//注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificate:)name:@"customNotificate" object:nil];//自定义notificate:方法
4.单例模式
单例就是指全局中只有一个对象存在,用于全局共享资源,每个APP都有单例如UIApplication ,如系统的单例:NSUserDefault,NSFileManager,NSNotificationCenter等等。一般应用场景管理类,引擎类,比如我们一般做用户信息储存时,通常会用单例储存,因为在一些应用操作的时候,经常需要先登陆。单例也有缺点,单例一旦创建,在APP整个使用过程中,都不会被销毁,这占用一定的内存空间,因此不可滥用单例。
具体步骤:
1 在类的内部提供一个static修饰的全局变量
2 提供一个类方法,方便外界访问
3 重写+allocWithZone方法,保证永远都只为单例对象分配一次内存空间
4 严谨起见,重写-copyWithZone方法和-MutableCopyWithZone方法
#import "Tools.h"
@implementation Tools
// 创建静态对象 防止外部访问
static Tools *_instance;
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
// @synchronized (self) {
// // 为了防止多线程同时访问对象,造成多次分配内存空间,所以要加上----线程锁
// if (_instance == nil) {
// _instance = [super allocWithZone:zone];
// }
// return _instance;
// }
// 也可以使用一次性代码
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (_instance == nil) {
_instance = [super allocWithZone:zone];
}
});
return _instance;
}
// 为了使实例易于外界访问 我们一般提供一个类方法
// 类方法命名规范 share类名|default类名|类名
+(instancetype)shareTools
{
return [[self alloc]init];
}
// 严谨写法,重写copyWithZone 和 mutableCopyWithZone
-(id)copyWithZone:(NSZone *)zone
{
return _instance;
}
-(id)mutableCopyWithZone:(NSZone *)zone
{
return _instance;
}
5.代理模式
Delegate就是代理,代理是一种设置模式。在IOS开发中会使用大量的代理模式,常见的应用场景就是反向传值。
实现A类向B类传值:
A类: .h
@protocol TopButDelegate <NSObject>//协议
- (void)transButIndex:(NSInteger)index;//协议方法
@end
@interface AddressBarView : UIView
@property (nonatomic, assign) id<TopButDelegate>delegate;//代理属性
- (void)ClickBut:(UIButton *)sender;//此方法执行时判断协议方法的执行情况
.m
//代理方法, 通过BUT 下标 来滑动视图
- (void)ClickBut:(UIButton *)sender{
if (self.delegate && [self.delegate respondsToSelector:@selector(transButIndex:)]) {
//代理存在且有这个transButIndex:方法
[self.delegate transButIndex:sender.tag - 1000];
}
}
B类:.m
@interface B ()<TopButDelegate>
@property (nonatomic, retain) A * bar; //A类
- (void)viewDidLoad {
self.bar = [[AddressBarView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 40)];
[self.view addSubview: self.bar];
self.bar.delegate = self;//给A类的代理属性给self(B类),这样B就可以执行协议方法啦
[self.bar ClickBut:but];
}
//执行协议方法
- (void)transButIndex:(NSInteger)index
{
self.baseCollection.contentOffset = CGPointMake(index * self.baseCollection.frame.size.width, 0);
}
6.工厂方法
在项目中大量使用工厂设计模式,特别是生成控件的API,已经封装成一套,全部都是扩展的类方法,这样可以简化很多代码
--------例子就不举了,实际上就是类方法,省去了实例化对象的步骤,很方便