實(shí)例講解iOS應(yīng)用的設(shè)計(jì)模式開發(fā)中的Visitor訪問(wèn)者模式
為了方便向大家展示,先給出簡(jiǎn)短的定義:
訪問(wèn)者模式(Visitor),表示一個(gè)作用于某對(duì)象結(jié)構(gòu)中的各元素的操作。它使你可以在不改變各元素的類的前提下定義作用于這些元素的新操作。
緊接著,給出其類結(jié)構(gòu)圖。
訪問(wèn)者模式適用于數(shù)據(jù)結(jié)構(gòu)相對(duì)穩(wěn)定的系統(tǒng),它把數(shù)據(jù)結(jié)構(gòu)和作用于結(jié)構(gòu)上的操作之間的耦合解脫開,使得操作結(jié)合可以相對(duì)自由地演化。
訪問(wèn)者模式的目的是要把處理從數(shù)據(jù)結(jié)構(gòu)分離出來(lái)。很多系統(tǒng)可以按照算法和數(shù)據(jù)結(jié)構(gòu)分開,如果這樣的系統(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類:
#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
我們的汽車結(jié)構(gòu)很簡(jiǎn)單,只包含1個(gè)引擎,4個(gè)車輪,并且各個(gè)類也沒(méi)有復(fù)雜的實(shí)現(xiàn),僅僅覆寫了description,讓其輸出簡(jiǎn)要的信息。
好,實(shí)例化一輛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;
}
控制臺(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é)議:
@class NimoEngine, NimoWheel;
@protocol NimoComponentVisitor <NSObject>
- (void) visitEngine:(NimoEngine *) engine;
- (void) visitWheel:(NimoWheel *) wheel;
@end
修改我們的類,使其能夠接受訪問(wèn)
添加訪問(wèn)支持的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
添加訪問(wèn)支持的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
添加訪問(wèn)支持的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
我們?cè)陬愔刑砑恿?(void)acceptComponentVisitor:(id<NimoComponentVisitor>)visitor接口,同意實(shí)現(xiàn)了訪問(wèn)者協(xié)議的visitor訪問(wèn)。(我家大門常打開,啦啦啦啦啦)
讓我們來(lái)看下第一位訪問(wèn)者是誰(shuí)?剛剛上面我們有提到一個(gè)需求,想對(duì)汽車各組件進(jìn)行全面升級(jí)。那么就讓這位專業(yè)的訪問(wèn)者來(lái)做吧!
定義具體訪問(wèn)者:此訪問(wèn)者具備升級(jí)汽車各組件的能力
#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(@"我是升級(jí)人員,正在對(duì)引擎<%@>進(jìn)行升級(jí)", engine);
}
- (void) visitWheel:(NimoWheel *) wheel
{
NSLog(@"我是升級(jí)人員,正在對(duì)車輪<%@>進(jìn)行升級(jí)", wheel);
}
@end
讓我們來(lái)看看這位訪問(wèn)者的工作能力如何
#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;
}
看來(lái)這位訪問(wèn)者的工作很出色。
如果我們還需要對(duì)汽車各組件進(jìn)行維修呢?那就定義一個(gè)專職維修的訪問(wèn)者
#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(@"我是維修人員,正在對(duì)引擎<%@>進(jìn)行維修", engine);
}
- (void) visitWheel:(NimoWheel *) wheel
{
NSLog(@"我是維修人員,正在對(duì)車輪<%@>進(jìn)行維修", wheel);
}
@end
//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)造成破壞。
- Java 的雙重分發(fā)與 Visitor 模式實(shí)例詳解
- C#設(shè)計(jì)模式之Visitor訪問(wèn)者模式解決長(zhǎng)隆歡樂(lè)世界問(wèn)題實(shí)例
- 學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)訪問(wèn)者模式(Visitor)
- Java設(shè)計(jì)模式之訪問(wèn)模式(Visitor者模式)介紹
- php設(shè)計(jì)模式 Visitor 訪問(wèn)者模式
- C++的命名空間詳解
- C++基于OpenCV實(shí)現(xiàn)手勢(shì)識(shí)別的源碼
- C++內(nèi)存模型和名稱空間詳解
- 淺談 C++17 里的 Visitor 模式
相關(guān)文章
iOS中從網(wǎng)絡(luò)獲取數(shù)據(jù)的幾種方法的比較
IOS中獲取網(wǎng)絡(luò)數(shù)據(jù)一般有三種:1、NSURLCondition(已過(guò)時(shí)) 2、NSURLSession 3、三方庫(kù)AFNetWorking。下面通過(guò)本文給大家比較這三種方法的區(qū)別對(duì)比2017-11-11iOS實(shí)現(xiàn)UIScrollView的無(wú)限輪播功能(原理)詳解
在現(xiàn)在的一些App中常常見到圖片輪播器,一般用于展示廣告、新聞等數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于iOS實(shí)現(xiàn)UIScrollView的無(wú)限輪播功能(原理)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-09-09iOS App連續(xù)閃退時(shí)上報(bào)crash日志的方法詳解
iOS App 有時(shí)可能遇到啟動(dòng)必 crash 的絕境:每次打開 App 都閃退,無(wú)法正常使用App。下面這篇文章主要給大家介紹了iOS App連續(xù)閃退時(shí)上報(bào)crash日志的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2018-04-04iOS應(yīng)用開發(fā)中對(duì)UIImage進(jìn)行截取和縮放的方法詳解
這篇文章主要介紹了iOS應(yīng)用開發(fā)中對(duì)UIImage進(jìn)行截取和縮放的方法,分別講解了如何截取指定區(qū)域大小的UIImage以及縮放到指定大小和等比縮放的具體操作過(guò)程,需要的朋友可以參考下2016-04-04iOS掃描二維碼實(shí)現(xiàn)手勢(shì)拉近拉遠(yuǎn)鏡頭
這篇文章主要為大家詳細(xì)介紹了iOS掃描二維碼實(shí)現(xiàn)手勢(shì)拉近拉遠(yuǎn)鏡頭,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04iOS如何優(yōu)雅地實(shí)現(xiàn)序列動(dòng)畫詳解
這篇文章主要給大家介紹了關(guān)于iOS如何優(yōu)雅地實(shí)現(xiàn)序列動(dòng)畫的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12iOS開發(fā)技能weak和strong修飾符的規(guī)范使用詳解
這篇文章主要為大家介紹了iOS開發(fā)技能weak和strong修飾符的規(guī)范使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07