设计模式:适配器模式

所谓适配器模式,通俗来讲就是比如每一个国家都有自己的电压和频率,中国大陆是 220V 和50Hz ,中国香港是230V和50Hz.这时就需要用到适配,在代码中我们也同样会用到,使用适配器可以解决不一致问题,让代码更有扩展性,可以降低数据层与视图成的耦合度。

  • 创建抽象适配器对象
  • 适配器与视图层建立输出联系
  • 适配器与数据层建立输入联系
  • 类适配器与对象适配器

首先规范一个protocol

#import 
#import 

@protocol AdapterProtocol 

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

@end

然后让一个基类遵守该协议,这里就相当于一个接口


#import 
#import "AdapterProtocol.h"

@interface Adapter : NSObject
//输入的数据
@property(nonatomic,weak)id  inputData;
- (instancetype)initWithData:(id)inputData;

@end

定义一个类继承于Adapter,这个类中实现了适配,但是同时达到了解耦的目的。

#import "SubviewAdapter.h"
#import "Model.h"
#import "Model2.h"
@implementation SubviewAdapter

#pragma mark- 重写父类的方法
- (NSString *)name {
    NSString *name ;
    if ([self.inputData isKindOfClass:[Model class]]) {
        Model *model = self.inputData;
        name = model.name;
    }else if ([self.inputData isKindOfClass:[Model2 class]]){
        Model2 *model2 = self.inputData;
        name = model2.name;
    }
    return name;
}

- (UIColor *)lineColor {
    UIColor *lineColor;
    if ([self.inputData isKindOfClass:[Model class]]) {
        Model *model = self.inputData;
        lineColor = model.lineColor;
    }else if ([self.inputData isKindOfClass:[Model2 class]]){
        Model2 *modal2 = self.inputData;
        lineColor = modal2.lineColor;
    }
    return lineColor;
}

- (NSString *)phoneNumber{
    NSString *phoneNum;
    if ([self.inputData isKindOfClass:[Model class]]) {
        Model *model = self.inputData;
        phoneNum = model.phoneNum;
    }else if ([self.inputData isKindOfClass:[Model2 class]]){
        Model2 *model2 = self.inputData;
        phoneNum = model2.phoneNum;
    }
    return phoneNum;
}
@end

其中ModelModel2是两个对象,用对象来赋值比直接数据赋值要更灵活直观不易出错
以上其实都是实现了数据的输入,现在需要把输入的数据进行赋值显示出来


#import 
#import "AdapterProtocol.h"

@interface AdapterView : UIView

@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *phoneNum;
@property(nonatomic,strong) UIColor *lineColor;
//初始化  加载实现了AdapterProtocol协议的对象
- (void)initWithInputData:(id )inputData;
@end
#import "AdapterView.h"

@interface AdapterView ()
@property(nonatomic,strong)UILabel *nameLabel;
@property(nonatomic,strong)UILabel *phoneLabel;
@property(nonatomic,strong)UIView *lineView;
@end

@implementation AdapterView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self  = [super initWithFrame:frame]) {
        
    }
    return self;
}

- (void)setupUI{
    
    self.backgroundColor     = [UIColor whiteColor];
    self.layer.borderWidth   = 0.5f;
    self.layer.shadowOpacity = 0.5f;
    self.layer.shadowOffset  = CGSizeMake(5, 5);
    self.layer.shadowRadius  = 1.f;
    self.layer.shadowColor   = [UIColor grayColor].CGColor;
    
    //name
    self.nameLabel      = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 150, 25)];
    self.nameLabel.font = [UIFont fontWithName:@"Avenir-Light" size:20.f];
    self.nameLabel.text = self.name;
    [self addSubview:self.nameLabel];
    
    //lineView
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 45, 200, 5)];
    self.lineView.backgroundColor = self.lineColor;
    [self addSubview:self.lineView];
    
    //phoneLabel
    self.phoneLabel  = [[UILabel alloc] initWithFrame:CGRectMake(41, 105, 150, 20)];
    self.phoneLabel.textAlignment = NSTextAlignmentRight;
    self.phoneLabel.font = [UIFont fontWithName:@"AvenirNext-UltraLightItalic" size:16.f];
    self.phoneLabel.text = self.phoneNum;
    [self addSubview:self.phoneLabel];
}

- (void)initWithInputData:(id )inputData{
    
    self.name = [inputData name];
    self.lineColor = [inputData lineColor];
    self.phoneNum = [inputData phoneNumber];
    [self setupUI];
 
}

@end

最后在主控制器中写上逻辑代码


Model *model = [[Model alloc]init];
    model.name = @"John";
    model.phoneNum = @"372-432-11";
    model.lineColor = [UIColor lightGrayColor];
    
    //输入的数据源
    SubviewAdapter *subAdapter = [[SubviewAdapter alloc]initWithData:model];
    
    //输出
    AdapterView *adapterView = [[AdapterView alloc]initWithFrame:CGRectMake(0, 0, 200, 130)];
    adapterView.center = self.view.center;
    [adapterView initWithInputData:subAdapter];
    
    [self.view addSubview:adapterView];

总结来说也就是:

  • 首先定义一个公共的接口(吃,跑)
  • 让一个基类去实现这些接口(吃,跑)
  • 创建一个子类继承与上面的基类,这里面重写接口的方法。(怎么吃,怎么跑),如果想要增加新的功能,就只需要在子类中新增一个方法(飞),就跟适配器一样,只要有不同的频率和电压出现,额外新增一个就OK了。
  • 把输入的数据赋值 跟tableViewCell赋值的方法一样

你可能感兴趣的:(设计模式:适配器模式)