桥接模式

1类图

这里写图片描述

2定义

将抽象部分与实现部分分离,使他们都可以独立地进行变化。

3应用场景

3.1 如果一个系统需要在构建的抽象化角色和具体化角色之间增加更多的灵活性,避免这两个层次之间建立静态的继承联系,可以通过桥接模式使它们在抽象层建立一个关联关系。

通用模板

4实现部分的抽象接口

public interface Implementor {

    public void operationImpl();

}

5实现部分具体的实现一

public class ConcreteImplementorA implements Implementor{

    @Override
    public void operationImpl() {
        System.out.println("ConcreteImplementorA");
    }

}

6实现部分具体的实现二

public class ConcreteImplementorB implements Implementor{

    @Override
    public void operationImpl() {
        System.out.println("ConcreteImplementorB");
    }

}

7抽象部分实现

public abstract class Abstraction {

    private Implementor mImplementor;

    public Abstraction(Implementor implementor) {
        mImplementor = implementor;
    }

    public void operation(){
        mImplementor.operationImpl();
    }

}

8抽象部分的子类

public class RefinedAbstraction extends Abstraction {

    private Implementor mImplementor;

    public RefinedAbstraction(Implementor implementor) {
        super(implementor);
        mImplementor = implementor;
    }

    public void refinedOperation(){
        System.out.println("RefinedAbstraction ");
        mImplementor.operationImpl();
    }

}

9客户类

public class Client {

    public static void main(String[] args) {

        Implementor implementorA = new ConcreteImplementorA();
        Implementor implementorB = new ConcreteImplementorB();

        RefinedAbstraction refinedA = new RefinedAbstraction(implementorA);
        refinedA.refinedOperation();
        System.out.println("---------------------");
        RefinedAbstraction refinedB = new RefinedAbstraction(implementorB);
        refinedB.refinedOperation();
    }

}

简单实现

10咖啡类

public abstract class Coffee {

    protected CoffeeAdditives impl;

    public Coffee(CoffeeAdditives impl) {
        this.impl = impl;
    }

    public abstract void makeCoffee();

}

11大杯

public class LargeCoffee extends Coffee {

    public LargeCoffee(CoffeeAdditives impl) {
        super(impl);
    }

    @Override
    public void makeCoffee() {
        System.out.println("大杯的" + impl.addSomething() + "咖啡");
    }

}

12中杯

public class MiddleCoffee extends Coffee {

    public MiddleCoffee(CoffeeAdditives impl) {
        super(impl);
    }

    @Override
    public void makeCoffee() {
        System.out.println("中杯的" + impl.addSomething() + "咖啡");
    }

}

13小杯

public class SmallCoffee extends Coffee {

    public SmallCoffee(CoffeeAdditives impl) {
        super(impl);
    }

    @Override
    public void makeCoffee() {
        System.out.println("小杯的" + impl.addSomething() + "咖啡");
    }

}

14不同味的咖啡抽象类

public abstract class CoffeeAdditives {

    public abstract String addSomething();

}

15加糖

public class Sugar extends CoffeeAdditives {

    @Override
    public String addSomething() {
        return "加糖";
    }
}

16原味

public class Ordinary extends CoffeeAdditives {

    @Override
    public String addSomething() {
        return "原味";
    }

}

17测试类

public class Test {

    public static void main(String[] args) {
        Ordinary implOrdinary = new Ordinary();

        Sugar implSuhar = new Sugar();

        LargeCoffee largeCoffeeOrdinary = new LargeCoffee(implOrdinary);
        largeCoffeeOrdinary.makeCoffee();

        LargeCoffee largeCoffeeSuhar = new LargeCoffee(implSuhar);
        largeCoffeeSuhar.makeCoffee();

        System.out.println("-----------------------------------");

        SmallCoffee smallCoffeeOrdinary = new SmallCoffee(implOrdinary);
        smallCoffeeOrdinary.makeCoffee();

        SmallCoffee smallCoffeeSuhar = new SmallCoffee(implSuhar);
        smallCoffeeSuhar.makeCoffee();

        System.out.println("-----------------------------------");

        MiddleCoffee middleCoffeeOrdinary = new MiddleCoffee(implOrdinary);
        middleCoffeeOrdinary.makeCoffee();

        MiddleCoffee middleCoffeeSuhar = new MiddleCoffee(implSuhar);
        middleCoffeeOrdinary.makeCoffee();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值