桥梁模式

  1. 实现化角色抽象类或接口
public interface Implementor {
    void doSomething();
    void doAnything();
}
  1. 实现化角色抽象类或接口实现类
public class ConcreteImplementor implements Implementor {
    @Override
    public void doSomething() {
        //具体逻辑
    }
    @Override
    public void doAnything() {
        //具体逻辑
    }
}
  1. 抽象化角色抽象类
public abstract class Abstraction {
    //定义实现化角色
    private Implementor implementor;

    public Abstraction(Implementor implementor) {
        this.implementor = implementor;
    }
    //执行实现化角色方法
    public void request(){
        this.implementor.doSomething();
    }
    public Implementor getImplementor(){
        return this.implementor;
    }
}
  1. 具体抽象化角色
public class RefinedAbstraction extends Abstraction {
    public RefinedAbstraction(Implementor implementor) {
        super(implementor);
    }
    //修正父类方法
    @Override
    public void request() {
        super.request();
        super.getImplementor().doAnything();
    }
}
  1. 场景使用
    Implementor implementor = new ConcreteImplementor();
    Abstraction abstraction = new RefinedAbstraction(implementor);
    abstraction.request();

你可能感兴趣的:(桥梁模式)