桥接模式 把事物和具体的实现分开 将抽象与实现化解耦 使得二者可以独立变化 对于很多统一对外的接口 习惯上会多封装一层便于理清抽象逻辑
public class Bridge {
private Sourceable sourceable;
public void method() {
sourceable.method();
}
public Sourceable getSourceable() {
return sourceable;
}
public void setSourceable(Sourceable sourceable) {
this.sourceable = sourceable;
}
}
interface Sourceable {
void method();
}
class SourceSub1 implements Sourceable {
@Override
public void method() {
System.out.println("this is the first method");
}
}
class SourceSub2 implements Sourceable {
@Override
public void method() {
System.out.println("this is the second method");
}
}
class Mybridge extends Bridge{
@Override
public void method(){
getSourceable().method();
}
}
class Test{
public static void main(String[]args){
Mybridge mybridge = new Mybridge();
mybridge.setSourceable(new SourceSub1());
mybridge.method();
mybridge.setSourceable(new SourceSub2());
mybridge.method();
}
}
输出的结果为: