Patterns / Structural patterns
image

//Target
@protocol IText <NSObject>
//Request()
- (NSString *)getText;
@end

//Adaptee
@interface StringList : NSObject
@property (nonatomic, strongNSMutableArray<NSString *> *rows;

//SpecificRequest()
- (NSString *)getString;
- (void)add:(NSString *)value;
@end

@implementation StringList

- (instancetype)init {
    self = [super init];
    if (self) {
        _rows = [NSMutableArray array];
    }
    return self;
}

- (NSString *)getString {
    return [self.rows componentsJoinedByString:@"\n"];
}

- (void)add:(NSString *)value {
    [self.rows addObject:value];
}
@end

//Adapter
@interface TextAdapter : NSObject <IText>
@property (nonatomic, strongStringList *rowList;
@end

@implementation TextAdapter
//Request()
- (NSString *)getText {
    if (self.rowList == nil) {
        return @"";
    }
    return [self.rowList getString];
}
@end

TextAdapter *getTextAdapter(void) {
    TextAdapter *adapter = [[TextAdapter allocinit];
    StringList *rowList = [[StringList allocinit];
    [rowList add:@"line 1"];
    [rowList add:@"line 2"];
    adapter.rowList = rowList;
    return adapter;
}

//Client
TextAdapter *adapter = getTextAdapter();
NSString *text = [adapter getText];
//text: line 1
//      line 2

NSLog(@"text is \"%@\"", text);


Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.