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 * 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

 

细节

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值