Oh!Coder

Coding Life

Objective C--中介者模式

| Comments

今天和大家分享的模式是中介者模式。

看到这个名字,猜想很多朋友会想到中介机构。其实,中介者模式的功能就是充当了中介结构。下面还是先给出定义。

中介模式(Mediator),用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使用其耦合松散,而且可以独立地改变它们之间的交互。

下面先给出类结构图,再做简单解释。

pic

中介者模式很容易在系统中引用,但是也比较容易误用。所以当系统出现了“多对多”交互复杂的对象群时,不要急于使用中介者模式,而要先反思系统在设计上是不是合理。

下面我们来说一说,中介者模式的优缺点。中介者的优点首先是Mediator的出现减少了各个Colleague的耦合,使得可以独立地改变和复用各个Colleague类和Mediator。其次,由于把对象如何协作进行了抽象,将中介作为一个独立的概念并将其封装在一个对象中,这样关注的对象就从对象各自本身的行为转移到它们之间的交互上来,也就是站在一个更宏观的角度去看待系统。

相对来说,缺点也很明显。由于ConcreteMediator控制了集中化,于是就把交互复杂性变为了中介者的复杂性,这就使得中介者会变得比任何一个ConcreteColleage都复杂。所以一旦ConcreteMediator崩溃,那么整个系统都会受到影响。

还是那句老话,世上没有银弹,合适的就是最好的!

下面给大家简单展示一下具体实现。

注意:本文所有代码均在ARC环境下编译通过。

  • Mediator类接口
1
2
3
4
5
6
7
#import <Foundation/Foundation.h>

@class Colleague;
@interface Mediator :NSObject
-(void)Send:(NSString*)message
           :(Colleague*)colleague;
@end
  • Mediator类实现
1
2
3
4
5
6
7
#import "Mediator.h"

@implementation Mediator
-(void)Send:(NSString *)message :(Colleague *)colleague{
    return;
}
@end
  • Colleague类接口
1
2
3
4
5
6
7
8
#import <Foundation/Foundation.h>

@class Mediator;
@interface Colleague :NSObject{
    Mediator *myMediator;
}
-(Colleague*)MyInit:(Mediator*)mediator;
@end
  • Colleague类实现
1
2
3
4
5
6
7
8
9
10
#import "Colleague.h"

@implementation Colleague
-(Colleague*)MyInit:(Mediator *)mediator{
    if (self == [super init]) {
        myMediator = mediator;
    }
    return self;
}
@end
  • ConcreteMediator类接口
1
2
3
4
5
6
7
8
#import "Mediator.h"

@class ConcreteColleague1;
@class ConcreteColleague2;
@interface ConcreteMediator :Mediator
@property ConcreteColleague1*colleague1;
@property ConcreteColleague2*colleague2;
@end
  • ConcreteMediator类实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#import "ConcreteMediator.h"
#import "ConcreteColleague1.h"
#import "ConcreteColleague2.h"
#import "Colleague.h"

@implementation ConcreteMediator
@synthesize colleague1;
@synthesize colleague2;

-(void)Send:(NSString *)message :(Colleague *)colleague{
    if ([colleague isKindOfClass:[ConcreteColleague1 class]]) {
        [colleague2 Notify:message];
    }
    else {
        [colleague1 Notify:message];
    }
}
@end
  • ConcreteColleague1类接口
1
2
3
4
5
6
7
8
#import "Colleague.h"

@class Mediator;
@interface ConcreteColleague1 :Colleague
-(ConcreteColleague1*)MyInit:(Mediator*)mediator;
-(void)Send:(NSString*)message;
-(void)Notify:(NSString*)message;
@end
  • ConcreteColleague1类实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#import "ConcreteColleague1.h"
#import "Mediator.h"

@implementation ConcreteColleague1
-(ConcreteColleague1*)MyInit:(Mediator*)mediator{
    if (self == [super init]) {
        myMediator = mediator;
    }
    return self;
}
-(void)Send:(NSString *)message{
    [myMediator Send:message :self];
}
-(void)Notify:(NSString *)message{
    NSLog(@"ConcreteColleague1 got message:%@", message);
}
@end
  • ConcreteColleague2类接口
1
2
3
4
5
6
7
8
#import "Colleague.h"

@class Mediator;
@interface ConcreteColleague2 :Colleague
-(ConcreteColleague2*)MyInit:(Mediator*)mediator;
-(void)Send:(NSString*)message;
-(void)Notify:(NSString*)message;
@end
  • ConcreteColleague2类实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#import "ConcreteColleague2.h"
#import "Mediator.h"

@implementation ConcreteColleague2
-(ConcreteColleague2*)MyInit:(Mediator*)mediator{
    if (self == [super init]) {
        myMediator = mediator;
    }
    return self;
}
-(void)Send:(NSString *)message{
    [myMediator Send:message :self];
}
-(void)Notify:(NSString *)message{
    NSLog(@"ConcreteColleague2 got message:%@", message);
}
@end
  • Main方法调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#import <Foundation/Foundation.h>
#import "ConcreteMediator.h"
#import "ConcreteColleague1.h"
#import "ConcreteColleague2.h"

int main(int argc,const char * argv[])
{
    @autoreleasepool{
        ConcreteMediator *m = [[ConcreteMediator alloc]init];
        ConcreteColleague1 *c1 = [[ConcreteColleague1 alloc]MyInit:m];
        ConcreteColleague2 *c2 = [[ConcreteColleague2 alloc]MyInit:m];
        [m setColleague1:c1];
        [m setColleague2:c2];
        [c1 Send:@"Good morning"];
        [c2 Send:@"Good afternoon"];
    }
    return 0;
}

完工!

Comments