Contents
  1. 1. 桥接模式、组合不同抽象类型的具体实现类的对象,个人觉得和组合模式和策略模式差不多,就是使用抽象类型来解耦具体类的关联.
  2. 2. 模拟 PC组合CPU ,使用 桥接模式 实现.
  3. 3. CPU类型的抽象
  4. 4. ADM CPU
  5. 5. Intel CPU
  6. 6. 抽象PC类,提供基础性公共代码
  7. 7. LenevoComputer
  8. 8. ISWComputer
  9. 9. demo示例

桥接模式、组合不同抽象类型的具体实现类的对象,个人觉得和组合模式策略模式差不多,就是使用抽象类型来解耦具体类的关联.


模拟 PC组合CPU ,使用 桥接模式 实现.


CPU类型的抽象

1
2
3
4
5
6
7
8
9
10
11
#import <Foundation/Foundation.h>

/**
* CPU类型的抽象
*/
@protocol CpuAbility <NSObject>

/** CPU的效率 */
- (NSString *)abilityCpu;

@end

ADM CPU

1
2
3
4
5
6
7
8
#import <Foundation/Foundation.h>

//实现CPU剪口
#import "CpuAbility.h"

@interface ADMCpu : NSObject <CpuAbility>

@end
1
2
3
4
5
6
7
8
9
#import "ADMCpu.h"

@implementation ADMCpu

- (NSString *)abilityCpu {
return @"ADM CPU 跑分 999分";
}

@end

Intel CPU

1
2
3
4
5
6
7
8
#import <Foundation/Foundation.h>

//实现CPU剪口
#import "CpuAbility.h"

@interface IntelCpu : NSObject <CpuAbility>

@end
1
2
3
4
5
6
7
8
9
#import "IntelCpu.h"

@implementation IntelCpu

- (NSString *)abilityCpu {
return @"Intel CPU 跑分 99999999分";
}

@end

抽象PC类,提供基础性公共代码

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
#import <Foundation/Foundation.h>

//PC包含一个CPU
#import "CpuAbility.h"

/**
* PC的抽象模板类
*/
@interface AbstractComputer : NSObject

/**
* 组合一个CPU对象
*/
@property (nonatomic, strong) id<CpuAbility> cpu;

/**
* 需要一个CPU对象
*/
- (instancetype)initWithCPU:(id<CpuAbility>)cpu;

/**
* CPU跑分
*/
- (void)checkPcAbility;

@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#import "AbstractComputer.h"

@implementation AbstractComputer

- (instancetype)initWithCPU:(id<CpuAbility>)cpu {
self = [super init];
if (self) {
_cpu = cpu;
}
return self;
}

- (void)checkPcAbility {//子类实现}

@end

LenevoComputer

1
2
3
4
5
6
7
8
#import "AbstractComputer.h"

/**
* 联想PC
*/
@interface LenevoComputer : AbstractComputer

@end
1
2
3
4
5
6
7
8
9
10
11
12
13
#import "LenevoComputer.h"

@implementation LenevoComputer

////////////////////////////////////////////////////////////
//////重写抽象方法
////////////////////////////////////////////////////////////

- (void)checkPcAbility {
NSLog(@"Lenevo Computer CPU 跑分: %@", [self.cpu abilityCpu]);
}

@end

ISWComputer

1
2
3
4
5
6
7
8
#import "AbstractComputer.h"

/**
* IBM PC
*/
@interface ISWComputer : AbstractComputer

@end
1
2
3
4
5
6
7
8
9
10
11
12
13
#import "ISWComputer.h"

@implementation ISWComputer

////////////////////////////////////////////////////////////
//////重写抽象方法
////////////////////////////////////////////////////////////

- (void)checkPcAbility {
NSLog(@"IBM Computer CPU跑分: %@", [self.cpu abilityCpu]);
}

@end

demo示例

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
33
34
#import "BridgeTest.h"

//所有PC
#import "LenevoComputer.h"
#import "ISWComputer.h"

//所有CPU
#import "ADMCpu.h"
#import "IntelCpu.h"

@implementation BridgeTest

- (void)test {

////////////////////////////////////////////////////////////
//////Intel CPU + Lenevo Computer
////////////////////////////////////////////////////////////
id<CpuAbility> intelCPU = [IntelCpu new];
LenevoComputer *pc1 = [[LenevoComputer alloc] initWithCPU:intelCPU];

////////////////////////////////////////////////////////////
//////ADM CPU + ISW Computer
////////////////////////////////////////////////////////////
id<CpuAbility> admCPU = [ADMCpu new];
ISWComputer *pc2 = [[ISWComputer alloc] initWithCPU:admCPU];

////////////////////////////////////////////////////////////
//////PC1 与 PC2 各自的跑分测试
////////////////////////////////////////////////////////////
[pc1 checkPcAbility];
[pc2 checkPcAbility];
}

@end
Contents
  1. 1. 桥接模式、组合不同抽象类型的具体实现类的对象,个人觉得和组合模式和策略模式差不多,就是使用抽象类型来解耦具体类的关联.
  2. 2. 模拟 PC组合CPU ,使用 桥接模式 实现.
  3. 3. CPU类型的抽象
  4. 4. ADM CPU
  5. 5. Intel CPU
  6. 6. 抽象PC类,提供基础性公共代码
  7. 7. LenevoComputer
  8. 8. ISWComputer
  9. 9. demo示例