iOS设计模式 - 适配器
iOS设计模式 - 适配器

效果


说明
1. 为了让客户端尽可能的通用,我们使用适配器模式来隔离客户端与外部参数的联系,只让客户端与适配器通信.
2. 本教程实现了适配器模式的类适配器与对象适配器两种模式,各有优缺点.
3. 如果对面向对象基本原理以及设计模式基本原理不熟悉,本教程会变得难以理解.
源码
https://github.com/YouXianMing/iOS-Design-Patterns
//
// BusinessCardView.h
// Adapter
//
// Created by YouXianMing on 15/7/25.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h>
#import "BusinessCardAdapterProtocol.h" #define BUSINESS_FRAME CGRectMake(0, 0, 200, 130) @interface BusinessCardView : UIView /**
* 名字
*/
@property (nonatomic, strong) NSString *name; /**
* 线条颜色
*/
@property (nonatomic, strong) UIColor *lineColor; /**
* 电话号码
*/
@property (nonatomic, strong) NSString *phoneNumber; /**
* 加载数据(实现了BusinessCardAdapterProtocol协议的数据)
*
* @param data 实现了BusinessCardAdapterProtocol协议的数据
*/
- (void)loadData:(id <BusinessCardAdapterProtocol>)data; @end
//
// BusinessCardView.m
// Adapter
//
// Created by YouXianMing on 15/7/25.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "BusinessCardView.h" @interface BusinessCardView () @property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UILabel *phoneNumberLabel; @end @implementation BusinessCardView #pragma mark - 初始化
- (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(, );
self.layer.shadowRadius = .f;
self.layer.shadowColor = [UIColor grayColor].CGColor; self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
self.nameLabel.font = [UIFont fontWithName:@"Avenir-Light" size:.f];
[self addSubview:self.nameLabel]; self.lineView = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
[self addSubview:self.lineView]; self.phoneNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
self.phoneNumberLabel.textAlignment = NSTextAlignmentRight;
self.phoneNumberLabel.font = [UIFont fontWithName:@"AvenirNext-UltraLightItalic" size:.f];
[self addSubview:self.phoneNumberLabel];
} - (void)loadData:(id <BusinessCardAdapterProtocol>)data { self.name = [data name];
self.lineColor = [data lineColor];
self.phoneNumber = [data phoneNumber];
} #pragma mark - 重写setter,getter方法
@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
//
// BusinessCardAdapter.h
// NormalProblem
//
// Created by YouXianMing on 15/7/25.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "BusinessCardAdapterProtocol.h" @interface BusinessCardAdapter : NSObject <BusinessCardAdapterProtocol> /**
* 输入对象
*/
@property (nonatomic, weak) id data; /**
* 与输入对象建立联系
*
* @param data 输入的对象
*
* @return 实例对象
*/
- (instancetype)initWithData:(id)data; @end
//
// BusinessCardAdapter.m
// NormalProblem
//
// Created by YouXianMing on 15/7/25.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "BusinessCardAdapter.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
//
// BusinessCardAdapterProtocol.h
// NormalProblem
//
// Created by YouXianMing on 15/7/25.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import <Foundation/Foundation.h> @protocol BusinessCardAdapterProtocol <NSObject> - (NSString *)name; - (UIColor *)lineColor; - (NSString *)phoneNumber; @end
分析
这是基于BusinessCardView构建出来的必不可少的抽象适配器以及一个协议,通过继承抽象适配器来实现具体的适配器,协议是用来统一接口.

对象适配器与类适配器.

客户端(BusinessCardView)只与适配器进行通信,它不关心数据源 NormalModel 与 SpecialModel 的业务逻辑

此处的抽象是核心所在

iOS设计模式 - 适配器的更多相关文章
- IOS设计模式之一(MVC模式,单例模式)
iOS 设计模式-你可能已经听说过这个词,但是你真正理解它意味着什么吗?虽然大多数的开发者可能都会认为设计模式是非常重要的,然而关于设计模式这一主题的文章却不多,并且有时候我们开发者在写代码的时候也不 ...
- iOS书摘之Objective-C编程之道 iOS设计模式解析
来自<Objective-C编程之道iOS设计模式解析>一书的摘要总结 一.Prototype 原型模式 定义:使用原型实例指定创建对象的种类,并通过复制这个原型创建新的对象.(<设 ...
- iOS 设计模式
很赞的总结 iOS Design Patterns 中文版 IOS设计模式之一(MVC模式,单例模式) IOS设计模式之二(门面模式,装饰器模式) IOS设计模式之三(适配器模式,观察者模式) IOS ...
- iOS 设计模式之工厂模式
iOS 设计模式之工厂模式 分类: 设计模式2014-02-10 18:05 11020人阅读 评论(2) 收藏 举报 ios设计模式 工厂模式我的理解是:他就是为了创建对象的 创建对象的时候,我们一 ...
- iOS设计模式之生成器
iOS设计模式之生成器 1.生成器模式的定义 (1): 将一个复杂的对象的构件与它的表示分离,使得相同的构建过程能够创建不同的表示 (2): 生成器模式除了客户之外还包括一个Director(指导者) ...
- IOS设计模式之三:MVC模式
IOS设计模式之三:MVC模式 模型-视图-控制器 这个模式其实应该叫做MCV,用控制器把model与view隔开才对,也就是model与view互相不知道对方的存在,没有任何瓜葛,他们就像一个团 ...
- iOS设计模式 - 享元
iOS设计模式 - 享元 原理图 说明 享元模式使用共享物件,用来尽可能减少内存使用量以及分享资讯给尽可能多的相似物件:它适合用于只是因重复而导致使用无法令人接受的大量内存的大量物件.通常物件中的部分 ...
- iOS设计模式 - 责任链
iOS设计模式 - 责任链 原理图 说明 在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链.请求在这个链上传递,直到链上的某一个对象决定处理此请求.发出这个请求的客户端并不知道链 ...
- iOS设计模式 - 模板
iOS设计模式 - 模板 原理图 说明 定义一个操作中的算法的骨架,而将步骤延迟到子类中.模板方法使得子类可以不改变一个算法的结构即可重定义算法的某些特定步骤. 源码 https://github.c ...
随机推荐
- 【html5】cookie、sessionStorage、localStorage
第四条补充: cookie中包含domain和path,所有向该域下该路径发送的请求头部都会包含这个cookie: session浏览器关闭后消失,只能由最初给对象存储数据的页面访 ...
- js中in关键字的用法
1. 在For...In 声明用于对数组或者对象的属性进行循环/迭代操作. 例子:var a = new Array; for(x in a){ console.log(x); } 2. 判断对象是否 ...
- leetcode5:subsets问题
问题描述: Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subse ...
- mysql 索引的简单使用
1 索引(index) 索引是一个单独的.物理的数据库结构, 它是某个表中一列或若干列值的集合和相应的指向表中物理标识这些值的数据页的逻辑指针清单 他的作用和字典的目录是一样的,就是为了加快查询的速度 ...
- java当中的定时器
对于开发游戏项目的同胞来说,Timer 这个东西肯定不会陌生,今天对以前自己经常使用的定时进行了一番小小的总结!没有写具体实现的原理,只是列举出了其中的四种比较常见的使用方法,相对而言,所以只要按照其 ...
- R语言数据重塑cbind+rbind+merge+ melt+cast
R语言中的数据重塑是关于变化的数据分为行和列的方式.大多数R地数据处理的时候是通过将输入的数据作为一个数据帧进行.这是很容易提取一个数据帧的行和列数据,但在某些情况,当我们需要的数据帧的格式是不同的来 ...
- java调用ruby代码
问题: 最近在做一个应用的时候碰到了一个问题.客户端需要调用服务器端传回的脚本信息,然后执行.其中脚本类型包括ruby.而java中调用ruby的代码大致如下: String jrubyCode=&q ...
- UICollectionView二级树展开
公司项目是社区类的,上周就下载了些社区类APP看了下,发现小区无忧首页的顶部蛮好玩,就试着做了一下,现在先把UICollectionView的二级树展开功能分享一下 . 1.效果图 2.创建子Coll ...
- Spring----有关bean的配置
1.单例类的配置如果我们想创建一个单例类的bean,只能会通过静态工厂来创建.下图为一个单例类: Stage并没有提供公开的构造方法,构造方法都是私有的,必须通过getInstance()方法获得已经 ...
- SQL Serever学习8——数据表3
创建索引 索引就像是字典的目录一样,可以快速的指定需要的数据. 有没有索引的区别 一个没有索引的集合,如果我们需要查找某一个对象,需要遍历整个集合,直到找到匹配的对象,整个工作费时费力,这只是找一个对 ...