最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2016年03月22日 09:37:10   作者:TonyGo  
這篇文章主要介紹了iOS應(yīng)用的設(shè)計(jì)模式開(kāi)發(fā)中的Visitor訪問(wèn)者模式的實(shí)例,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下

為了方便向大家展示,先給出簡(jiǎn)短的定義:

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

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

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

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

訪問(wèn)者模式的優(yōu)點(diǎn)就是增加新的操作很容易,因?yàn)樵黾有碌牟僮骶鸵馕吨黾右粋€(gè)新的訪問(wèn)者。訪問(wèn)者模式將有關(guān)的行為集中到一個(gè)訪問(wèn)者對(duì)象中。

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

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

定義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)很簡(jiǎn)單,只包含1個(gè)引擎,4個(gè)車輪,并且各個(gè)類也沒(méi)有復(fù)雜的實(shí)現(xiàn),僅僅覆寫了description,讓其輸出簡(jiǎn)要的信息。

好,實(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)

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

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

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

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

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

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

修改我們的類,使其能夠接受訪問(wèn)

添加訪問(wèn)支持的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

添加訪問(wèn)支持的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

添加訪問(wèn)支持的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

我們?cè)陬愔刑砑恿?(void)acceptComponentVisitor:(id<NimoComponentVisitor>)visitor接口,同意實(shí)現(xiàn)了訪問(wèn)者協(xié)議的visitor訪問(wèn)。(我家大門常打開(kāi),啦啦啦啦啦)

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

定義具體訪問(wèn)者:此訪問(wèn)者具備升級(jí)汽車各組件的能力

復(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í)人員,正在對(duì)引擎<%@>進(jìn)行升級(jí)", engine);
}
 
- (void) visitWheel:(NimoWheel *) wheel
{
    NSLog(@"我是升級(jí)人員,正在對(duì)車輪<%@>進(jìn)行升級(jí)", wheel);
}
 
@end

讓我們來(lái)看看這位訪問(wèn)者的工作能力如何
復(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);
        
        //對(duì)組建進(jìn)行“升級(jí)”
        NimoComponentUpgrade *upgradeVisitor = [[NimoComponentUpgrade alloc] init];
        [car acceptComponentVisitor:upgradeVisitor];
    }
    return 0;
}

201632293606776.jpg (768×243)

看來(lái)這位訪問(wèn)者的工作很出色。

如果我們還需要對(duì)汽車各組件進(jìn)行維修呢?那就定義一個(gè)專職維修的訪問(wè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(@"我是維修人員,正在對(duì)引擎<%@>進(jìn)行維修", engine);
}
 
- (void) visitWheel:(NimoWheel *) wheel
{
    NSLog(@"我是維修人員,正在對(duì)車輪<%@>進(jìn)行維修", wheel);
}
 
@end

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

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

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

相關(guān)文章

最新評(píng)論

全南县| 旅游| 伊宁县| 万州区| 来安县| 金门县| 鄢陵县| 武穴市| 郧西县| 雅安市| 榕江县| 科尔| 南投县| 康平县| 鄂托克旗| 嵩明县| 特克斯县| 横山县| 三江| 高阳县| 喜德县| 高雄县| 扶绥县| 仁寿县| 神农架林区| 乌什县| 长汀县| 五家渠市| 体育| 璧山县| 正阳县| 两当县| 兰州市| 兴业县| 苏尼特右旗| 盈江县| 靖安县| 拉萨市| 岢岚县| 南汇区| 上思县|