為了便利向年夜家展現,先給出冗長的界說:
拜訪者形式(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拜訪者形式】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!