iOS design mode-Mediator

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.