程序員在現實生活中,一些集合對象中有許多不同的元素,每個元素也有許多不同的訪問者和處理方法,別著急,今天武林技術頻道為大家實例講解iOS應用的設計模式開發中的Visitor訪問者模式。
為了方便向大家展示,先給出簡短的定義:
訪問者模式(Visitor),表示一個作用于某對象結構中的各元素的操作。它使你可以在不改變各元素的類的前提下定義作用于這些元素的新操作。
緊接著,給出其類結構圖。

訪問者模式適用于數據結構相對穩定的系統,它把數據結構和作用于結構上的操作之間的耦合解脫開,使得操作結合可以相對自由地演化。
訪問者模式的目的是要把處理從數據結構分離出來。很多系統可以按照算法和數據結構分開,如果這樣的系統有比較穩定的數據結構,又有易于變化的算法的話,使用訪問者模式就是比較合適的,因為訪問者模式使得算法操作的增加變得容易。
訪問者模式的優點就是增加新的操作很容易,因為增加新的操作就意味著增加一個新的訪問者。訪問者模式將有關的行為集中到一個訪問者對象中。
那其實,訪問者模式的缺點也就是使增加新的數據結構變得苦難了。所以,GoF四人中的一個作者增經說過,‘大多時候你并不需要訪問者模式,但當一旦你需要訪問者模式的時候,那就是真的需要它了'。
那么下面還是老慣例,給大家展示一下簡單的實現。
一個簡單的Car模型,含有1臺Engine、4個Wheel,使用訪問者模式添加對Car的升級與維修操作。
定義Engine類:
?
#import <Foundation/Foundation.h>
#import "NimoComponentVisitor.h"
?
@interface NimoEngine : NSObject
?
@property (nonatomic, copy) NSString *modelName;
- (id)initWithModelName:(NSString *)modelName;
?
@end
?
?
?
#import "NimoEngine.h"
?
@implementation NimoEngine
?
- (id)initWithModelName:(NSString *)modelName
{
??? self = [super init];
??? if (self) {
??????? _modelName = [modelName copy];
??? }
??? return self;
}
?
- (id) init
{
??? return [self initWithModelName:@"Slant 6"];
}
?
- (NSString *)description
{
??? return [NSString stringWithFormat:@"Engine: %@", _modelName];
}
?
@end
定義Wheel類:
?
?
?
#import <Foundation/Foundation.h>
?
@interface NimoWheel : NSObject
?
@property (nonatomic, assign) float diameter; //車輪直徑
?
@end
?
?
?
#import "NimoWheel.h"
?
@implementation NimoWheel
?
- (id)init
{
??? self = [super init];
??? if (self) {
??????? _diameter = 400.0f;
??? }
??? return self;
}
?
-(NSString *)description
{
??? return [NSString stringWithFormat:@"Wheel: %f mm", _diameter];
}
?
@end
定義Car類:
?
?
?
#import <Foundation/Foundation.h>
?
@class NimoEngine, NimoWheel;
?
@interface NimoCar : NSObject
?
@property (nonatomic) NimoEngine *engine;
@property (nonatomic, readonly) NSArray *arrayOfWheels;
?
- (void)addWheel:(NimoWheel *)wheel atIndex:(NSUInteger) index;
?
@end
?
?
?
@interface NimoCar()
?
@property (nonatomic, readwrite) NSMutableArray *mutableArrayOfWheels;
?
@end
?
?
?
@implementation NimoCar
?
- (id)init
{
??? if (self = [super init]) {
??????? _mutableArrayOfWheels = [[NSMutableArray alloc] initWithCapacity:4];
??? }
????
??? return self;
}
?
- (void)addWheel:(NimoWheel *)wheel atIndex:(NSUInteger) index
{
??? [_mutableArrayOfWheels insertObject:wheel atIndex:index];
}
?
- (NSArray *)arrayOfWheels
{
??? return [_mutableArrayOfWheels copy];
}
?
- (NSString *)description
{
??? return [NSString stringWithFormat:@"My car: %@", [NSDictionary dictionaryWithObjects:@[_engine, self.arrayOfWheels] forKeys:@[@"Engine", @"Wheels"]]];
}
?
@end
我們的汽車結構很簡單,只包含1個引擎,4個車輪,并且各個類也沒有復雜的實現,僅僅覆寫了description,讓其輸出簡要的信息。
?
好,實例化一輛Car, 看看效果:
?
#import <Foundation/Foundation.h>
#import "NimoCar.h"
#import "NimoEngine.h"
#import "NimoWheel.h"
?
int main(int argc, const char * argv[]) {
??? @autoreleasepool {
????????
??????? NimoCar *car = [[NimoCar alloc] init];
??????? NimoEngine *engine = [[NimoEngine alloc] initWithBrandName:@"V8"];
??????? NimoWheel *wheelA = [[NimoWheel alloc] init];
??????? NimoWheel *wheelB = [[NimoWheel alloc] init];
??????? NimoWheel *wheelC = [[NimoWheel alloc] init];
??????? NimoWheel *wheelD = [[NimoWheel alloc] init];
????????
??????? car.engine = engine;
??????? [car addWheel:wheelA atIndex:0];
??????? [car addWheel:wheelB atIndex:1];
??????? [car addWheel:wheelC atIndex:2];
??????? [car addWheel:wheelD atIndex:3];
????????
??????? NSLog(@"%@", car);
????????
??? }
??? return 0;
}
?
?
?

控制臺跟意料中一樣輸出了Car的信息。至此,準備工作做好了。
訪問者模式:表示一個作用于某對象結構中的各元素的操作。它讓我們可以在不改變各元素的類的前提下定義作用于這些元素的新操作。---《設計模式》(Addison-Wesley, 1994)
這段話比較拗口,不太好理解。拿剛剛完成的Car類來舉例,NimoCar類就是對象結構,其中包含的元素為:NimoEngine類和NimoWheel類。如果現在需要對Car進行全面升級(新操作),通常的做法是NimoEngine與NimoWheel都向外提供“升級”的接口。如果還需要對Car進行維修呢?那又得向外提供“維修”的接口。隨著類似的需求越多,NimoEngine與NimoWheel向外提供的接口就越多,類也變得越復雜。有沒有簡單的方法呢?有!把這些瑣事都交給訪問者來做吧,NimoEngine與NimoWheel只要同意被訪問就成。
定義訪問者協議:
?
@class NimoEngine, NimoWheel;
?
@protocol NimoComponentVisitor <NSObject>
?
- (void) visitEngine:(NimoEngine *) engine;
- (void) visitWheel:(NimoWheel *) wheel;
?
@end
修改我們的類,使其能夠接受訪問
?
添加訪問支持的Engine類:
?
#import <Foundation/Foundation.h>
#import "NimoComponentVisitor.h"
?
@interface NimoEngine : NSObject
?
@property (nonatomic, copy) NSString *modelName;
?
- (id)initWithModelName:(NSString *)modelName;
?
- (void)acceptComponentVisitor:(id<NimoComponentVisitor>) visitor;
?
@end
?
?
?
#import "NimoEngine.h"
?
@implementation NimoEngine
?
- (id)initWithModelName:(NSString *)modelName
{
??? self = [super init];
??? if (self) {
??????? _modelName = [modelName copy];
??? }
??? return self;
}
?
- (id) init
{
??? return [self initWithModelName:@"Slant 6"];
}
?
- (void)acceptComponentVisitor:(id<NimoComponentVisitor>) visitor
{
??? [visitor visitEngine:self];
}
?
- (NSString *)description
{
??? return [NSString stringWithFormat:@"Engine: %@", _modelName];
}
?
@end
添加訪問支持的Wheel類:
?
?
?
#import <Foundation/Foundation.h>
#import "NimoComponentVisitor.h"
?
@interface NimoWheel : NSObject
?
@property (nonatomic, assign) float diameter; //直徑
?
- (void)acceptComponentVisitor:(id<NimoComponentVisitor>) visitor;
?
@end
?
?
?
#import "NimoWheel.h"
?
@implementation NimoWheel
?
- (id)init
{
??? self = [super init];
??? if (self) {
??????? _diameter = 400.0f;
??? }
??? return self;
}
?
- (void)acceptComponentVisitor:(id<NimoComponentVisitor>) visitor
{
??? [visitor visitWheel:self];
}
?
-(NSString *)description
{
??? return [NSString stringWithFormat:@"Wheel: %f mm", _diameter];
}
?
@end
添加訪問支持的Car類
?
?
?
#import <Foundation/Foundation.h>
#import "NimoComponentVisitor.h"
?
@class NimoEngine, NimoWheel;
?
@interface NimoCar : NSObject
?
@property (nonatomic) NimoEngine *engine;
@property (nonatomic, readonly) NSArray *arrayOfWheels;
?
- (void)addWheel:(NimoWheel *)wheel atIndex:(NSUInteger) index;
?
- (void)acceptComponentVisitor:(id<NimoComponentVisitor>) visitor;
@end
?
?
?
#import "NimoCar.h"
#import "NimoEngine.h"
#import "NimoWheel.h"
?
@interface NimoCar()
?
@property (nonatomic, readwrite) NSMutableArray *mutableArrayOfWheels;
?
@end
?
?
?
@implementation NimoCar
?
- (id)init
{
??? if (self = [super init]) {
??????? _mutableArrayOfWheels = [[NSMutableArray alloc] initWithCapacity:4];
??? }
????
??? return self;
}
?
- (void)addWheel:(NimoWheel *)wheel atIndex:(NSUInteger) index
{
??? [_mutableArrayOfWheels insertObject:wheel atIndex:index];
}
?
- (NSArray *)arrayOfWheels
{
??? return [_mutableArrayOfWheels copy];
}
?
- (void)acceptComponentVisitor:(id<NimoComponentVisitor>) visitor
{
??? [_engine acceptComponentVisitor:visitor];
??? for (NimoWheel *wheel in self.arrayOfWheels) {
??????? [wheel acceptComponentVisitor:visitor];
??? }
}
?
- (NSString *)description
{
??? return [NSString stringWithFormat:@"My car: %@", [NSDictionary dictionaryWithObjects:@[_engine, self.arrayOfWheels] forKeys:@[@"Engine", @"Wheels"]]];
}
?
@end
我們在類中添加了-(void)acceptComponentVisitor:(id<NimoComponentVisitor>)visitor接口,同意實現了訪問者協議的visitor訪問。(我家大門常打開,啦啦啦啦啦)
?
讓我們來看下第一位訪問者是誰?剛剛上面我們有提到一個需求,想對汽車各組件進行全面升級。那么就讓這位專業的訪問者來做吧!
定義具體訪問者:此訪問者具備升級汽車各組件的能力
?
#import <Foundation/Foundation.h>
#import "NimoComponentVisitor.h"
?
@interface NimoComponentUpgrade : NSObject <NimoComponentVisitor>
?
- (void) visitEngine:(NimoEngine *) engine;
- (void) visitWheel:(NimoWheel *) wheel;
?
@end
?
?
?
#import "NimoComponentUpgrade.h"
?
@implementation NimoComponentUpgrade
?
- (void) visitEngine:(NimoEngine *) engine
{
??? NSLog(@"我是升級人員,正在對引擎<%@>進行升級", engine);
}
?
- (void) visitWheel:(NimoWheel *) wheel
{
??? NSLog(@"我是升級人員,正在對車輪<%@>進行升級", wheel);
}
?
@end
讓我們來看看這位訪問者的工作能力如何
?
?
?
#import <Foundation/Foundation.h>
#import "NimoCar.h"
#import "NimoEngine.h"
#import "NimoWheel.h"
#import "NimoComponentMaintenance.h"
#import "NimoComponentUpgrade.h"
?
int main(int argc, const char * argv[]) {
??? @autoreleasepool {
????????
??????? NimoCar *car = [[NimoCar alloc] init];
??????? NimoEngine *engine = [[NimoEngine alloc] initWithModelName:@"V8"];
??????? NimoWheel *wheelA = [[NimoWheel alloc] init];
??????? NimoWheel *wheelB = [[NimoWheel alloc] init];
??????? NimoWheel *wheelC = [[NimoWheel alloc] init];
??????? NimoWheel *wheelD = [[NimoWheel alloc] init];
????????
??????? car.engine = engine;
??????? [car addWheel:wheelA atIndex:0];
??????? [car addWheel:wheelB atIndex:1];
??????? [car addWheel:wheelC atIndex:2];
??????? [car addWheel:wheelD atIndex:3];
????????
??????? NSLog(@"%@", car);
????????
??????? //對組建進行“升級”
??????? NimoComponentUpgrade *upgradeVisitor = [[NimoComponentUpgrade alloc] init];
??????? [car acceptComponentVisitor:upgradeVisitor];
??? }
??? return 0;
}
?
?
?

看來這位訪問者的工作很出色。
如果我們還需要對汽車各組件進行維修呢?那就定義一個專職維修的訪問者
?
#import <Foundation/Foundation.h>
#import "NimoComponentVisitor.h"
?
@interface NimoComponentMaintenance : NSObject <NimoComponentVisitor>
?
- (void) visitEngine:(NimoEngine *) engine;
- (void) visitWheel:(NimoWheel *) wheel;
?
@end
?
?
?
#import "NimoComponentMaintenance.h"
?
@implementation NimoComponentMaintenance
?
- (void) visitEngine:(NimoEngine *) engine
{
??? NSLog(@"我是維修人員,正在對引擎<%@>進行維修", engine);
}
?
- (void) visitWheel:(NimoWheel *) wheel
{
??? NSLog(@"我是維修人員,正在對車輪<%@>進行維修", wheel);
}
?
@end
?
?
?
//main.m
??????? ...
??????? //對組建進行“維修”
??????? NimoComponentMaintenance *maintenanceVisitor = [[NimoComponentMaintenance alloc] init];
??????? [car acceptComponentVisitor:maintenanceVisitor];
??????? ...
???????
使用訪問者模式后,添加操作,只需實現具體的訪問者,不會對類的結構造成破壞。
實例講解iOS應用的設計模式開發中的Visitor訪問者模式,想必大家現在心中都會有了答案,武林技術頻道這個網址小編大力推薦,里面有著不少的資源可供你利用哦。
?