桥接模式

  • 桥接模式也叫桥梁模式,将实现与抽象放在两个不同的层次中,使得两者可以独立地变化。(最主要的将实现和抽象两个层次划分开来)
  • 常见场景:JDBC驱动程序、银行转账系统(转账分类和转账用户类型)、消息管理(消息类型、消息分类)

代码实现

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// 实现接口角色
interface Implementor {
doSomething() : void;
doAnything() : void;
}

// 具体实现角色
class ConcreteImplementor1 implements Implementor {
public doSomething() : void {

}
public doAnything() : void {

}
}
class ConcreteImplementor2 implements Implementor {
public doSomething() : void {

}
public doAnything() : void {

}
}

// 抽象类
abstract class Abstraction {
private imp : Implementor;
constructor(imp : Implementor) {
this.imp = imp;
}

// 自身的行为和属性
public request() : void {
this.imp.doSomething();
}
}
// 具体抽象化角色
class RefinedAbstraction extends Abstraction {
constructor(imp : Implementor) {
super(imp);
}

public request() : void {
// 自己写一些处理业务
super.request();
}
}

// 调用
// 定义一个实现化角色
const imp : Implementor = new ConcreteImplementor1();
// 定义一个抽象化角色
const abs : Abstraction = new RefinedAbstraction(imp);
// 执行上下文
abs.request();

参考链接