桥接模式
UML
解释说明
桥接模式 :将抽象部分与它的实现部分分离,使它们都可以独立的变化。
代码实现
package learn18;
public abstract class Implementor {
public abstract void operation ( ) ;
}
ConcreteImplementorA.class
package learn18;
public class ConcreteImplementorA extends Implementor {
@Override
public void operation ( ) {
System. out. println ( "执行方法A!" ) ;
}
}
ConcreteImplementorB.class
package learn18;
public class ConcreteImplementorB extends Implementor {
@Override
public void operation ( ) {
System. out. println ( "执行方法B!" ) ;
}
}
package learn18;
public abstract class Abstraction {
protected Implementor implementor;
public void setImplementor ( Implementor implementor) {
this . implementor = implementor;
}
public abstract void option ( ) ;
}
package learn18;
public class RefinedAbstraction extends Abstraction {
@Override
public void option ( ) {
implementor. operation ( ) ;
}
}
import learn18. *;
public class Main {
public static void main ( String[ ] args) throws Exception {
Abstraction a = new RefinedAbstraction ( ) ;
a. setImplementor ( new ConcreteImplementorA ( ) ) ;
a. option ( ) ;
Abstraction b = new RefinedAbstraction ( ) ;
b. setImplementor ( new ConcreteImplementorB ( ) ) ;
b. option ( ) ;
}
}
参考资料