美文网首页
iOS设计模式 (十)中介者模式

iOS设计模式 (十)中介者模式

作者: handsome5 | 来源:发表于2018-05-12 18:27 被阅读24次
    概念
    • 中介者模式,定义了一个中介对象来封装一系列对象之间的交互关系。中介者使各个对象之间不需要显式地相互引用,从而使耦合性降低,而且可以独立地改变它们之间的交互行为。通俗的讲就是如果你想租房,一般不会去找房东,房东会把房源租金注意事项交给中介,然后你直接找中介就可以租房了。


      9.png
    中介者UML
    中介者.png
    demo实例
    • AbstractColleage
    
    #import <Foundation/Foundation.h>
    
    @class AbstractColleage;
    
    @protocol colleageDelegate<NSObject>
    
    - (void)colleageValue:(AbstractColleage *)event;
    
    @end
    
    @interface AbstractColleage : NSObject
    
    @property (nonatomic, weak) id <colleageDelegate>delegate;
    
    @end
    
    #import "AbstractColleage.h"
    
    @implementation AbstractColleage
    
    @end
    
    • Colleage 类
    #import "AbstractColleage.h"
    
    @interface Colleage : AbstractColleage
    
    - (void)changeValue:(float)value;
    
    @property (nonatomic, assign)float value;
    
    @end
    
    #import "Colleage.h"
    
    @implementation Colleage
    
    - (void)changeValue:(float)value{
        self.value = value;
        if (self.delegate && [self.delegate respondsToSelector:@selector(colleageValue:)]) {
            [self.delegate colleageValue:self];
        }
    }
    
    @end
    
    • AbstractMediator
    #import <Foundation/Foundation.h>
    #import "AbstractColleage.h"
    
    @interface AbstractMediator : NSObject <colleageDelegate>
    
    @end
    #import "AbstractMediator.h"
    
    
    @implementation AbstractMediator
    
    - (void)colleageValue:(AbstractColleage *)event
    {
        
    }
    
    @end
    
    • MediatorOne
    #import "AbstractMediator.h"
    #import "Colleage.h"
    
    @interface MediatorOne : AbstractMediator
    
    @property (nonatomic, strong)Colleage *colleageA;
    @property (nonatomic, strong)Colleage *colleageB;
    @property (nonatomic, strong)Colleage *colleageC;
    
    
    - (NSDictionary *)values;
    
    @end
    #import "MediatorOne.h"
    
    @implementation MediatorOne
    
    - (NSDictionary *)values
    {
        return  @{
                  @"A":@(self.colleageA.value),
                  @"B":@(self.colleageB.value),
                  @"C":@(self.colleageC.value),
                  };
        
    }
    
    - (void)colleageValue:(AbstractColleage *)event
    {
        if ([event isEqual:self.colleageA]) {
        
            self.colleageB.value = self.colleageA.value *4;
            self.colleageC.value = self.colleageA.value *6;
        }
        else if ([event isEqual:self.colleageB]){
            self.colleageA.value = self.colleageA.value *2;
            self.colleageB.value = self.colleageB.value /2;
            self.colleageC.value = self.colleageC.value /5;
        }
        else {
            self.colleageA.value = self.colleageA.value *2;
            self.colleageB.value = self.colleageB.value *3;
            self.colleageC.value = self.colleageC.value *4;
        }
    }
    
    @end
    
    demo地址:https://github.com/defuliu/MediatorPattern

    相关文章

      网友评论

          本文标题:iOS设计模式 (十)中介者模式

          本文链接:https://www.haomeiwen.com/subject/tzsjlftx.html