国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

實(shí)例講解iOS應(yīng)用的設(shè)計模式開發(fā)中的Visitor訪問者模式

2020-07-26 03:25:20
字體:
供稿:網(wǎng)友

為了方便向大家展示,先給出簡短的定義:

訪問者模式(Visitor),表示一個作用于某對象結(jié)構(gòu)中的各元素的操作。它使你可以在不改變各元素的類的前提下定義作用于這些元素的新操作。

緊接著,給出其類結(jié)構(gòu)圖。

201632293447218.jpg (500×385)
訪問者模式適用于數(shù)據(jù)結(jié)構(gòu)相對穩(wěn)定的系統(tǒng),它把數(shù)據(jù)結(jié)構(gòu)和作用于結(jié)構(gòu)上的操作之間的耦合解脫開,使得操作結(jié)合可以相對自由地演化。

訪問者模式的目的是要把處理從數(shù)據(jù)結(jié)構(gòu)分離出來。很多系統(tǒng)可以按照算法和數(shù)據(jù)結(jié)構(gòu)分開,如果這樣的系統(tǒng)有比較穩(wěn)定的數(shù)據(jù)結(jié)構(gòu),又有易于變化的算法的話,使用訪問者模式就是比較合適的,因為訪問者模式使得算法操作的增加變得容易。

訪問者模式的優(yōu)點(diǎn)就是增加新的操作很容易,因為增加新的操作就意味著增加一個新的訪問者。訪問者模式將有關(guān)的行為集中到一個訪問者對象中。

那其實(shí),訪問者模式的缺點(diǎn)也就是使增加新的數(shù)據(jù)結(jié)構(gòu)變得苦難了。所以,GoF四人中的一個作者增經(jīng)說過,‘大多時候你并不需要訪問者模式,但當(dāng)一旦你需要訪問者模式的時候,那就是真的需要它了'。

那么下面還是老慣例,給大家展示一下簡單的實(shí)現(xiàn)。
一個簡單的Car模型,含有1臺Engine、4個Wheel,使用訪問者模式添加對Car的升級與維修操作。

定義Engine類:

復(fù)制代碼 代碼如下:

#import <Foundation/Foundation.h>
#import "NimoComponentVisitor.h"
 
@interface NimoEngine : NSObject
 
@property (nonatomic, copy) NSString *modelName;
- (id)initWithModelName:(NSString *)modelName;
 
@end

復(fù)制代碼 代碼如下:

#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類:
復(fù)制代碼 代碼如下:

#import <Foundation/Foundation.h>
 
@interface NimoWheel : NSObject
 
@property (nonatomic, assign) float diameter; //車輪直徑
 
@end

復(fù)制代碼 代碼如下:

#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類:
復(fù)制代碼 代碼如下:

#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

復(fù)制代碼 代碼如下:

@interface NimoCar()
 
@property (nonatomic, readwrite) NSMutableArray *mutableArrayOfWheels;
 
@end

復(fù)制代碼 代碼如下:

@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

我們的汽車結(jié)構(gòu)很簡單,只包含1個引擎,4個車輪,并且各個類也沒有復(fù)雜的實(shí)現(xiàn),僅僅覆寫了description,讓其輸出簡要的信息。

好,實(shí)例化一輛Car, 看看效果:

復(fù)制代碼 代碼如下:

#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;
}

201632293536226.png (768×152)

控制臺跟意料中一樣輸出了Car的信息。至此,準(zhǔn)備工作做好了。

訪問者模式:表示一個作用于某對象結(jié)構(gòu)中的各元素的操作。它讓我們可以在不改變各元素的類的前提下定義作用于這些元素的新操作。---《設(shè)計模式》(Addison-Wesley, 1994)

這段話比較拗口,不太好理解。拿剛剛完成的Car類來舉例,NimoCar類就是對象結(jié)構(gòu),其中包含的元素為:NimoEngine類和NimoWheel類。如果現(xiàn)在需要對Car進(jìn)行全面升級(新操作),通常的做法是NimoEngine與NimoWheel都向外提供“升級”的接口。如果還需要對Car進(jìn)行維修呢?那又得向外提供“維修”的接口。隨著類似的需求越多,NimoEngine與NimoWheel向外提供的接口就越多,類也變得越復(fù)雜。有沒有簡單的方法呢?有!把這些瑣事都交給訪問者來做吧,NimoEngine與NimoWheel只要同意被訪問就成。

定義訪問者協(xié)議:

復(fù)制代碼 代碼如下:

@class NimoEngine, NimoWheel;
 
@protocol NimoComponentVisitor <NSObject>
 
- (void) visitEngine:(NimoEngine *) engine;
- (void) visitWheel:(NimoWheel *) wheel;
 
@end

修改我們的類,使其能夠接受訪問

添加訪問支持的Engine類:

復(fù)制代碼 代碼如下:

#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

復(fù)制代碼 代碼如下:

#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類:
復(fù)制代碼 代碼如下:

#import <Foundation/Foundation.h>
#import "NimoComponentVisitor.h"
 
@interface NimoWheel : NSObject
 
@property (nonatomic, assign) float diameter; //直徑
 
- (void)acceptComponentVisitor:(id<NimoComponentVisitor>) visitor;
 
@end

復(fù)制代碼 代碼如下:

#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類
復(fù)制代碼 代碼如下:

#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

復(fù)制代碼 代碼如下:

#import "NimoCar.h"
#import "NimoEngine.h"
#import "NimoWheel.h"
 
@interface NimoCar()
 
@property (nonatomic, readwrite) NSMutableArray *mutableArrayOfWheels;
 
@end

復(fù)制代碼 代碼如下:

@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接口,同意實(shí)現(xiàn)了訪問者協(xié)議的visitor訪問。(我家大門常打開,啦啦啦啦啦)

讓我們來看下第一位訪問者是誰?剛剛上面我們有提到一個需求,想對汽車各組件進(jìn)行全面升級。那么就讓這位專業(yè)的訪問者來做吧!

定義具體訪問者:此訪問者具備升級汽車各組件的能力

復(fù)制代碼 代碼如下:

#import <Foundation/Foundation.h>
#import "NimoComponentVisitor.h"
 
@interface NimoComponentUpgrade : NSObject <NimoComponentVisitor>
 
- (void) visitEngine:(NimoEngine *) engine;
- (void) visitWheel:(NimoWheel *) wheel;
 
@end

復(fù)制代碼 代碼如下:

#import "NimoComponentUpgrade.h"
 
@implementation NimoComponentUpgrade
 
- (void) visitEngine:(NimoEngine *) engine
{
    NSLog(@"我是升級人員,正在對引擎<%@>進(jìn)行升級", engine);
}
 
- (void) visitWheel:(NimoWheel *) wheel
{
    NSLog(@"我是升級人員,正在對車輪<%@>進(jìn)行升級", wheel);
}
 
@end

讓我們來看看這位訪問者的工作能力如何
復(fù)制代碼 代碼如下:

#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);
        
        //對組建進(jìn)行“升級”
        NimoComponentUpgrade *upgradeVisitor = [[NimoComponentUpgrade alloc] init];
        [car acceptComponentVisitor:upgradeVisitor];
    }
    return 0;
}

201632293606776.jpg (768×243)

看來這位訪問者的工作很出色。

如果我們還需要對汽車各組件進(jìn)行維修呢?那就定義一個專職維修的訪問者

復(fù)制代碼 代碼如下:

#import <Foundation/Foundation.h>
#import "NimoComponentVisitor.h"
 
@interface NimoComponentMaintenance : NSObject <NimoComponentVisitor>
 
- (void) visitEngine:(NimoEngine *) engine;
- (void) visitWheel:(NimoWheel *) wheel;
 
@end

復(fù)制代碼 代碼如下:

#import "NimoComponentMaintenance.h"
 
@implementation NimoComponentMaintenance
 
- (void) visitEngine:(NimoEngine *) engine
{
    NSLog(@"我是維修人員,正在對引擎<%@>進(jìn)行維修", engine);
}
 
- (void) visitWheel:(NimoWheel *) wheel
{
    NSLog(@"我是維修人員,正在對車輪<%@>進(jìn)行維修", wheel);
}
 
@end

復(fù)制代碼 代碼如下:

//main.m
        ...
        //對組建進(jìn)行“維修”
        NimoComponentMaintenance *maintenanceVisitor = [[NimoComponentMaintenance alloc] init];
        [car acceptComponentVisitor:maintenanceVisitor];
        ...
       

使用訪問者模式后,添加操作,只需實(shí)現(xiàn)具體的訪問者,不會對類的結(jié)構(gòu)造成破壞。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 乃东县| 嘉义市| 游戏| 普宁市| 金川县| 南京市| 汕尾市| 普定县| 沙坪坝区| 德惠市| 红桥区| 宜宾县| 怀远县| 门头沟区| 平阳县| 凉城县| 汕尾市| 密云县| 荥阳市| 兴海县| 丘北县| 昌都县| 罗田县| 漠河县| 铜川市| 电白县| 澄城县| 安丘市| 资溪县| 井冈山市| 张家川| 札达县| 平南县| 河东区| 丹阳市| 天门市| 鄂托克旗| 县级市| 沙坪坝区| 辽阳县| 胶州市|