• 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

    细节

  • 相关阅读:
    SQLServer之创建AFETER DELETE触发器
    SQLServer之创建DML AFTER UPDATE触发器
    SQLServer之创建DML AFTER INSERT触发器
    css制作环形进度条
    curl教程2
    windows下更改鼠标滚轮方向
    curl tutorial with examples of usage
    为npm设置代理
    日期类型的input元素设置默认值为当天
    加盐密码哈希:如何正确使用
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4911640.html
Copyright © 2020-2023  润新知