適配器模式
- iOS中的適配器模式非驮,主要由目標(biāo)協(xié)議,適配者,適配器三部分組成.
適配器模式分類
- 類適配器: 適配器是繼承自適配者的
- 對象適配器 : 適配器是引用適配者的
適配器模式UML
適配器模式實(shí)例應(yīng)用
#import <Foundation/Foundation.h>
@interface USD : NSObject
- (float)accepteUSD;
@end
#import "USD.h"
@implementation USD
- (float)accepteUSD
{
return 100;
}
@end
- AcepptDapter類(目標(biāo)協(xié)議)
#import <Foundation/Foundation.h>
@protocol AcepptDapter <NSObject>
- (float)dapterUSD;
@end
#import "USD.h"
#import "AcepptDapter.h"
@interface DapaterRMB : USD<AcepptDapter>
@end
#import "DapaterRMB.h"
@implementation DapaterRMB
- (float)dapterUSD
{
return [self accepteUSD] *6.61;
}
@end
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
#import "ViewController.h"
#import "DapaterRMB.h"
#import "ObjectDapterCNY.h"
#import "USD.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
DapaterRMB *dapterRMB = [[DapaterRMB alloc] init];
float dd = [dapterRMB dapterUSD];
NSLog(@"ddd ====== :%0.2f",dd);
ObjectDapterCNY *cny = [[ObjectDapterCNY alloc] initWithObject:[USD new]];
float cc = [cny dapterUSD];
NSLog(@"cc =====:%0.2f",cc);
}
@end
#import <Foundation/Foundation.h>
#import "USD.h"
#import "AcepptDapter.h"
@interface ObjectDapterCNY : NSObject<AcepptDapter>
@property (nonatomic, strong)USD *usdDapter;
- (instancetype)initWithObject:(USD *)dapterUSD;
@end
#import "ObjectDapterCNY.h"
@implementation ObjectDapterCNY
- (instancetype)initWithObject:(USD *)dapterUSD
{
self = [super init];
if (self) {
self.usdDapter = dapterUSD;
}
return self;
}
- (float)dapterUSD
{
return [self.usdDapter accepteUSD] *6.61;
}
@end