iOS design mode-Mediator
Schematic diagram
Description
Encapsulates a series of object interactions with a mediation object. The mediator makes the objects not need to explicitly reference each other, so that they are loosely coupled, and can independently change the interaction between them.
Note: The Mediator object itself has no reuse value, just encapsulates the logical operation inside a class.
Source
Https://github.com/YouXianMing/MediatorPattern
////TextFieldMediator.h//Mediatorpattern////Created by youxianming on 15/10/26.//copyright©2015 year Zipeiyi. All rights reserved.//#import<Foundation/Foundation.h>#import<UIKit/UIKit.h>@interfaceTextfieldmediator: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 year Zipeiyi. All rights reserved.//#import "TextFieldMediator.h"@implementationTextfieldmediator-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *)string { if([TextField isEqual:self.textField_1]) {nsstring*currentnum = [Self Currentstringwithtextfield:textfield replacementstring:stringInrange:range]; Self.textField_2.text= [NSString stringWithFormat:@"%.F", Currentnum.floatvalue *7]; Self.textField_3.text= [NSString stringWithFormat:@"%.F", Currentnum.floatvalue * -]; } Else if([TextField isEqual:self.textField_2]) {nsstring*currentnum = [Self Currentstringwithtextfield:textfield replacementstring:stringInrange: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:stringInrange:range]; Self.textField_1.text= [NSString stringWithFormat:@"%.F", Currentnum.floatvalue/ -]; Self.textField_2.text= [NSString stringWithFormat:@"%.F", Currentnum.floatvalue/2]; } returnYES;}-(NSString *) Currentstringwithtextfield: (Uitextfield *) TextField replacementstring: (NSString *)stringInRange: (nsrange) range {nsmutablestring*mutablestring =[nsmutablestring StringWithString:textField.text]; if(string. Length) {[Mutablestring insertstring:stringAtIndex:range.location]; } Else{[mutablestring deletecharactersinrange:range]; } return[NSString stringwithstring:mutablestring];}@end
////VIEWCONTROLLER.M//Mediatorpattern////Created by youxianming on 15/10/26.//copyright©2015 year Zipeiyi. All rights reserved.//#import "ViewController.h"#import "TextFieldMediator.h"@interfaceViewcontroller () <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@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //Initializing the controlSelf.textfield_1 = [Self Createtextfieldwithframe:cgrectmake (Ten, -+ +*0, -, -)]; Self.textfield_2= [Self Createtextfieldwithframe:cgrectmake (Ten, -+ +*1, -, -)]; Self.textfield_3= [Self Createtextfieldwithframe:cgrectmake (Ten, -+ +*2, -, -)]; [Self.view AddSubview:self.textField_1]; [Self.view AddSubview:self.textField_2]; [Self.view AddSubview:self.textField_3]; //Initialize MediatorSelf.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; //set a proxy as a mediatorSelf.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; returnTmpfield;}@end
Details
iOS design mode-Mediator