标签:http The 特点 imp dos 客户 函数 str 避免
桥梁模式也称为桥接模式,是结构型设计模式之一。将抽象和实现解耦,使得两者可以独立地变化
12345
public interface Implementor { //基本方法 public void doSomething(); public void doAnything();}
1234567891011
public class ConcreteImplementor1 implements Implementor { @Override public void doSomething() { System.out.println("ConcreteImplementor1 doSomething!"); } @Override public void doAnything() { System.out.println("ConcreteImplementor1 doAnything!"); }}
public class ConcreteImplementor2 implements Implementor { @Override public void doSomething() { System.out.println("ConcreteImplementor2 doSomething!"); } @Override public void doAnything() { System.out.println("ConcreteImplementor2 doAnything!"); }}
123456789101112131415161718
public abstract class Abstraction { //定义对实现化角色的引用 private Implementor imp; //约束子类必须实现该构造函数 public Abstraction(Implementor imp) { this.imp = imp; } //自身的行为和属性 public void request() { this.imp.doSomething(); } public Implementor getImp() { return imp; }}
123456789101112131415
public class RefineAbstraction extends Abstraction { public RefineAbstraction(Implementor imp) { super(imp); } @Override public void request() { /** * 业务处理 */ super.request(); super.getImp().doAnything(); }}
public class Test { public static void main(String args[]) { Implementor imp = new ConcreteImplementor1(); Abstraction abstraction = new RefineAbstraction(imp); abstraction.request(); Implementor imp2 = new ConcreteImplementor2(); abstraction = new RefineAbstraction(imp2); abstraction.request(); }}
原文:大专栏 设计模式-桥梁模式
设计模式-桥梁模式
原文地址:https://www.cnblogs.com/petewell/p/11612050.html