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设计模式 - 责任链 原理图 说明 在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链.请求在这个链上传递,直到链上的某一个对象决定处理此请求.发出这个请求的客户端并不知道链 ...
随机推荐
- ActiveMQ开发注意要点
目录1.如何保证消息的成功处理2.避免消息队列的并发3.消息有效期的管理4.过期消息,处理失败的消息如何处理 1.保证消息的成功处理消息发送成功后,接收端接收到了消息.然后进行处理,但是可能由于某种原 ...
- 如何在python3环境下的Django中使用MySQL数据库
我们在使用Django过程中,连接MySQL数据库时,对Python不同的版本对应的库也不一样,用惯了Python2的人在使用Python3时经常会遇到下面的错误: Error loading MyS ...
- Redis的服务命令(实现开机自启动)
在Redis的安装目录下,有一个redis.windows-service.conf文件,即默认的配置文件, 如果需要修改端口号,或者设置密码就需要修改其中的内容: 默认端口号是6379,你可以随意修 ...
- shell:syntax error:unexpected end of file/Starting proxy www-balancer: cannot bind socket--转载
src:http://www.2cto.com/os/201308/238962.html 执行某bash脚本是发生: syntax error: unexpected end of file 主 ...
- JSON必知必会
知识点比较杂,简单的以列表形式罗列知识点 1.json是基于javascript对象字面量的,所以他们看起来很像.但是js对象字面量不需要给名称-值对中的名称两边加上双引号.而在JSON中,却是必要的 ...
- 十九、curator recipes之PathChildrenCache
简介 curator可以监听路径下子节点的变更操作,如创建节点,删除节点 官方文档:http://curator.apache.org/curator-recipes/path-cache.html ...
- 高并发第十一弹:J.U.C -AQS(AbstractQueuedSynchronizer) 组件:Lock,ReentrantLock,ReentrantReadWriteLock,StampedLock
既然说到J.U.C 的AQS(AbstractQueuedSynchronizer) 不说 Lock 是不可能的.不过实话来说,一般 JKD8 以后我一般都不用Lock了.毕竟sychronize ...
- mysql行转列,列转行
行转列,列转行是我们在开发过程中经常碰到的问题.行转列一般通过CASE WHEN 语句来实现,也可以通过 SQL SERVER 2005 新增的运算符PIVOT来实现.用传统的方法,比较好理解.层次清 ...
- web页面相关的一些常见可用字符介绍——张鑫旭
by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=1623 正文开始之前先 ...
- 简单的PHP的任务队列
文章太长,不作过多介绍,反正,文章的头部就说明了大概的意思...原文如下:写了一个简单的队列任务处理.多进程任务,异步任务可能会用到这个(主要是命令行应用)比如,任务的某个一个环节速度十分不稳定,可能 ...