Cấu trúc lớp: Ví dụ mình có sơ đồ lớp như sau:
- Adapter theo quan hệ Navigability hoặc Aggregation hay Composition như nhau:
- Adapter theo quan hệ kế thừa Generalization hay Realization như sau:
Cách sử dụng: Chúng ta thường dùng Adapter trong việc bảo trì ứng dụng. Chúng ta không thể viết code vào những đối tượng có sẵn trước đó nhưng vẫn phải đảm bảo hệ thống chạy tương thích với những lớp đó hay có thể sửa mới thì chúng ta ứng dụng Adapter vào.
Code ví dụ trong Objective C:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Interface | |
@protocol Writer <NSObject> | |
- (BOOL)open; | |
- (NSString*)write:(NSString*)content; | |
- (BOOL)close; | |
@end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "Writer.h" | |
#import "OldHtmlWriter.h" | |
@interface HtmlWriterClassAdapter : OldHtmlWriter <Writer> | |
@end | |
@implementation HtmlWriterClassAdapter | |
- (instancetype)initWithFileName:(NSString*)name { | |
self = [self init]; | |
if (self) { | |
fileName = name; | |
} | |
return self; | |
} | |
- (BOOL)open { | |
[super openFile]; | |
NSLog(@"After open html file by class adapter:%@",fileName); | |
return YES; | |
} | |
- (NSString*)write:(NSString*)content { | |
[super writeFile:[super conversionString:content]]; | |
return [NSString stringWithFormat:@"ClassAdapter %@",content]; | |
} | |
- (BOOL)close { | |
[super closeFile]; | |
NSLog(@"After close html file by class adapter:%@",fileName); | |
return YES; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "Writer.h" | |
@interface HtmlWriterObjectAdapter : NSObject <Writer> { | |
NSString* fileName; | |
} | |
- (instancetype)initWithFileName:(NSString*)name; | |
@end | |
@interface HtmlWriterObjectAdapter () { | |
OldHtmlWriter* oldWriter; | |
} | |
@end | |
@implementation HtmlWriterObjectAdapter | |
- (instancetype)initWithFileName:(NSString*)name { | |
self = [self init]; | |
if (self) { | |
fileName = name; | |
oldWriter = [[OldHtmlWriter alloc] initWithFileName:fileName]; | |
} | |
return self; | |
} | |
- (BOOL)open { | |
[oldWriter openFile]; | |
NSLog(@"After open html file by object adapter:%@",fileName); | |
return YES; | |
} | |
- (NSString*)write:(NSString*)content { | |
[oldWriter writeFile:[oldWriter conversionString:content]]; | |
return [NSString stringWithFormat:@"ObjectAdapter %@",content]; | |
} | |
- (BOOL)close { | |
[oldWriter closeFile]; | |
NSLog(@"After close html file by object adapter:%@",fileName); | |
return YES; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface OldHtmlWriter : NSObject { | |
NSString* fileName; | |
} | |
- (instancetype)initWithFileName:(NSString*)name; | |
- (void)openFile; | |
- (NSString*)conversionString:(NSString*)string; | |
- (void)writeFile:(NSString*)content; | |
- (void)closeFile; | |
@end | |
@implementation OldHtmlWriter | |
- (instancetype)initWithFileName:(NSString*)name { | |
self = [self init]; | |
if (self) { | |
fileName = name; | |
} | |
return self; | |
} | |
- (void)openFile { | |
NSLog(@"Open file name:%@",fileName); | |
} | |
- (NSString*)conversionString:(NSString*)string { | |
NSLog(@"Conversion string"); | |
return string; | |
} | |
- (void)writeFile:(NSString*)content { | |
NSLog(@"Write file name:%@",fileName); | |
} | |
- (void)closeFile { | |
NSLog(@"Close file name:%@",fileName); | |
} | |
@end |
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.