iOS设计模式 - 中介者
iOS设计模式 - 中介者
原理图
说明
用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
注:中介者对象本身没有复用价值,只是将逻辑操作封装在一个类里面而已
源码
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 * ];
self.textField_3.text = [NSString stringWithFormat:@"%.f", currentNum.floatValue * ]; } 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 / ];
self.textField_3.text = [NSString stringWithFormat:@"%.f", currentNum.floatValue * ]; } else { NSString *currentNum = [self currentStringWithTextField:textField replacementString:string inRange:range];
self.textField_1.text = [NSString stringWithFormat:@"%.f", currentNum.floatValue / ];
self.textField_2.text = [NSString stringWithFormat:@"%.f", currentNum.floatValue / ];
} 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(, + * , , )];
self.textField_2 = [self createTextFieldWithFrame:CGRectMake(, + * , , )];
self.textField_3 = [self createTextFieldWithFrame:CGRectMake(, + * , , )];
[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
细节
iOS设计模式 - 中介者的更多相关文章
- IOS设计模式之一(MVC模式,单例模式)
iOS 设计模式-你可能已经听说过这个词,但是你真正理解它意味着什么吗?虽然大多数的开发者可能都会认为设计模式是非常重要的,然而关于设计模式这一主题的文章却不多,并且有时候我们开发者在写代码的时候也不 ...
- iOS书摘之Objective-C编程之道 iOS设计模式解析
来自<Objective-C编程之道iOS设计模式解析>一书的摘要总结 一.Prototype 原型模式 定义:使用原型实例指定创建对象的种类,并通过复制这个原型创建新的对象.(<设 ...
- iOS设计模式 - (1)概述
近期可自由安排的时间比較多, iOS应用方面, 没什么好点子, 就先放下, 不写了.花点时间学学设计模式. 之后将会写一系列博文, 记录设计模式学习过程. 当然, 由于我自己是搞iOS的, 所以之后设 ...
- 7. 星际争霸之php设计模式--中介者模式
题记==============================================================================本php设计模式专辑来源于博客(jymo ...
- iOS 设计模式之工厂模式
iOS 设计模式之工厂模式 分类: 设计模式2014-02-10 18:05 11020人阅读 评论(2) 收藏 举报 ios设计模式 工厂模式我的理解是:他就是为了创建对象的 创建对象的时候,我们一 ...
- iOS设计模式之生成器
iOS设计模式之生成器 1.生成器模式的定义 (1): 将一个复杂的对象的构件与它的表示分离,使得相同的构建过程能够创建不同的表示 (2): 生成器模式除了客户之外还包括一个Director(指导者) ...
- IOS设计模式之三:MVC模式
IOS设计模式之三:MVC模式 模型-视图-控制器 这个模式其实应该叫做MCV,用控制器把model与view隔开才对,也就是model与view互相不知道对方的存在,没有任何瓜葛,他们就像一个团 ...
- iOS设计模式 - 享元
iOS设计模式 - 享元 原理图 说明 享元模式使用共享物件,用来尽可能减少内存使用量以及分享资讯给尽可能多的相似物件:它适合用于只是因重复而导致使用无法令人接受的大量内存的大量物件.通常物件中的部分 ...
- iOS设计模式 - 责任链
iOS设计模式 - 责任链 原理图 说明 在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链.请求在这个链上传递,直到链上的某一个对象决定处理此请求.发出这个请求的客户端并不知道链 ...
随机推荐
- 不用bootstrap,只用CSS创建网格布局
本文译自[http://j4n.co/blog/Creating-your-own-css-grid-system],英语好的,可直接查看原网页,不需要FQ. 翻译拿不准的地方会有英文原文,方便大家理 ...
- Python WSGI接口
WSGI(Web Server Gateway Interface 或 Python Web Server Gateway Interface ),是为 Python 语言定义的 Web 服务器与 W ...
- UIKit 框架之Bar、Controller
UIKit框架中有各种Bar,UITabBar.UINavigationBar.UIToolbar.Bar对应的就有一些Item,tabBarItem.navigationItem.toolbarIt ...
- [转]Extending the User Interface in Outlook 2010
本文转自:https://msdn.microsoft.com/en-us/library/office/ee692172%28v=office.14%29.aspx#OfficeOLExtendin ...
- SQL中利用ROW_NUMBER()进行分页查询
SELECT ContractName ,ContractNO, State,CreateDate FROM (SELECT ContractName ,ContractNO,CreateDate, ...
- .NET创建WebService服务简单的例子
Web service是一个基于可编程的web的应用程序,用于开发分布式的互操作的应用程序,也是一种web服务 WebService的特性有以下几点: 1.使用XML(标准通用标记语言)来作为数据交互 ...
- jade——创建第一个jade模板
什么是jade? jade是node.js的一个模板引擎,参考了haml的语法,是简写的html语言. 使用单个标签代替双标签,类似于Python,通过缩进来确定从属关系,没有结束符号,非常简洁,使用 ...
- MFC数据库操作
本例采用Microsoft SQL2008建立的一个数据库表 /****链接数据库操作**/ 在stdafx.h的头文件中加入 #import "C:\Program Files\Commo ...
- cookie封装函数与使用方法(转)
函数封装: var Cookie = function(name, value, options) { // 如果第二个参数存在 if (typeof value != 'undefined') { ...
- 浏览器根对象window之事件
1. [事件]动画 onanimationstart onanimationend onanimationiteration onwebkitanimationend onwebkitanimatio ...