【原】IOS中KVO模式的解析与应用

时间:2023-12-31 17:34:14

最近老翁在项目中多处用到了KVO,深感这种模式的好处。现总结如下:

一、概述

KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知。简单的说就是每次指定的被观察的对象的属性被修改后,KVO就会自动通知相应的观察者了。

KVO其实也是“观察者”设计模式的一种应用。我的看法是,这种模式有利于两个类间的解耦合,尤其是对于 业务逻辑与视图控制 这两个功能的解耦合。

二、引子

先来看个引子:

有一个业务类:Walker,在这个类内部只负责关于业务逻辑的处理,比如负责从服务器传来的JSON中解析数据,或做其他业务数据上的处理。

有另一个类:ViewController,专门负责界面的交互与试图更新。其中,需要讲Walker的某些属性显示出来,并实时更新。

目前,据我所能想到的方法有以下几种:

方法1、直接的函数调用

Walker的类内部,把创建一个ViewController的对象,然后调用ViewController的修改界面的方法,把需要改动的属性值作为形参传给该函数。

这种方式最直观,因为它不需要绕任何弯子。但是,确实最糟的方法。因为Walker与ViewController这两个类从此紧紧耦合在一起了。记住这句话,处理业务逻辑的类,对外部的事情知道得越少越好。甚至于,要做到外部是否有VC(View Controller),有多少个VC都不影响我。假设这是一个项目,程序员A负责业务逻辑的处理,程序员B负责UI,则采取这种方式后,程序员A就受制于B,互相干扰。

方法2、利用消息通信机制(NSNotification)

在Walker内部建立消息中心NSNotificationCenter,把实例化之后的VC对象作为observer。Center建在Walker或者VC都无所谓,具体看我博客的另一篇文章【NSNotificationCenter总结】。这种方法比前面的方法好一些。但是有一个很大的缺点:如果Walker需要更改的属性很多而且很频繁,那么这种方式很不方便传值。而且,注意到了没,“把实例化后的VC对象作为observer”,始终逃不开在Walker内部对VC实例化。依旧是耦合着。

方法3、利用delegate

关于delegate的介绍有很多,这里就不多讲。但是在这种需求下用 delegate,有点“杀鸡用牛刀”感觉,成本较大,而且不直观。

方法4、利用KVO模式

所有的代码都将在ViewController中实现。对于Walker,它自己都不知道外部是否有VC,以及VC会怎用用我的属性。

三、Demo

 //
// Walker.h
// KVOExample
//
// Created by 老翁 on 13-7-29.
// Copyright (c) 2013年 wzl. All rights reserved.
// #import <Foundation/Foundation.h> @interface Walker : NSObject
{
NSInteger _age;
NSString *_name;
} @property (nonatomic) NSInteger age;
@property (nonatomic, retain) NSString *name; - (id)initWithName:(NSString *)name age:(NSInteger)age; @end
#import "Walker.h"

@implementation Walker

@synthesize age = _age;
@synthesize name = _name; - (void)dealloc
{
[_name release];
[super dealloc];
} - (id)initWithName:(NSString *)name age:(NSInteger)age
{
if (self = [super init]) {
_name = name;
_age = age;
}
return self;
} @end

ViewController代码:

//
// ViewController.h
// KVOExample
//
// Created by 老翁 on 13-7-29.
// Copyright (c) 2013年 wzl. All rights reserved.
// #import <UIKit/UIKit.h>
#import "Walker.h" @class Walker; @interface ViewController : UIViewController
{
Walker *_walker;
UILabel *_ageLabel;
} @property (nonatomic, assign) Walker *walker; @end
 //
// ViewController.m
// KVOExample
//
// Created by 老翁 on 13-7-29.
// Copyright (c) 2013年 wzl. All rights reserved.
// #import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize walker = _walker; - (void)dealloc
{
[_walker release];
[_ageLabel release];
[super dealloc];
} - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_walker = [[Walker alloc] initWithName:@"老翁" age:];
/* 关键步骤 */
[_walker addObserver:self
forKeyPath:@"age"
options:NSKeyValueObservingOptionNew
context:nil]; //UI initialization
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(, , , )];
[btn setBackgroundColor:[UIColor lightGrayColor]];
[btn setTitle:@"增加5岁" forState:UIControlStateNormal];
[btn addTarget:self
action:@selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn]; _ageLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
_ageLabel.text = [NSString stringWithFormat:@"%@现在的年龄是: %d", _walker.name, _walker.age];
_ageLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:_ageLabel]; } - (void)buttonPressed
{
_walker.age += ;
} /* KVO function, 只要object的keyPath属性发生变化,就会调用此函数*/
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"age"] && object == _walker) {
_ageLabel.text = [NSString stringWithFormat:@"%@现在的年龄是: %d", _walker.name, _walker.age];
}
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

【原】IOS中KVO模式的解析与应用

点击了按钮之后,系统会调用 observeValueForKeyPath :函数,这个函数应该也是回调函数。在该函数内部做UI更新。我们以这种轻量级的方式达到了目的。

【原】IOS中KVO模式的解析与应用

##################################################

声明 转载请注明原文地址:

http://www.cnblogs.com/wengzilin/p/3223770.html

欢迎私下交流学习:

QQ 719113951

##################################################