美文网首页
iOS 适配器模式

iOS 适配器模式

作者: 印林泉 | 来源:发表于2017-03-03 13:51 被阅读121次
  • 适配器模式
    创建适配协议,创建抽象适配器类,创建类适配器/对象适配器。

  • 应用,适用场景
    电源适配器,普通充电器(类适配器),苹果设备充电器(对象适配器)。输入电压,输出电压。

适配器协议

//
//  AdapterProtocol.h
//  LearnAdapter
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@protocol AdapterProtocol <NSObject>

- (NSString *)name;
- (UIColor *)lineColor;
- (NSString *)phoneNumber;

@end

抽象适配器

//
//  Adapter.h
//  LearnAdapter
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "AdapterProtocol.h"

@interface Adapter : NSObject<AdapterProtocol>

///输入对象
@property (weak, nonatomic) id data;

///与输入对象建立联系
- (instancetype)initWithData:(id)data;

@end
//
//  Adapter.m
//  LearnAdapter
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import "Adapter.h"

@implementation Adapter

- (instancetype)initWithData:(id)data {
    self = [super init];
    if (self) {
        self.data = data;
    }
    return self;
}

- (NSString *)name {
    return nil;
}

- (UIColor *)lineColor {
    return nil;
}

- (NSString *)phoneNumber {
    return nil;
}

@end

类适配器(ModelAdapter)

//
//  Model.h
//  LearnAdapter
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface Model : NSObject

///名字
@property (retain, nonatomic) NSString *name;
///线条颜色
@property (retain, nonatomic) UIColor *lineColor;
///电话号码
@property (retain, nonatomic) NSString *phoneNumber;

@end
//
//  ModelAdapter.h
//  LearnAdapter
//
//  Created by 印林泉 on 2017/3/3.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import "Adapter.h"

@interface ModelAdapter : Adapter

@end
//
//  ModelAdapter.m
//  LearnAdapter
//
//  Created by 印林泉 on 2017/3/3.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import "ModelAdapter.h"
#import "Model.h"

@implementation ModelAdapter

- (instancetype)initWithData:(id)data {
    self = [super init];
    if (self) {
        self.data = data;
    }
    return self;
}

- (NSString *)name {
    Model *data = self.data;
    return data.name;
}

- (UIColor *)lineColor {
    Model *data = self.data;
    return data.lineColor;
}

- (NSString *)phoneNumber {
    Model *data = self.data;
    return data.phoneNumber;
}

@end

类适配器(NewModelAdapter)

//
//  NewModel.h
//  LearnAdapter
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NewModel : NSObject

///名字
@property (retain, nonatomic) NSString *name;
///线条颜色
@property (retain, nonatomic) NSString *lineColorHexString;
///电话号码
@property (retain, nonatomic) NSString *phoneNumber;

@end
//
//  NewModelAdapter.h
//  LearnAdapter
//
//  Created by 印林泉 on 2017/3/3.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import "Adapter.h"

@interface NewModelAdapter : Adapter

@end
//
//  NewModelAdapter.m
//  LearnAdapter
//
//  Created by 印林泉 on 2017/3/3.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import "NewModelAdapter.h"
#import "NewModel.h"

@implementation NewModelAdapter

- (instancetype)initWithData:(id)data {
    self = [super init];
    if (self) {
        self.data = data;
    }
    return self;
}

- (NSString *)name {
    NewModel *data = self.data;
    return data.name;
}

- (UIColor *)lineColor {
    NewModel *data = self.data;
    //todo 简化
    UIColor *color = nil;
    if ([data.lineColorHexString isEqualToString:@"black"]) {
        color = [UIColor blackColor];
    }
    else {
        color = [UIColor redColor];
    }
    return color;
}

- (NSString *)phoneNumber {
    NewModel *data = self.data;
    return data.phoneNumber;
}

@end

对象适配器(ObjectAdapter)

//
//  ObjectAdapter.h
//  LearnAdapter
//
//  Created by 印林泉 on 2017/3/3.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import "Adapter.h"

@interface ObjectAdapter : Adapter

@end
//
//  ObjectAdapter.m
//  LearnAdapter
//
//  Created by 印林泉 on 2017/3/3.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import "ObjectAdapter.h"
#import "Model.h"
#import "NewModel.h"

@implementation ObjectAdapter

- (instancetype)initWithData:(id)data {
    self = [super init];
    if (self) {
        self.data = data;
    }
    return self;
}

- (NSString *)name {
    if ([self.data isMemberOfClass:[Model class]]) {
        Model *data = self.data;
        return data.name;
    }
    else if ([self.data isMemberOfClass:[NewModel class]]) {
        NewModel *data = self.data;
        return data.name;
    }
    else {
        return nil;
    }
}

- (UIColor *)lineColor {
    if ([self.data isMemberOfClass:[Model class]]) {
        Model *data = self.data;
        return data.lineColor;
    }
    else if ([self.data isMemberOfClass:[NewModel class]]) {
        NewModel *data = self.data;
        UIColor *color = nil;
        if ([data.lineColorHexString isEqualToString:@"black"]) {
            color = [UIColor blackColor];
        }
        else {
            color = [UIColor redColor];
        }
        return color;
    }
    else {
        return nil;
    }
}

- (NSString *)phoneNumber {
    if ([self.data isMemberOfClass:[Model class]]) {
        Model *model = self.data;
        return model.phoneNumber;
    }
    else if ([self.data isMemberOfClass:[NewModel class]]) {
        NewModel *model = self.data;
        return model.phoneNumber;
    }
    else {
        return nil;
    }
}

@end

使用

//
//  ViewController.m
//  LearnAdapter
//
//  Created by 印林泉 on 2017/3/2.
//  Copyright © 2017年 yiq. All rights reserved.
//

#import "ViewController.h"
#import "View.h"
#import "Model.h"
#import "ModelAdapter.h"
#import "NewModel.h"
#import "NewModelAdapter.h"

#import "ObjectAdapter.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self classAdapter];
    [self objectAdapter];
}

- (void)classAdapter {
    View *view = [[View alloc] initWithFrame:VIEW_FRAME];
    
    Model *model = [[Model alloc] init];
    model.name = @"yinlinqvan";
    model.lineColor = [UIColor redColor];
    model.phoneNumber = @"18721409352";
    
    NewModel *newModel = [[NewModel alloc] init];
    newModel.name = @"linda";
    newModel.lineColorHexString = @"black";
    newModel.phoneNumber = @"18721409352";
    
    ///与输入建立联系
    Adapter *modelAdapter = [[ModelAdapter alloc] initWithData:model];
    //Adapter *modelAdapter = [[NewModelAdapter alloc] initWithData:newModel];
    ///与输出建立联系
    [view loadData:modelAdapter];
    [self.view addSubview:view];
}

- (void)objectAdapter {
    View *view = [[View alloc] initWithFrame:VIEW_FRAME_2];
    
    Model *model = [[Model alloc] init];
    model.name = @"linda";
    model.lineColor = [UIColor redColor];
    model.phoneNumber = @"18721409352";
    
    NewModel *newModel = [[NewModel alloc] init];
    newModel.name = @"linda";
    newModel.lineColorHexString = @"black";
    newModel.phoneNumber = @"18721409352";
    
    ///与输入建立联系
    //Adapter *modelAdapter = [[ObjectAdapter alloc] initWithData:model];
    Adapter *modelAdapter = [[ObjectAdapter alloc] initWithData:newModel];
    ///与输出建立联系
    [view loadData:modelAdapter];
    [self.view addSubview:view];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

review以前的代码,进行适当的改进。

相关文章

网友评论

      本文标题:iOS 适配器模式

      本文链接:https://www.haomeiwen.com/subject/kgpkgttx.html