Swift设计模式之适配器模式
来源:互联网 发布:sql 删除行数据 编辑:程序博客网 时间:2024/05/19 11:47
转自
- Swift设计模式
原文
- Design-Patterns-In-Swift
// 适配器模式// 百度百科:适配器模式(有时候也称包装样式或者包装)将一个类的接口适配成用户所期待的。一个适配允许通常因为接口不兼容而不能在一起工作的类工作在一起,做法是将类自己的接口包裹在一个已存在的类中// 设计模式分类:结构型模式/** * 两针插座接口 */protocol TwoPinPlugProtocol { var pin1: String { get } var pin2: String { get }}/// 现在有一个三针插座class ThreePinPlug { var pin1: String { get { return "pin1 of ThreePinPlug" } } var pin2: String { get { return "pin2 of ThreePinPlug" } } var pin3: String { get { return "pin3 of ThreePinPlug" } }}/// 这是一个适配器, 将三针插座转化为两针插座class TwoPinPlugAdapter: TwoPinPlugProtocol { let threePinPlug: ThreePinPlug! init(plug: ThreePinPlug) { self.threePinPlug = plug } var pin1: String { get { return threePinPlug.pin1 } } var pin2: String { get { return threePinPlug.pin2 } }}/** 打印两针插座信息(不可以接收三针插座) - parameter plug: 两针插座 */func printTwoPinPlugInfo(plug: TwoPinPlugProtocol) -> Void { print("two pin plug info: [pin1 = \(plug.pin1), pin2 = \(plug.pin2)]")}let threePinPlug = ThreePinPlug() // 目前有一个三针插座let twoPinPlugAdapter = TwoPinPlugAdapter(plug: threePinPlug) // 用适配器来包装printTwoPinPlugInfo(twoPinPlugAdapter) // 包装后可以将三针插座放入只接收两针插座的接口中
0 0
- Swift设计模式之适配器模式
- 设计模式之适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式 之 适配器
- 设计模式 之 适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式 之 适配器模式
- 设计模式之适配器模式
- 设计模式之适配器模式
- 设计模式之适配器模式
- 设计模式之适配器模式
- 设计模式之适配器模式
- Swift设计模式之外观模式
- 计算机图形学(二)输出图元_10_多边形填充区_2_分割凹多边形
- ANSI、GBK、GB2312、UTF-8、GB18030和、UNICODE编码解读
- 增强学习Reinforcement Learning经典算法梳理3:TD方法
- Android Https相关完全解析 当OkHttp遇到Https
- Swift设计模式之适配器模式
- 1.Linux基础权限
- 权限管理-数据权限
- XML无效字符过滤
- [Gradle]Error:java.lang.OutOfMemoryError: Java heap space . Please assign more memory to Gradle in t
- Swift设计模式之桥梁模式
- jQuery基础——事件篇
- Oracle正则表达式小结
- Swift设计模式之命令模式