亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

訪問者模式(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)點就是增加新的操作很容易,因為增加新的操作就意味著增加一個新的訪問者。訪問者模式將有關(guān)的行為集中到一個訪問者對象中。

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

那么下面還是老慣例,給大家展示一下簡單的實現(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ù)雜的實現(xiàn),僅僅覆寫了description,讓其輸出簡要的信息。

好,實例化一輛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的信息。至此,準備工作做好了。

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

這段話比較拗口,不太好理解。拿剛剛完成的Car類來舉例,NimoCar類就是對象結(jié)構(gòu),其中包含的元素為:NimoEngine類和NimoWheel類。如果現(xiàn)在需要對Car進行全面升級(新操作),通常的做法是NimoEngine與NimoWheel都向外提供“升級”的接口。如果還需要對Car進行維修呢?那又得向外提供“維修”的接口。隨著類似的需求越多,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接口,同意實現(xiàn)了訪問者協(xié)議的visitor訪問。(我家大門常打開,啦啦啦啦啦)

讓我們來看下第一位訪問者是誰?剛剛上面我們有提到一個需求,想對汽車各組件進行全面升級。那么就讓這位專業(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(@"我是升級人員,正在對引擎<%@>進行升級", engine);
}
 
- (void) visitWheel:(NimoWheel *) wheel
{
    NSLog(@"我是升級人員,正在對車輪<%@>進行升級", 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);
        
        //對組建進行“升級”
        NimoComponentUpgrade *upgradeVisitor = [[NimoComponentUpgrade alloc] init];
        [car acceptComponentVisitor:upgradeVisitor];
    }
    return 0;
}

201632293606776.jpg (768×243)

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

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

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

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

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

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

相關(guān)文章

  • iOS中從網(wǎng)絡(luò)獲取數(shù)據(jù)的幾種方法的比較

    iOS中從網(wǎng)絡(luò)獲取數(shù)據(jù)的幾種方法的比較

    IOS中獲取網(wǎng)絡(luò)數(shù)據(jù)一般有三種:1、NSURLCondition(已過時) 2、NSURLSession 3、三方庫AFNetWorking。下面通過本文給大家比較這三種方法的區(qū)別對比
    2017-11-11
  • iOS實現(xiàn)聯(lián)系人列表功能

    iOS實現(xiàn)聯(lián)系人列表功能

    這篇文章主要為大家詳細介紹了iOS實現(xiàn)聯(lián)系人列表功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • iOS中的通知機制

    iOS中的通知機制

    網(wǎng)上經(jīng)常說iOS的通知機制是使用了觀察者模式,里面有兩個角色,其一是poster(發(fā)送者),另一個是observer(接受信息的訂閱者)。接下來通過本文給大家介紹iOS中的通知機制,感興趣的朋友一起學習吧
    2016-04-04
  • iOS實現(xiàn)UIScrollView的無限輪播功能(原理)詳解

    iOS實現(xiàn)UIScrollView的無限輪播功能(原理)詳解

    在現(xiàn)在的一些App中常常見到圖片輪播器,一般用于展示廣告、新聞等數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于iOS實現(xiàn)UIScrollView的無限輪播功能(原理)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2018-09-09
  • IOS 開發(fā)之Swift 元組的實例詳解

    IOS 開發(fā)之Swift 元組的實例詳解

    這篇文章主要介紹了IOS 開發(fā)之Swift 元組的實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • iOS App連續(xù)閃退時上報crash日志的方法詳解

    iOS App連續(xù)閃退時上報crash日志的方法詳解

    iOS App 有時可能遇到啟動必 crash 的絕境:每次打開 App 都閃退,無法正常使用App。下面這篇文章主要給大家介紹了iOS App連續(xù)閃退時上報crash日志的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。
    2018-04-04
  • iOS應(yīng)用開發(fā)中對UIImage進行截取和縮放的方法詳解

    iOS應(yīng)用開發(fā)中對UIImage進行截取和縮放的方法詳解

    這篇文章主要介紹了iOS應(yīng)用開發(fā)中對UIImage進行截取和縮放的方法,分別講解了如何截取指定區(qū)域大小的UIImage以及縮放到指定大小和等比縮放的具體操作過程,需要的朋友可以參考下
    2016-04-04
  • iOS掃描二維碼實現(xiàn)手勢拉近拉遠鏡頭

    iOS掃描二維碼實現(xiàn)手勢拉近拉遠鏡頭

    這篇文章主要為大家詳細介紹了iOS掃描二維碼實現(xiàn)手勢拉近拉遠鏡頭,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • iOS如何優(yōu)雅地實現(xiàn)序列動畫詳解

    iOS如何優(yōu)雅地實現(xiàn)序列動畫詳解

    這篇文章主要給大家介紹了關(guān)于iOS如何優(yōu)雅地實現(xiàn)序列動畫的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-12-12
  • iOS開發(fā)技能weak和strong修飾符的規(guī)范使用詳解

    iOS開發(fā)技能weak和strong修飾符的規(guī)范使用詳解

    這篇文章主要為大家介紹了iOS開發(fā)技能weak和strong修飾符的規(guī)范使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07

最新評論