• 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

    细节

  • 相关阅读:
    春秋宋国
    MySQL查询性能优化七种武器之索引下推
    HbuliderX H5前端部署+Nginx+docker20220912
    春秋秦国
    春秋郑国
    docker安装 iotdb
    springboot集成Spring Batch 20220902
    跨境电商
    江西两大文化巨头:抚州有叫板江西的底气,吉安有叫板全国的底气
    CDH搭建教程
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4911640.html
Copyright © 2020-2023  润新知