桥接模式

双重扩展

场景


本身抽象类可以继承扩展,同时传入另一个接口体系
抽象类和接口独立扩展,互不影响

代码


Sample
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
public interface Target {
void action();
}

public abstract Controller {
private Target target;

public Controller(Targer target) {
this.target = target;
}

public control() {
target.action();
}
}

public class Product implements Target {
@Override
public void action() {
//action
}
}

public class ProductController extends Controller {
@Override
public control() {
super.control();
//other control logic
}
}

调用方代码

1
2
3
Target target = new Product();
Controller controller = new ProductController(target);
controller.control();

特征


抽象类内部包含接口,构造传入