桥接模式就是把声明和实现分离。怎么理解?就是将一个类的方法在另一个与之关联的类里实现。举例来说,比如电视机遥控器向不同类型的电视机发送指令,怎么做到?首先,因为遥控器是唯一的,电视机是不同的,所以我们得把电视机抽象出来,就是创建一个抽象类来声明一个公用方法,然而抽象类并不可以实现(就算实现也是公用的功能才可以), 让它的子类来实现各自个性化的功能。
PS:在这里,我也把遥控器也抽象了一下。这个和桥接无关,请勿混淆。
代码举例:
1.---------------------------抽象类
抽象类电视:.h
#import <Foundation/Foundation.h>
@interface HZAbstractTV : NSObject
// 抽象方法,抽象类无需实现。子类不同,所以留给子类具体实现。
- (void)receiveCommand:(NSString *)command;
@end
抽象类遥控器:.h和.m
#import <Foundation/Foundation.h>
@class HZAbstractTV;
@interface HZAbstractRemote : NSObject
@property(nonatomic, strong) HZAbstractTV *TV;
- (void)sendCommand:(NSString *)command;
@end
#import "HZAbstractRemote.h"
#import "HZAbstractTV.h"
@implementation HZAbstractRemote
- (void)sendCommand:(NSString *)command {
[self.TV receiveCommand:command];
}
@end
2.-------------------------子类
子类遥控器:.h和.m
#import "HZAbstractRemote.h"
@interface HZRemote : HZAbstractRemote
- (void)up;
- (void)down;
- (void)left;
- (void)right;
@end
#import "HZRemote.h"
@implementation HZRemote
- (void)up {
[self sendCommand:@"up"];
}
- (void)down {
[self sendCommand:@"down"];
}
- (void)left {
[self sendCommand:@"left"];
}
- (void)right {
[self sendCommand:@"right"];
}
@end
子类电视机:
小米电视机: .h和.m
#import "HZAbstractTV.h"
@interface HZXiaoMiTV : HZAbstractTV
@end
#import "HZXiaoMiTV.h"
@implementation HZXiaoMiTV
- (void)receiveCommand:(NSString *)command {
// 打印
NSLog(@"小米电视接受到了指令-----:%@", command);
}
@end
康佳电视机:.h和.m
#import "HZAbstractTV.h"
@interface HZKJTV : HZAbstractTV
@end
#import "HZKJTV.h"
@implementation HZKJTV
- (void)receiveCommand:(NSString *)command {
// 打印
NSLog(@"康佳电视接受到了指令-----:%@", command);
}
@end
3.-------------------------视图控制器的代码调用:
#import "ViewController.h"
#import "HZRemote.h"
#import "HZXiaoMiTV.h"
#import "HZKJTV.h"
@interface ViewController ()
@property (nonatomic, strong) HZRemote *remote;
@property (nonatomic, strong) HZXiaoMiTV *xiaomiTV;
@property (nonatomic, strong) HZKJTV *kjTV;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//0.1
UIButton *remoteBtn1 = [[UIButton alloc] initWithFrame:CGRectMake(100, 50, 100, 50)];
[remoteBtn1 setTitle:@"遥控小米" forState:UIControlStateNormal];
[remoteBtn1 addTarget:self action:@selector(remoteBtnClick1:) forControlEvents:UIControlEventTouchUpInside];
remoteBtn1.backgroundColor = [UIColor redColor];
[self.view addSubview:remoteBtn1];
//0.2
UIButton *remoteBtn2 = [[UIButton alloc] initWithFrame:CGRectMake(100, 300, 100, 50)];
[remoteBtn2 setTitle:@"遥控康佳" forState:UIControlStateNormal];
[remoteBtn2 addTarget:self action:@selector(remoteBtnClick2:) forControlEvents:UIControlEventTouchUpInside];
remoteBtn2.backgroundColor = [UIColor redColor];
[self.view addSubview:remoteBtn2];
//1.
HZRemote *remote = [HZRemote new];
self.remote = remote;
HZXiaoMiTV *xiaomiTV = [HZXiaoMiTV new];
self.xiaomiTV = xiaomiTV;
HZKJTV *kjTV = [HZKJTV new];
self.kjTV = kjTV;
}
- (void)remoteBtnClick1:(UIButton *)remoteBtn1 {
//.控制小米TV
self.remote.TV = self.xiaomiTV;
[self.remote down];
}
- (void)remoteBtnClick2:(UIButton *)remoteBtn2 {
//.控制康佳TV
self.remote.TV = self.kjTV;
[self.remote up];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end