适配器

作者: XZhongWen | 来源:发表于2019-03-12 11:05 被阅读0次

iOS 设计模式 - 适配器

原理图

对象适配器

Object Adapter.png

类适配器

多重继承


Class Adaptor.png

说明

  • 为了让客户端尽可能的通用,我们使用适配器模式来隔离客户端与外部参数的联系,只让客户端与适配器通信.
  • 实现重用没有提供给客户端所要求接口的类的方式
  • 协同具有不兼容接口类之间的工作

代码实现

名片协议

//
//  BusinessCardAdapterProtocol.h
//  Adapter
//
//  Created by mye on 2019/3/12.
//  Copyright © 2019 mye. All rights reserved.
//

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

@protocol BusinessCardAdapterProtocol <NSObject>

- (NSString *)name;

- (UIColor *)lineColor;

- (NSString *)phoneNumber;

@end

适配器

//
//  BusinessCardAdapter.h
//  Adapter
//
//  Created by mye on 2019/3/12.
//  Copyright © 2019 mye. All rights reserved.
//

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

@interface BusinessCardAdapter : NSObject <BusinessCardAdapterProtocol>

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

/**
 *  与输入对象建立联系
 *
 *  @param data 输入的对象
 *
 *  @return 实例对象
 */
- (instancetype)initWithData:(id)data;

@end

//
//  BusinessCardAdapter.m
//  Adapter
//
//  Created by mye on 2019/3/12.
//  Copyright © 2019 mye. All rights reserved.
//

#import "BusinessCardAdapter.h"
#import <UIKit/UIKit.h>

@implementation BusinessCardAdapter

- (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

客户端

//
//  BusinessCard.h
//  Adapter
//
//  Created by mye on 2019/3/12.
//  Copyright © 2019 mye. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "BusinessCardAdapterProtocol.h"

@interface BusinessCard : UIView

- (void)loadData:(id<BusinessCardAdapterProtocol>)data;

@end

//
//  BusinessCard.m
//  Adapter
//
//  Created by mye on 2019/3/12.
//  Copyright © 2019 mye. All rights reserved.
//

#import "BusinessCard.h"

@interface BusinessCard()

/**
 *  名字
 */
@property (nonatomic, strong) NSString *name;

/**
 *  线条颜色
 */
@property (nonatomic, strong) UIColor  *lineColor;

/**
 *  电话号码
 */
@property (nonatomic, strong) NSString *phoneNumber;

@property (nonatomic, strong) UILabel  *nameLabel;
@property (nonatomic, strong) UIView   *lineView;
@property (nonatomic, strong) UILabel  *phoneNumberLabel;

@end

@implementation BusinessCard

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

- (void)setup {
    
    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;
    
    
    self.nameLabel      = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 150, 25)];
    self.nameLabel.font = [UIFont fontWithName:@"Avenir-Light" size:20.f];
    [self addSubview:self.nameLabel];
    
    
    self.lineView                 = [[UIView alloc] initWithFrame:CGRectMake(0, 45, 200, 5)];
    [self addSubview:self.lineView];
    
    
    self.phoneNumberLabel               = [[UILabel alloc] initWithFrame:CGRectMake(41, 105, 150, 20)];
    self.phoneNumberLabel.textAlignment = NSTextAlignmentRight;
    self.phoneNumberLabel.font          = [UIFont fontWithName:@"AvenirNext-UltraLightItalic" size:16.f];
    [self addSubview:self.phoneNumberLabel];
}

- (void)loadData:(id <BusinessCardAdapterProtocol>)data {
    
    self.name        = [data name];
    self.lineColor   = [data lineColor];
    self.phoneNumber = [data phoneNumber];
}

@synthesize name        = _name;
@synthesize lineColor   = _lineColor;
@synthesize phoneNumber = _phoneNumber;

- (void)setName:(NSString *)name {
    
    _name           = name;
    _nameLabel.text = name;
}

- (NSString *)name {
    
    return _name;
}

- (void)setLineColor:(UIColor *)lineColor {
    
    _lineColor                = lineColor;
    _lineView.backgroundColor = _lineColor;
}

- (UIColor *)lineColor {
    
    return _lineColor;
}

- (void)setPhoneNumber:(NSString *)phoneNumber {
    
    _phoneNumber           = phoneNumber;
    _phoneNumberLabel.text = phoneNumber;
}

- (NSString *)phoneNumber {
    
    return _phoneNumber;
}

@end

相关文章

  • 【设计模式】适配器模式

    学习文章 iOS设计模式 - 适配器 适配器模式(Adapter):类适配器、对象适配器 类图 说明 类适配器: ...

  • ListView常用知识点归纳

    适配器 适配器实现过程:共3步:新建适配器-->添加数据源到适配器-->视图加载适配器 适配器的数据源:Array...

  • iOS设计模式 (五) 适配器模式

    适配器模式 iOS中的适配器模式,主要由目标协议,适配者,适配器三部分组成. 适配器模式分类 类适配器: 适配器是...

  • 设计模式-适配器

    适配器模式,目的是为了适配补偿,对于适配器模式,我们要学习的两种方式是类适配器和对象适配器。 类适配器 类适配器是...

  • Java设计模式(二)

    talk is cheap show me the code 适配器模式 类适配器模式 接口适配器模式 对象适配器...

  • 适配器模式

    目录 1、什么是适配器模式? 2、适配器模式结构? 3、如何实现适配器模式? 4、适配器模式的特点? 5、适配器模...

  • 适配器模式

    先直观感受下什么叫适配器 适配器模式有类的适配器模式和对象的适配器模式两种不同的形式。 类适配器模式 对象适配器模...

  • iOS 适配器模式

    适配器模式创建适配协议,创建抽象适配器类,创建类适配器/对象适配器。 应用,适用场景电源适配器,普通充电器(类适配...

  • 适配器模式

    对象适配器目标类--继承或引用<---适配器--->关联---适配者 类适配器目标类--继承或引用<---适配器-...

  • ArrayAdapter列表适配器SimpleAdapter简单

    ArrayAdapter列表适配器SimpleAdapter简单的适配器BaseAdapter基础适配器packa...

网友评论

      本文标题:适配器

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