设计模式 - 适配器

简介:

设计模式 - 适配器

适配器也叫接口适配,其目的是为了减少不同类型数据之间的耦合度而进行的数据转换,有利于减少冗余代码。

源码如下:

ModelCell.h 与 ModelCell.m

//
//  ModelCell.h
//  Adapter
//
//  Created by YouXianMing on 15/1/6.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ModelCell : UITableViewCell

@property (nonatomic, strong) UILabel *name;
@property (nonatomic, strong) UILabel *age;

@end


//
//  ModelCell.m
//  Adapter
//
//  Created by YouXianMing on 15/1/6.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "ModelCell.h"

@implementation ModelCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.name           = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 18)];
        self.name.font      = [UIFont boldSystemFontOfSize:16.f];
        self.name.textColor = [UIColor redColor];
        [self addSubview:self.name];
        
        self.age           = [[UILabel alloc] initWithFrame:CGRectMake(10, 18 + 10, 200, 14)];
        self.age.font      = [UIFont italicSystemFontOfSize:12.f];
        self.age.textColor = [UIColor blackColor];
        [self addSubview:self.age];
        
    }
    
    return self;
}

@end

AdapterModel.h 与  AdapterModel.m
//
//  AdapterModel.h
//  Adapter
//
//  Created by YouXianMing on 15/1/6.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface AdapterModel : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *age;

/**
 *  根据字典来初始化
 *
 *  @param dic model字典
 *
 *  @return 实例对象
 */
+ (instancetype)adapterWithDictionary:(NSDictionary *)dic;

/**
 *  根据对象来初始化
 *
 *  @param dic model字典
 *
 *  @return 实例对象
 */
+ (instancetype)adapterWithObject:(id)object;

@end


//
//  AdapterModel.m
//  Adapter
//
//  Created by YouXianMing on 15/1/6.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "AdapterModel.h"

@implementation AdapterModel

+ (instancetype)adapterWithDictionary:(NSDictionary *)dic {
    
    AdapterModel *model = nil;
    
    if (dic != nil && [dic isKindOfClass:[NSDictionary class]]) {
        model      = [AdapterModel new];
        model.name = dic[@"name"];
        model.age  = dic[@"age"];
    }
    
    return model;
}

+ (instancetype)adapterWithObject:(id)object {
    // 预留
    
    return [AdapterModel new];
}

@end

控制器源码:
//
//  ViewController.m
//  Adapter
//
//  Created by YouXianMing on 15/1/6.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "ViewController.h"
#import "ModelCell.h"
#import "AdapterModel.h"

static NSString *ModelCellFlag = @"ModelCell";

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView     *tableView;
@property (nonatomic, strong) NSMutableArray  *dataArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化数据源
    [self createDataSource];
    
    // 初始化tableView
    [self createTableView];
}

#pragma mark - 数据源相关
- (void)createDataSource {
    self.dataArray = [NSMutableArray array];
    
    [self.dataArray addObject:[AdapterModel adapterWithDictionary:@{@"name": @"FireEmblem",
                                                                    @"age" : @"40"}]];
    
    [self.dataArray addObject:[AdapterModel adapterWithDictionary:@{@"name": @"YouXianMing",
                                                                    @"age" : @"27"}]];
    
    [self.dataArray addObject:[AdapterModel adapterWithDictionary:@{@"name": @"QiuLiang",
                                                                    @"age" : @"28"}]];
    
    [self.dataArray addObject:[AdapterModel adapterWithDictionary:@{@"name": @"PingKang",
                                                                    @"age" : @"25"}]];
}
#pragma mark - tableView相关
- (void)createTableView {
    self.tableView            = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate   = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[ModelCell class] forCellReuseIdentifier:ModelCellFlag];
    [self.view addSubview:self.tableView];
}

#pragma mark row数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
#pragma mark cell初始化
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ModelCell *cell     = [tableView dequeueReusableCellWithIdentifier:ModelCellFlag];
    
    AdapterModel *model = self.dataArray[indexPath.row];
    
    cell.name.text      = model.name;
    cell.age.text       = model.age;
    
    return cell;
}
#pragma mark cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

@end

以下是核心代码处:

目录
相关文章
|
设计模式 中间件 Java
设计模式3:代理、适配器、装饰器模式
代理模式是一种结构型设计模式,通过中间件解耦服务提供者和使用者,使使用者间接访问服务提供者,便于封装和控制。
310 3
|
设计模式 API
【设计模式】适配器和桥接器模式有什么区别
【设计模式】适配器和桥接器模式有什么区别
434 1
|
设计模式 uml
设计模式-适配器模式(对象适配器)
设计模式-适配器模式(对象适配器)
165 0
|
设计模式 前端开发 Java
设计模式之适配器模式(类适配器、对象适配器、源码体现)
设计模式之适配器模式(类适配器、对象适配器、源码体现)
257 0
|
设计模式 API uml
设计模式-适配器
设计模式-适配器
230 0
|
设计模式
24种设计模式-适配器设计模式
24种设计模式-适配器设计模式
24种设计模式-适配器设计模式
|
设计模式 大数据
大数据开发基础的设计模式的适配器
适配器模式是大数据开发基础的设计模式之一。它是一种结构型模式,用于将一个类的接口转换成客户端所期望的另一个接口。适配器让原本由于接口不兼容而不能一起工作的类可以合作无间。
225 0
|
设计模式 Java
浅析Java设计模式【2.2】——适配器
Java常用设计模式,适配器模式
263 0
浅析Java设计模式【2.2】——适配器
|
SQL 存储 设计模式
一看就懂的适配器设计模式
一般客户端通过目标类的接口访问它所提供的服务。有时,现有类可以满足客户端类的需要,但所提供接口不一定是客户端所期望的,可能因为现有类中方法名与目标类中定义的方法名不一致。
197 0
|
设计模式 前端开发
前端通用编程基础的设计模式之适配器
在前端开发中,我们常常需要对外部库或者组件进行使用和集成。但是这些库或者组件的接口可能并不符合我们自己的需求,这时候就需要使用适配器模式来实现接口的转换和兼容。
305 0

热门文章

最新文章