今天和大家分享的是访问者模式。
为了方便向大家展示,先给出简短的定义。
访问者模式(Visitor),表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作。
紧接着,给出其类结构图。
访问者模式适用于数据结构相对稳定的系统,它把数据结构和作用于结构上的操作之间的耦合解脱开,使得操作结合可以相对自由地演化。
访问者模式的目的是要把处理从数据结构分离出来。很多系统可以按照算法和数据结构分开,如果这样的系统有比较稳定的数据结构,又有易于变化的算法的话,使用访问者模式就是比较合适的,因为访问者模式使得算法操作的增加变得容易。
访问者模式的优点就是增加新的操作很容易,因为增加新的操作就意味着增加一个新的访问者。访问者模式将有关的行为集中到一个访问者对象中。
那其实,访问者模式的缺点也就是使增加新的数据结构变得苦难了。所以,GoF四人中的一个作者增经说过,‘大多时候你并不需要访问者模式,但当一旦你需要访问者模式的时候,那就是真的需要它了’。
那么下面还是老惯例,给大家展示一下简单的实现(再次说明一下,这些代码都使在ARC环境下编译的!)。
-
Visitors类接口
1
2
3
4
5
6
7
|
#import <Foundation/Foundation.h>
@class
ConcreteElementA,ConcreteElementB;
@interface
Visitors :
NSObject
-(
void
)VisitConcreteElementA:(ConcreteElementA *)concreteElementA;
-(
void
)VisitConcreteElementB:(ConcreteElementB *)concreteElementB;
@end
|
-
Visitors类实现
1
2
3
4
5
6
7
8
9
10
11
12
|
#import "Visitors.h"
#import "ConcreteElementA.h"
#import "ConcreteElementB.h"
@implementation
Visitors
-(
void
)VisitConcreteElementA:(ConcreteElementA *)concreteElementA{
return
;
}
-(
void
)VisitConcreteElementB:(ConcreteElementB *)concreteElementB{
return
;
}
@end
|
-
ConcreteVisitor1类接口
1
2
3
4
|
#import "Visitors.h"
@interface
ConcreteVisitor1:Visitors
@end
|
-
ConcreteVisitor1类实现
1
2
3
4
5
6
7
8
9
10
|
#import "ConcreteVisitor1.h"
#import "ConcreteElementA.h"
@implementation
ConcreteVisitor1
-(
void
)VisitConcreteElementA:(ConcreteElementA *)concreteElementA{
NSString
*eleName =
NSStringFromClass
([concreteElementA
class
]);
NSString
*visitorName =
NSStringFromClass
([
self
class
]);
NSLog
(@
"%@被%@访问"
, eleName, visitorName);
}
@end
|
-
ConcreteVisitor2类接口
1
2
3
4
|
#import "Visitors.h"
@interface
ConcreteVisitor2:Visitors
@end
|
-
ConcreteVisitor2类实现
1
2
3
4
5
6
7
8
9
10
|
#import "ConcreteVisitor2.h"
#import "ConcreteElementB.h"
@implementation
ConcreteVisitor2
-(
void
)VisitConcreteElementB:(ConcreteElementB *)concreteElementB{
NSString
*eleName =
NSStringFromClass
([concreteElementB
class
]);
NSString
*visitorName =
NSStringFromClass
([
self
class
]);
NSLog
(@
"%@被%@访问"
, eleName, visitorName);
}
@end
|
-
Elements类接口
1
2
3
4
5
6
|
#import <Foundation/Foundation.h>
@class
Visitors;
@interface
Elements :
NSObject
-(
void
)Accept:(Visitors*)visitor;
@end
|
-
Elements类实现
1
2
3
4
5
6
7
8
|
#import "Elements.h"
#import "Visitors.h"
@implementation
Elements
-(
void
)Accept:(Visitors *)visitor{
return
;
}
@end
|
-
ConcreteElementA类接口
1
2
3
4
5
|
#import "Elements.h"
@interface
ConcreteElementA :Elements
-(
void
)OperationA;
@end
|
-
ConcreteElementA类实现
1
2
3
4
5
6
7
8
9
10
11
|
#import "ConcreteElementA.h"
#import "Visitors.h"
@implementation
ConcreteElementA
-(
void
)OperationA{
return
;
}
-(
void
)Accept:(Visitors *)visitor{
[visitor VisitConcreteElementA:
self
];
}
@end
|
-
ConcreteElementB类接口
1
2
3
4
5
|
#import "Elements.h"
@interface
ConcreteElementB :Elements
-(
void
)OperationB;
@end
|
-
ConcreteElementB类实现
1
2
3
4
5
6
7
8
9
10
11
|
#import "ConcreteElementB.h"
#import "Visitors.h"
@implementation
ConcreteElementB
-(
void
)OperationB{
return
;
}
-(
void
)Accept:(Visitors *)visitor{
[visitor VisitConcreteElementB:
self
];
}
@end
|
-
ObjectStructure类接口
1
2
3
4
5
6
7
8
9
10
11
|
#import <Foundation/Foundation.h>
@class
Elements;
@class
Visitors;
@interface
ObjectStructure :
NSObject
{
NSMutableArray
*elements;
}
-(
void
)Attach:(Elements*)element;
-(
void
)Detach:(Elements*)element;
-(
void
)Accept:(Visitors*)visitor;
@end
|
-
ObjectStructure类实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#import "ObjectStructure.h"
#import "Elements.h"
@implementation
ObjectStructure
-(
id
)init{
if
(
self
== [
super
init]) {
elements = [[
NSMutableArray
alloc]init];
}
return
self
;
}
-(
void
)Attach:(Elements *)element{
[elements addObject:element];
}
-(
void
)Detach:(Elements *)element{
[elements removeObject:element];
}
-(
void
)Accept:(Visitors *)visitor{
for
(Elements *e in elements) {
[e Accept:visitor];
}
}
@end
|
-
Main方法调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#import <Foundation/Foundation.h>
#import "ObjectStructure.h"
#import "ConcreteElementA.h"
#import "ConcreteElementB.h"
#import "ConcreteVisitor1.h"
#import "ConcreteVisitor2.h"
int
main (
int
argc,
const
char
* argv[])
{
@autoreleasepool
{
ObjectStructure *o = [[ObjectStructure alloc]init];
ConcreteElementA *eA = [ConcreteElementA
new
];
ConcreteElementB *eB = [ConcreteElementB
new
];
[o Attach:eA];
[o Attach:eB];
ConcreteVisitor1 *v1 = [ConcreteVisitor1
new
];
ConcreteVisitor2 *v2 = [ConcreteVisitor2
new
];
[o Accept: v1];
[o Accept: v2];
}
return
0;
}
|
“打完”收工!