适配器模式:将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
优点:
1. 将目标类和适配者类解耦,通过使用适配器让不兼容的接口变成了兼容,让客户从实现的接口解耦。
2. 增加了类的透明性和复用性,将具体的实现封装在适配者类中,对于客户端类来说是透明的,而且提高了适配者的复用性。
3. 灵活性和扩展性都非常好在不修改原有代码的基础上增加新的适配器类,符合“开闭原则”。
缺点:
1.增加了中间层导致性能会有所下降。
2.由于增加中间类,导致降低代码可读性
总结:
在系统的数据和行为都正确,但接口不符时,我们应该考虑用适配器,目的是使控制范围之外的一个原有对象与某个接口匹配。适配器模式主要应用于希望复用一些现存的类。但是接口又与复用的环境要求不一致的情况。
项目地址:https://github.com/GameRisker/Study-Design-Patterns.git
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// // main.m // Adapter // // Created by GameRisker on 2/28/16. // Copyright (c) 2016 GameRisker. All rights reserved. // #import "Forwards.h" #import "Player.h" #import "Translator.h" #import <Foundation/Foundation.h> int main(int argc, const char *argv[]) { @autoreleasepool { // insert code here... Player *b = [[Forwards alloc] initWithName:@"Game"]; [b attack]; Translator *t = [[Translator alloc] initWithName:@"Risker"]; [t attack]; [t defense]; } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// // Player.h // // // Created by GameRisker on 2/28/16. // // #import <Foundation/Foundation.h> @interface Player : NSObject @property(nonatomic, copy) NSString *name; - (void)attack; - (void)defense; - (instancetype)initWithName:(NSString *)name; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
// // Player.m // // // Created by GameRisker on 2/28/16. // // #import "Player.h" @implementation Player - (void)attack { NSLog(@"%@ attack !", self.name); } - (void)defense { NSLog(@"%@ defense !", self.name); } - (instancetype)initWithName:(NSString *)name { if (self = [super init]) { self.name = name; } return self; } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// // Forwards.h // // // Created by GameRisker on 2/28/16. // // #import "Player.h" @interface Forwards : Player @end |
1 2 3 4 5 6 7 8 9 10 11 12 |
// // Forwards.m // // // Created by GameRisker on 2/28/16. // // #import "Forwards.h" @implementation Forwards @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// // ForeignCenter.h // // // Created by GameRisker on 2/28/16. // // #import <Foundation/Foundation.h> @interface ForeignCenter : NSObject @property(nonatomic, copy) NSString *name; - (void)foreignAttack; - (void)foreignDefense; - (instancetype)initWithName:(NSString *)name; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
// // ForeignCenter.m // // // Created by GameRisker on 2/28/16. // // #import "ForeignCenter.h" @implementation ForeignCenter - (void)foreignAttack { NSLog(@"%@ foreignAttack !", self.name); } - (void)foreignDefense { NSLog(@"%@ foreignDefense !", self.name); } - (instancetype)initWithName:(NSString *)name { if (self = [super init]) { self.name = name; } return self; } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// // Translator.h // // // Created by GameRisker on 2/28/16. // // #import "ForeignCenter.h" #import "Player.h" @interface Translator : Player @property(nonatomic, strong) ForeignCenter *center; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
// // Translator.m // // // Created by GameRisker on 2/28/16. // // #import "Translator.h" @implementation Translator - (void)attack { [self.center foreignAttack]; } - (void)defense { [self.center foreignDefense]; } - (instancetype)initWithName:(NSString *)name { if (self = [super init]) { self.center = [[ForeignCenter alloc] initWithName:name]; } return self; } @end |
2 Comments
年中快乐!
博客不错,嘎嘎!