1.桥接模式介绍
- 优点:
- 抽象部分和实现部分的解耦(分离),他们两个互相独立,不会影响到对方。
- 优秀的扩展能力。
- 实现细节对客户透明。
- 缺点: 桥接模式的引入会增加系统的理解与设计难度,由于聚合关联关系建立在抽象层,要求开发者针对抽象进行设计与编程。
- 本质: 就是2个变量(对象)的排列组合
- 使用场景: 对于两个独立变化的维度,使用桥接模式再适合不过了。
2.桥接模式的简单使用

public abstract class PhoneBridge {
protected Style style;
protected String brand;
public abstract void make();
}
public class ApplePhone extends PhoneBridge{
public ApplePhone(Style style) {
this.style = style;
this.brand = "苹果";
}
@Override
public void make() {
System.out.println("生产=>"+this.brand+this.style.type()+"手机");
}
}
public class HuaWeiPhone extends PhoneBridge{
public HuaWeiPhone(Style style) {
this.style = style;
this.brand = "华为";
}
@Override
public void make() {
System.out.println("生产=>"+this.brand+this.style.type()+"手机");
}
}
public interface Style{
String type();
}
public class FoldingStyle implements Style{
@Override
public String type() {
return "折叠式";
}
}
public class UprightStyle implements Style{
@Override
public String type() {
return "直立式";
}
}
public class RotatingStyle implements Style{
@Override
public String type() {
return "旋转式";
}
}
public class Test{
public static void main(String[] args) {
ApplePhone appleFolding = new ApplePhone(new FoldingStyle());
appleFolding.make();
ApplePhone appleRotating = new ApplePhone(new RotatingStyle());
appleRotating.make();
HuaWeiPhone huaWeiUpright = new HuaWeiPhone(new UprightStyle());
huaWeiUpright.make();
}
}
生产=>苹果折叠式手机
生产=>苹果旋转式手机
生产=>华为直立式手机