橋接模式(Bridge),將抽象部分與它的實現(xiàn)部分分離,使它們都可以獨立地變化。
主方法
public class main {
public static void main(String[] args) {
Abstraction ab = new RefinedAbstraction();
ab.setImplementor(new ConcreteImplementorA());
ab.Operation();
ab.setImplementor(new ConcreteImplementorB());
ab.Operation();
}
}
操作功能抽象類
/**
* 功能實現(xiàn)類
*/
public abstract class Implementor {
public abstract void operation();
}
操作派生類
/**
* 派生類A
*/
public class ConcreteImplementorA extends Implementor {
public void operation() {
System.out.println("具體實現(xiàn)A的方法執(zhí)行");
}
}
/**
* 派生類B
*/
public class ConcreteImplementorB extends Implementor {
public void operation() {
System.out.println("具體實現(xiàn)B的方法執(zhí)行");
}
}
抽象類
/**
* 抽象類
*/
public class Abstraction {
protected Implementor implementor;
public Implementor getImplementor() {
return implementor;
}
public void setImplementor(Implementor implementor) {
this.implementor = implementor;
}
public void Operation() {
implementor.operation();
}
}
被提煉的抽象類
public class RefinedAbstraction extends Abstraction {
public void Operation() {
implementor.operation();
}
}