阅读 339

ios-中介者模式

中介者模式是一种行为设计模式,能让你减少对象之间混乱无序的依赖关系,该模式会限制对象之间的直接交互,迫使他们通过一个中介对象进行合作。让对象之间互不感知。

  • 两个人对话面对面就可以交流了
  • 但是两个人被互相隔开,这个时候就用手机打电话了,手机就可以理解为两个人的中介
  • 两个人都持有手机中介,用手机来完成两个人的交流

源码

  • 交流者基本类
#import <Foundation/Foundation.h>

@class Mediator;
@interface Colleague : NSObject

@property(nonatomic, strong)Mediator *mediator;

-(instancetype)initWithMediator:(Mediator *)mediator;

-(void)notify:(NSString *)message;
-(void)send:(NSString *)message;
@end
#import "Colleague.h"

@implementation Colleague

@end
复制代码
  • 用户1
#import "Colleague.h"

@interface ConcreteColleague1 : Colleague

@end

#import "ConcreteColleague1.h"
#import "Mediator.h"

@implementation ConcreteColleague1

@synthesize mediator = _mediator;

-(instancetype)initWithMediator:(Mediator *)mediator{
    self = [super init];
    if (self) {
        _mediator = mediator;
    }
    return self;
}

-(void)send:(NSString *)message{
    NSLog(@"同事1发送了信息");
    [_mediator send:message colleague:self];
}

-(void)notify:(NSString *)message{
    NSLog(@"%@%@",@"同事1得到消息:", message);
}

@end

复制代码
  • 用户2

#import "Colleague.h"

@interface ConcreteColleague2 : Colleague

@end

#import "ConcreteColleague2.h"
#import "Mediator.h"

@implementation ConcreteColleague2
@synthesize mediator = _mediator;

-(instancetype)initWithMediator:(Mediator *)mediator{
    self = [super init];
    if (self) {
        _mediator = mediator;
    }
    return self;
}

-(void)send:(NSString *)message{
    NSLog(@"同事2发送了信息");
    [_mediator send:message colleague:self];
}

-(void)notify:(NSString *)message{
    NSLog(@"%@%@",@"同事2得到消息", message);
}
@end


复制代码
  • 中介者基类
#import <Foundation/Foundation.h>

@class Colleague;

@interface Mediator : NSObject

@property(nonatomic, strong)Colleague *colleague1;
@property(nonatomic, strong)Colleague *colleague2;

-(void)send:(NSString *)message colleague:(Colleague *)colleague;


@end

#import "Mediator.h"

@implementation Mediator

@end



复制代码
  • 中介类的实现类 其实这两个可以合并一下不需要很复杂
#import "Mediator.h"

@class Colleague;

@interface ConcreteMediator : Mediator



@end

#import "ConcreteMediator.h"
#import "ConcreteColleague1.h"
#import "ConcreteColleague2.h"

@implementation ConcreteMediator
@synthesize colleague1 =_colleague1;
@synthesize colleague2 = _colleague2;

-(void)send:(NSString *)message colleague:(Colleague *)colleague{
    if (colleague == _colleague1) {
        [_colleague2 notify:message];
    }else{
        [_colleague1 notify:message];
    }
}

@end
复制代码
  • 完成两个对象之间的通信
#import "ViewController.h"
#import "ConcreteColleague1.h"
#import "ConcreteColleague2.h"
#import "ConcreteMediator.h"


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化一个中介者对象
    ConcreteMediator *mediator = [[ConcreteMediator alloc]init];
    //初始化并且让两个同事有相同的中介者对象
    ConcreteColleague1 *c1 = [[ConcreteColleague1 alloc]initWithMediator:mediator];
    ConcreteColleague2 *c2 = [[ConcreteColleague2 alloc]initWithMediator:mediator];
    //给中介者对象绑定两个要交互的同事对象
    mediator.colleague1 = c1;
    mediator.colleague2 = c2;
    
    [c1 send:@"吃过饭了吗?"];
    [c2 send:@"没有呢,你打算请客?"];
    
}

@end

复制代码
  • 架构MVP架构就是利用中介者模式实现的
文章分类
iOS
文章标签