iOS设计模式 - 中介者
2015-10-26 22:46
477 查看
iOS设计模式 - 中介者
![](http://images2015.cnblogs.com/blog/607542/201510/607542-20151026163054497-85330009.gif)
原理图
![](http://images2015.cnblogs.com/blog/607542/201510/607542-20151026163027404-1425337260.png)
说明
用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
注:中介者对象本身没有复用价值,只是将逻辑操作封装在一个类里面而已
源码
https://github.com/YouXianMing/iOS-Design-Patterns
细节
![](http://images2015.cnblogs.com/blog/607542/201510/607542-20151026163054497-85330009.gif)
原理图
![](http://images2015.cnblogs.com/blog/607542/201510/607542-20151026163027404-1425337260.png)
说明
用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
注:中介者对象本身没有复用价值,只是将逻辑操作封装在一个类里面而已
源码
https://github.com/YouXianMing/iOS-Design-Patterns
// // TextFieldMediator.h // MediatorPattern // // Created by YouXianMing on 15/10/26. // Copyright © 2015年 ZiPeiYi. All rights reserved. // #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface TextFieldMediator : NSObject <UITextFieldDelegate> @property (nonatomic, weak) UITextField *textField_1; @property (nonatomic, weak) UITextField *textField_2; @property (nonatomic, weak) UITextField *textField_3; @end
// // TextFieldMediator.m // MediatorPattern // // Created by YouXianMing on 15/10/26. // Copyright © 2015年 ZiPeiYi. All rights reserved. // #import "TextFieldMediator.h" @implementation TextFieldMediator - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if ([textField isEqual:self.textField_1]) { NSString *currentNum = [self currentStringWithTextField:textField replacementString:string inRange:range]; self.textField_2.text = [NSString stringWithFormat:@"%.f", currentNum.floatValue * 7]; self.textField_3.text = [NSString stringWithFormat:@"%.f", currentNum.floatValue * 14]; } else if ([textField isEqual:self.textField_2]) { NSString *currentNum = [self currentStringWithTextField:textField replacementString:string inRange:range]; self.textField_1.text = [NSString stringWithFormat:@"%.f", currentNum.floatValue / 7]; self.textField_3.text = [NSString stringWithFormat:@"%.f", currentNum.floatValue * 2]; } else { NSString *currentNum = [self currentStringWithTextField:textField replacementString:string inRange:range]; self.textField_1.text = [NSString stringWithFormat:@"%.f", currentNum.floatValue / 14]; self.textField_2.text = [NSString stringWithFormat:@"%.f", currentNum.floatValue / 2]; } return YES; } - (NSString *)currentStringWithTextField:(UITextField *)textField replacementString:(NSString *)string inRange:(NSRange)range { NSMutableString *mutableString = [NSMutableString stringWithString:textField.text]; if (string.length) { [mutableString insertString:string atIndex:range.location]; } else { [mutableString deleteCharactersInRange:range]; } return [NSString stringWithString:mutableString]; } @end
// // ViewController.m // MediatorPattern // // Created by YouXianMing on 15/10/26. // Copyright © 2015年 ZiPeiYi. All rights reserved. // #import "ViewController.h" #import "TextFieldMediator.h" @interface ViewController () <UITextFieldDelegate> @property (nonatomic, strong) UITextField *textField_1; @property (nonatomic, strong) UITextField *textField_2; @property (nonatomic, strong) UITextField *textField_3; @property (nonatomic, strong) TextFieldMediator *mediator; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 初始化控件 self.textField_1 = [self createTextFieldWithFrame:CGRectMake(10, 30 + 40 * 0, 250, 30)]; self.textField_2 = [self createTextFieldWithFrame:CGRectMake(10, 30 + 40 * 1, 250, 30)]; self.textField_3 = [self createTextFieldWithFrame:CGRectMake(10, 30 + 40 * 2, 250, 30)]; [self.view addSubview:self.textField_1]; [self.view addSubview:self.textField_2]; [self.view addSubview:self.textField_3]; // 初始化中介者 self.mediator = [[TextFieldMediator alloc] init]; self.mediator.textField_1 = self.textField_1; self.mediator.textField_2 = self.textField_2; self.mediator.textField_3 = self.textField_3; // 将代理设置成中介者 self.textField_1.delegate = self.mediator; self.textField_2.delegate = self.mediator; self.textField_3.delegate = self.mediator; } - (UITextField *)createTextFieldWithFrame:(CGRect)frame { UITextField *tmpField = [[UITextField alloc] initWithFrame:frame]; tmpField.layer.borderWidth = 0.5f; tmpField.keyboardType = UIKeyboardTypeNumberPad; return tmpField; } @end
细节
![](http://images2015.cnblogs.com/blog/607542/201510/607542-20151026163826669-107143328.png)
相关文章推荐
- iOS 3d touch设计的秘密
- 1.5-nagios监控客户端-1
- 1.4-nagios安装
- 如何使用自己已经编译过的lamp安装cacti nagios zabbix
- ios 中的开源框架
- 蓝懿iOS 技术内容和心得 10。26
- 通知中心
- 我的iOS学习历程 - OC第一天
- 我的iOS学习历程 - OC第一天
- iOS launchScreen页面如何加载动画
- iOS中相对1970的时间转换为现在时间
- 时间与日期详解
- 时间与日期类封装
- 手机开发-IOS
- iOS中的GCD总结
- 博主自荐
- IOS 第三方库- AFNetworking
- 实战iOS7之NSURLSession
- iOS8定位代理方法不回调,解决办法
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制