设计模式 之 桥接模式

本质上还是组合

package com.test.pattern.bridge;

//实现接口
interface Implementor {
    public void operationImpl();
}

abstract class Abstraction {
    protected Implementor implementor;
    
    public Abstraction(Implementor implementor) {
        this.implementor = implementor;
    }
    
    public void operation() {
        implementor.operationImpl();
    }
}

class ConcreteImplementorA implements Implementor {

    public void operationImpl() {
        System.out.println("具体实现A");
    }
}

class ConcreteImplementorB implements Implementor {

    public void operationImpl() {
        System.out.println("具体实现B");
    }
}

class RefinedAbstraction extends Abstraction{

    public RefinedAbstraction(Implementor implementor) {
        super(implementor);
    }
    
    public void otherOperation() {
        System.out.println("其他操作");
    }
}

public class Test {
    public static void main(String[] args) {
        Implementor implementor = new ConcreteImplementorA();
        RefinedAbstraction abstraction = new RefinedAbstraction(implementor);
        abstraction.operation();
        abstraction.otherOperation();
    }
}
原文地址:https://www.cnblogs.com/heben/p/5784316.html