当前位置: 首页 > news >正文

【设计模式】JAVA Design Patterns——Bridge(桥接模式)

🔍目的


将抽象与其实现分离,以便二者可以独立变化。

🔍解释


真实世界例子

考虑一下你拥有一种具有不同附魔的武器,并且应该允许将具有不同附魔的不同武器混合使用。 你会怎么做? 为每个附魔创建每种武器的多个副本,还是只是创建单独的附魔并根据需要为武器设置它? 桥接模式使您可以进行第二次操作

通俗描述

桥接模式是一个更推荐组合而不是继承的模式。将实现细节从一个层次结构推送到具有单独层次结构的另一个对象。

维基百科

桥接模式是软件工程中使用的一种设计模式,旨在“将抽象与其实现分离,从而使两者可以独立变化”

程序示例

创建一个武器类层级

public interface Weapon {void wield();void swing();void unwield();Enchantment getEnchantment();
}public class Sword implements Weapon {private final Enchantment enchantment;public Sword(Enchantment enchantment) {this.enchantment = enchantment;}@Overridepublic void wield() {LOGGER.info("The sword is wielded.");enchantment.onActivate();}@Overridepublic void swing() {LOGGER.info("The sword is swinged.");enchantment.apply();}@Overridepublic void unwield() {LOGGER.info("The sword is unwielded.");enchantment.onDeactivate();}@Overridepublic Enchantment getEnchantment() {return enchantment;}
}public class Hammer implements Weapon {private final Enchantment enchantment;public Hammer(Enchantment enchantment) {this.enchantment = enchantment;}@Overridepublic void wield() {LOGGER.info("The hammer is wielded.");enchantment.onActivate();}@Overridepublic void swing() {LOGGER.info("The hammer is swinged.");enchantment.apply();}@Overridepublic void unwield() {LOGGER.info("The hammer is unwielded.");enchantment.onDeactivate();}@Overridepublic Enchantment getEnchantment() {return enchantment;}
}

创建单独的附魔类

public interface Enchantment {void onActivate();void apply();void onDeactivate();
}public class FlyingEnchantment implements Enchantment {@Overridepublic void onActivate() {LOGGER.info("The item begins to glow faintly.");}@Overridepublic void apply() {LOGGER.info("The item flies and strikes the enemies finally returning to owner's hand.");}@Overridepublic void onDeactivate() {LOGGER.info("The item's glow fades.");}
}public class SoulEatingEnchantment implements Enchantment {@Overridepublic void onActivate() {LOGGER.info("The item spreads bloodlust.");}@Overridepublic void apply() {LOGGER.info("The item eats the soul of enemies.");}@Overridepublic void onDeactivate() {LOGGER.info("Bloodlust slowly disappears.");}
}

原神启动!

var enchantedSword = new Sword(new SoulEatingEnchantment());
enchantedSword.wield();
enchantedSword.swing();
enchantedSword.unwield();
// The sword is wielded.
// The item spreads bloodlust.
// The sword is swinged.
// The item eats the soul of enemies.
// The sword is unwielded.
// Bloodlust slowly disappears.var hammer = new Hammer(new FlyingEnchantment());
hammer.wield();
hammer.swing();
hammer.unwield();
// The hammer is wielded.
// The item begins to glow faintly.
// The hammer is swinged.
// The item flies and strikes the enemies finally returning to owner's hand.
// The hammer is unwielded.
// The item's glow fades.

🔍类图

Bridge class diagram 

🔍适用场景

使用桥接模式当

  • 你想永久性的避免抽象和他的实现之间的绑定。有可能是这种情况,当实现需要被选择或者在运行时切换。
  • 抽象和他们的实现应该能通过写子类来扩展。这种情况下,桥接模式让你可以组合不同的抽象和实现并独立的扩展他们。
  • 对抽象的实现的改动应当不会对客户产生影响;也就是说,他们的代码不必重新编译。
  • 你有种类繁多的类。这样的类层次结构表明需要将一个对象分为两部分。Rumbaugh 使用术语“嵌套归纳”来指代这种类层次结构。
  • 你想在多个对象间分享一种实现(可能使用引用计数),这个事实应该对客户隐藏。一个简单的示例是Coplien的String类,其中多个对象可以共享同一字符串表示形式


相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 面试被问到不懂的东西,是直接说不懂还是坚持狡辩一下?
  • test_mqtt/python
  • 帝国CMS如何修改时间格式,变成几分钟,几小时教程
  • 常用 CSS 写法
  • 苹果MacOS系统使用微软远程桌面连接Windows电脑桌面详细步骤
  • Mac配置node环境
  • etcd 和 MongoDB 的混沌(故障注入)测试方法
  • 数据网络理论基础 第三章网络的时延模型
  • [力扣题解] 841. 钥匙和房间
  • 深入探讨 Java 8 集合操作:全面解析 Stream API 的强大功能
  • STM32_HAL__TIM_输出比较
  • C语言----判断n是否是2的次方数,利用到按位与,算法n(n-1)
  • 在linux中配置关于GFS创建各种卷以及卷组--配置实验
  • Anti Desgin Vue 实现 表格可编辑、新增、删除功能
  • 前端开发工程师——webpack
  • 【译】JS基础算法脚本:字符串结尾
  • 【node学习】协程
  • github指令
  • Objective-C 中关联引用的概念
  • PAT A1050
  • php ci框架整合银盛支付
  • Phpstorm怎样批量删除空行?
  • vue自定义指令实现v-tap插件
  • 搞机器学习要哪些技能
  • 解决iview多表头动态更改列元素发生的错误
  • 开放才能进步!Angular和Wijmo一起走过的日子
  • 巧用 TypeScript (一)
  • 数组大概知多少
  • 算法系列——算法入门之递归分而治之思想的实现
  • 微信小程序开发问题汇总
  • 我这样减少了26.5M Java内存!
  • 物联网链路协议
  • media数据库操作,可以进行增删改查,实现回收站,隐私照片功能 SharedPreferences存储地址:
  • 阿里云ACE认证学习知识点梳理
  • ​低代码平台的核心价值与优势
  • ​渐进式Web应用PWA的未来
  • ​力扣解法汇总1802. 有界数组中指定下标处的最大值
  • ​数据结构之初始二叉树(3)
  • #14vue3生成表单并跳转到外部地址的方式
  • #我与Java虚拟机的故事#连载13:有这本书就够了
  • $.ajax()
  • (0)Nginx 功能特性
  • (20)目标检测算法之YOLOv5计算预选框、详解anchor计算
  • (4) openssl rsa/pkey(查看私钥、从私钥中提取公钥、查看公钥)
  • (C++17) std算法之执行策略 execution
  • (板子)A* astar算法,AcWing第k短路+八数码 带注释
  • (第9篇)大数据的的超级应用——数据挖掘-推荐系统
  • (附源码)node.js知识分享网站 毕业设计 202038
  • (附源码)ssm高校升本考试管理系统 毕业设计 201631
  • (九)c52学习之旅-定时器
  • (全部习题答案)研究生英语读写教程基础级教师用书PDF|| 研究生英语读写教程提高级教师用书PDF
  • (入门自用)--C++--抽象类--多态原理--虚表--1020
  • (译) 理解 Elixir 中的宏 Macro, 第四部分:深入化
  • (转)memcache、redis缓存
  • 、写入Shellcode到注册表上线