美文网首页
桥接模式

桥接模式

作者: jacky123 | 来源:发表于2016-06-12 19:34 被阅读24次

桥接模式(Bridge Pattern):将抽象部分与它的实现部分分离,使它们都可以独立地变化。它是一种对象结构型模式,又称为柄体(Handle and Body)模式或接口(Interface)模式。设计模式

UML类图

  • Abstraction:抽象类
  • RefinedAbstraction:扩充抽象类
  • Implementor:实现类接口
  • ConcreteImplementor:具体实现类

示例

大家去咖啡馆喝咖啡一般分为4中。大杯加糖、大杯不加糖、小杯加糖、小杯不加糖。

1. 定义抽象类Coffee(抽象类)

public abstract class Coffee {
    //两个抽象类连接的唯一纽带
    protected CoffeeAdditives impl;

    public Coffee(CoffeeAdditives impl) {
        this.impl = impl;
    }
    
    /**
     * 咖啡具体是什么样子由子类决定
     */
    public abstract void makeCoffee();
}

2. 定义抽象类CoffeeAdditives(扩充抽象类)

public abstract class CoffeeAdditives {
    /**
     * 具体要加什么,由子类实现
     * @return
     */
    public abstract String addSomething();
}

3.Coffee的具体实现(抽象类实现)

public class LargeCoffee extends Coffee {

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

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

}

public class SmallCoffee extends Coffee {

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

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

}

4.CoffeeAdditives的具体是实现(扩充抽象类实现)

public class Ordinary extends CoffeeAdditives {

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

}

public class Sugar extends CoffeeAdditives {

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

}

5.使用

public class Client {
    public static void main(String[] args) {
        //咖啡
        Coffee coffee;
        
        //加糖
        CoffeeAdditives sugar = new Sugar();
        //原味
        CoffeeAdditives ordinary = new Ordinary();
        
        /**
         * 大杯咖啡,加糖
         */
        coffee = new LargeCoffee(sugar);
        coffee.makeCoffee();
        
        /**
         * 大杯咖啡,原味
         */
        coffee = new LargeCoffee(ordinary);
        coffee.makeCoffee();
        
        /**
         * 小杯咖啡,加糖
         */
        coffee = new SmallCoffee(sugar);
        coffee.makeCoffee();
        
        /**
         * 小杯咖啡,原味
         */
        coffee = new SmallCoffee(ordinary);
        coffee.makeCoffee();
    }
}

结果:

大杯的加糖咖啡
大杯的原味咖啡
小杯的加糖咖啡
小杯的原味咖啡

当我们以后需求变化时,比如增加了咖啡口味的种类,只要继续实现相应的 CoffeeAdditives类,如加奶、加蜂蜜、加盐等。
不管Coffee变化还是CoffeeAdditives变化了,其相对与对方来说都是独立的,两者并没有过多的交集,两者唯一的联系就是 Coffee保持了对CoffeeAdditives的引用,此乃两者之纽带,这就是桥接模式。

相关文章

  • 设计模式-桥接模式

    设计模式-桥接模式 定义 桥接模式(Bridge Pattern)也称为桥梁模式、接口(Interface)模式或...

  • 结构型模式:桥接模式

    文章首发:结构型模式:桥接模式 七大结构型模式之二:桥接模式。 简介 姓名 :桥接模式 英文名 :Bridge P...

  • 设计模式之桥接模式

    设计模式之桥接模式 1. 模式定义 桥接模式又称柄体模式或接口模式,它是一种结构性模式。桥接模式将抽象部分与实现部...

  • 06-01-001 虚拟机的网络连接方式(转运整理)

    一、Bridged(桥接模式) 什么是桥接模式?桥接模式就是将主机网卡与虚拟机虚拟的网卡利用虚拟网桥进行通信。在桥...

  • 桥接模式与中介模式

    桥接模式-BRIDGE 对桥接模式感兴趣,是因为公司业务上需要桥接Html5和ReactNative两个平台。桥接...

  • 设计模式——桥接模式

    设计模式——桥接模式 最近公司组件分享设计模式,然而分配给我的是桥接模式。就在这里记录我对桥接模式的理解吧。 定义...

  • 桥接模式

    个人博客http://www.milovetingting.cn 桥接模式 模式介绍 桥接模式也称为桥梁模式,是结...

  • 桥接模式

    桥接模式 参考原文: https://zhuanlan.zhihu.com/p/62390221 定义 桥接模式 ...

  • 10-桥接模式

    桥接模式-Bridge Pattern【学习难度:★★★☆☆,使用频率:★★★☆☆】 处理多维度变化——桥接模式(...

  • Java设计模式——桥接模式

    Java设计模式之桥接模式 回顾 上一期分享了适配器模式,主要为了实现解耦 桥接模式 简介 桥接模式是对象的结构模...

网友评论

      本文标题:桥接模式

      本文链接:https://www.haomeiwen.com/subject/rzoedttx.html