标签:接口协议 obj inf alt 数据 play span methods dem
将一个类的接口转换成客户希望的另外一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作
需要使用以前开发的“一些现存的对象”,但是新环境中要求的接口是这些现存对象所不满足的
实现步骤:
结构图:
/** * @brief 面向接口编程,协议负责定义适配接口 */ @protocol CellPhoneProtocol <NSObject> - (NSString *)name; - (NSString *)playMusic; - (NSString *)playMovie;
@interface CellPhone : NSObject<CellPhoneProtocol> @property(nonatomic,strong) id data; //生成子类接受数据的变量 - (instancetype)initWithData:(id)data; //便于子类接受数据源 @end
#import "CellPhone.h" @implementation CellPhone - (instancetype)initWithData:(id)data { if (self = [super init]) { self.data = data; //接受数据源数据 } return self; } #pragma mark --Protocol Methods - (NSString *)name { //便于子类重写实现 return nil; } - (NSString *)playMusic { //便于子类重写实现 return nil; } - (NSString *)playMovie { //便于子类重写实现 return nil; } @end
@interface Ios : CellPhone
@end
@implementation Ios - (NSString *)name { IosModel *iosModel = (IosModel *)self.data; return iosModel.name; } - (NSString *)playMovie { IosModel *iosModel = (IosModel *)self.data; return iosModel.movieName; } - (NSString *)playMusic { IosModel *iosModel = (IosModel *)self.data; return iosModel.musicName; } @end
@interface Android : CellPhone
@enAndroid.m
@implementation Android - (NSString *)name { AnroidModel *andriodModel = (AnroidModel *)self.data; return andriodModel.name; } - (NSString *)playMovie { AnroidModel *andriodModel = (AnroidModel *)self.data; return andriodModel.movieName; } - (NSString *)playMusic { AnroidModel *andriodModel = (AnroidModel *)self.data; return andriodModel.musicName; } @end
@interface WPhone : CellPhone
@enWPhone.m
@implementation WPhone - (NSString *)name { WPhoneModel *wphoneModel = (WPhoneModel *)self.data; return wphoneModel.name; } - (NSString *)playMovie { WPhoneModel *wphoneModel = (WPhoneModel *)self.data; return wphoneModel.movieName; } - (NSString *)playMusic { WPhoneModel *wphoneModel = (WPhoneModel *)self.data; return wphoneModel.musicName; } @end
#import "ViewController.h" #import "CellPhone.h" #import "Ios.h" #import "IosModel.h" #import "Android.h" #import "AnroidModel.h" #import "WPhone.h" #import "WPhoneModel.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self adapterWphone]; NSLog(@"%@",[self.delegate name]); NSLog(@"%@",[self.delegate playMovie]); NSLog(@"%@",[self.delegate playMusic]); } //适配iOS - (void)adapterIos { //数据源 IosModel *iosModel = [[IosModel alloc]init]; iosModel.name = @"iOS"; iosModel.musicName = @"iOS--音乐"; iosModel.movieName = @"iOS--电影"; //父类指针指向子类对象 CellPhone *cellPhone = [[Ios alloc] initWithData:iosModel]; self.delegate = cellPhone; } //适配andriod - (void)adapterAndriod { //数据源 AnroidModel *anroidModel = [[AnroidModel alloc]init]; anroidModel.name = @"Andriod"; anroidModel.musicName = @"Andriod--音乐"; anroidModel.movieName = @"Andriod--电影"; //父类指针指向子类对象 CellPhone *cellPhone = [[Ios alloc] initWithData:anroidModel]; self.delegate = cellPhone; } //适配Wphone - (void)adapterWphone { //数据源 WPhoneModel *wphoneModel = [[WPhoneModel alloc]init]; wphoneModel.name = @"WPhone"; wphoneModel.musicName = @"WPhone--音乐"; wphoneModel.movieName = @"WPhone--电影"; //父类指针指向子类对象 CellPhone *cellPhone = [[Ios alloc] initWithData:wphoneModel]; self.delegate = cellPhone; } - (void)setDelegate:(id<CellPhoneProtocol>)delegate { _delegate = delegate; } @end
2019-09-07 17:32:08.203781+0800 AdapterPattern[12274:6524407] WPhone 2019-09-07 17:32:08.203983+0800 AdapterPattern[12274:6524407] WPhone--电影 2019-09-07 17:32:08.204108+0800 AdapterPattern[12274:6524407] WPhone--音乐
AdapterPattern
标签:接口协议 obj inf alt 数据 play span methods dem
原文地址:https://www.cnblogs.com/lxlx1798/p/11482253.html