设计模式 - 适配器

设计模式 - 适配器

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

源码如下:

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

以下是核心代码处:

时间: 2024-09-11 14:14:08

设计模式 - 适配器的相关文章

iOS设计模式 - 适配器

iOS设计模式 - 适配器   效果   说明 1. 为了让客户端尽可能的通用,我们使用适配器模式来隔离客户端与外部参数的联系,只让客户端与适配器通信. 2. 本教程实现了适配器模式的类适配器与对象适配器两种模式,各有优缺点. 3. 如果对面向对象基本原理以及设计模式基本原理不熟悉,本教程会变得难以理解.   源码 https://github.com/YouXianMing/iOS-Design-Patterns // // BusinessCardView.h // Adapter // /

解读设计模式----适配器模式(Adapter Pattern)

在金庸笔下,三大神功都是难得之宝,多少人为得到他而......,仔细的分析下这三大神功,还是北冥较好,呵呵.我们从软件设计的角度来看,这不知算不算得上是一种复用(功力复用)的思想,只不过有点残忍罢.而在软件设计领域里,"复用"在某些时候也会出现很多问题,比如平台不兼容,开发语言不同或是接口不同等多种原因,弄得不好会不会出现既浪费了别人的现有资源,而自己的系统又无法完成呢?这有点像吸星----损人又损己. 企图将设计做好,就能够一劳永逸地坐享其成,这样的愿望就好上面所提到的吸星神功一般,

Java编程思想学习笔记——接口

1.抽象类和抽象方法 抽象方法:不完整的,仅有声明而没有方法体. abstract void f(); 抽象类:包含抽象方法的类.(若一个类包含一个或多个抽象方法,则该类必须限定为抽象的.) 1.用抽象类直接创建对象是不安全的,因为这个抽象类是不完整的.编译器通过这种方式保证了 抽象类的纯粹性. public abstract class Person { public abstract void eat(); public abstract void pee(); public abstrac

Android设计模式之适配器(Adapter)模式_Android

本文实例为大家分享了Android适配器模式源码,供大家参考,具体内容如下 1. 模式介绍 1.1模式的定义: 适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 1.2模式的使用场景: 用电源接口做例子,笔记本电脑的电源一般都是接受5V的电压,但是我们生活中的电线电压一般都是220V的输出.这个时候就出现了不匹配的状况,在软件开发中我们称之为接口不兼容,此时就需要适配器来进行一个接口转换.在软件开发中有一句话正好体现了这点:任

ASP.NET的适配器设计模式(Adapter)应用详解_实用技巧

前天有一网友问及有关设计模式的适配器模式(Adapter)时,说不太好理解.让Insus.NET能否举个简单的例子来说明一下.下面的动画是Insus.NET做出来的效果: 上面的演示,两个灯的规格一样,要求输入的电压为15伏. Light1是直接使用,而Light2是使用Adapter(电源适配器).因此Light1只能接收15伏的电压,小于15伏,会提示电压过低,如果超过了15伏,Light1肯定被烧坏. Light2使用了电源适配器,它接收15伏至220的电压,在这电压范围之内,电源适配器会

关于适配器设计模式,我越来越糊涂了

问题描述 以下是老师给我们讲适配器设计模式举得例子,我怎么都看不懂,感觉没有用到适配器设计模式,或者是老师本身举得这个例子就太绕了,我真的晕了.有谁给我细说说这些代码?interfacePowerSourceA{voidinsert();}classPowerSourceAImplimplementsPowerSourceA{publicvoidinsert(){System.out.println("电源A开始工作了...");}}interfacePowerSourceB{void

C#设计模式之适配器设计模式(Adapter)

结构模式(Structural Pattern)描述如何将类或者对象结合在一起形成更大的结构.结构模式描述两种不同的东西:类与类的实例.根据这一点,结构模式可以分为类的结构模式和对象的结构模式. 后续内容将包括以下结构模式: 适配器模式(Adapter):Match interfaces of different classes 合成模式(Composite):A tree structure of simple and composite objects 装饰模式(Decorator):Add

Java 设计模式 接口型模式 之 适配器Adapter模式 (二)

适配器模式的意图 : 使用不同接口的类所提供的服务为客户端提供其所希望的接口; -- 问题解决场景 : 在 类A 中实现了接口中的抽象方法, 客户端B 已经定义好了方法的调用, 但是调用的方法 与 类A 中的方法名不同, 这时我们就需要适配器模式了; -- eg : 类A 实现了接口A1, 类B 实现了接口B1, 这里C调用 A 和 B 希望 A 和 B 能提供相同方法的接口, 这时我们需要使用适配器模式; 1. 接口适配 (1) 接口适配简介 接口适配 :  -- 问题场景 : 客户端需要调用

《JavaScript设计模式》——10.2 生活中的适配器

10.2 生活中的适配器 "以前你没有接触过么?"小铭接着说"这可是编程中一种很常见的模式.其实生活中这种模式也很常见,你看公司的水房的两根垂直相交的水管连接处的直角弯管了么?它就是一个适配器,它使得两个不同方向的水管可以疏通流水.再比如我们三角插头手机充电器对于两项插头是不能用的,此时我们就需要一个三项转两项插头电源适配器等等,这些都是适配器.如果你明白这些,那么为页面中的代码写适配器就不难了,其实就是为两个代码库所写的代码兼容运行而书写的额外代码.有了这样的适配器,你就不