Java结构型设计模式之桥接模式详细讲解

作者:丨Jack_Chen丨 时间:2023-08-26 16:25:56 

桥接模式

概述

桥接模式(Bridge Pattern)也称为桥梁模式、接口(Interfce)模式或柄体(Handle and Body)模式,属于结构型模式。

它是将抽象部分与它的具体实现部分分离,使它们都可以独立地变化。

桥接模式主要目的是通过组合的方式建立两个类之间的联系,而不是继承。桥接模式的核心在于解耦抽象和实现。

Java结构型设计模式之桥接模式详细讲解

应用场景

当一个类内部具备两种或多种变化维度时,使用桥接模式可以解耦这些变化的维度,使高层代码架构隐定。

适用业务场景:

1.在抽象和具体实现之间需要增加更多的灵活性的场景。

2.一个类存在两个(或多个)独立变化的维度,而这两个(或多个)维度都需要独立进行扩展。

3.不希望使用继承,或因为多层继承导致系统类的个数剧增。

优缺点

优点:

1.分离抽象部分及其具体实现部分

2.提高了系统的扩展性

3.实现细节对客户透明

缺点:

1.增加了系统的理解与设计难度

2.需要正确地识别系统中两个独立变化的维度

主要角色

1.抽象(Abstraction)

该类持有一个对实现角色的引用,抽象角色中的方法需要实现角色来实现。抽象角色一般为抽象类(构造函数规定子类要传入一个实现对象)。

2.修正抽象(RefinedAbstraction)

抽象的具体实现,对抽象的方法进行完善和扩展。

3.实现(Implementor)

确定实现维度的基本操作,提供给抽象使用。该类一般为接口或抽象类。

1.具体实现(Concretelmplementor)

实现的具体实现。

Java结构型设计模式之桥接模式详细讲解

桥接模式的基本使用

创建实现角色

public interface IImplementor {
   void operationImpl();
}

创建具体实现角色

public class ConcreteImplementorA implements IImplementor {
   public void operationImpl() {
       System.out.println("ConcreteImplementorA operationImpl");
   }
}
public class ConcreteImplementorB implements IImplementor {
   public void operationImpl() {
       System.out.println("ConcreteImplementorB operationImpl");
   }
}

创建抽象角色

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

创建修正抽象角色

public class RefinedAbstraction extends Abstraction {
   public RefinedAbstraction(IImplementor implementor) {
       super(implementor);
   }
   @Override
   public void operation() {
       super.operation();
       System.out.println("RefinedAbstraction operation");
   }
}

客户端调用

public static void main(String[] args) {
       // 来一个实现化角色
       IImplementor impA = new ConcreteImplementorA();
       IImplementor impB = new ConcreteImplementorB();
       // 来一个抽象化角色,聚合实现
       Abstraction absA = new RefinedAbstraction(impA);
       Abstraction absB = new RefinedAbstraction(impB);
       // 执行操作
       absA.operation();
       absB.operation();
   }

ConcreteImplementorA operationImpl
RefinedAbstraction operation
ConcreteImplementorB operationImpl
RefinedAbstraction operation

桥接模式实现消息发送

使用桥接模式解耦消息类型与消息重要程度。

创建实现角色

创建实现角色,担任桥接角色,实现消息发送的统一接口

public interface IMessage {
   void send(String message);
}

创建具体实现角色

public class EmailMessage implements IMessage {
   @Override
   public void send(String message) {
       System.out.println("使用邮件消息发送" + message);
   }
}
public class SmsMessage implements IMessage {
   @Override
   public void send(String message) {
       System.out.println("使用短信消息发送" + message);
   }
}

创建抽象角色

创建抽象角色,担任桥接抽象角色,持有实现角色,且发送消息委派给实现对象发送

public abstract class AbastractMessage {
   // 持有实现角色的引用
   private IMessage message;
   // 构造函数,传入实现角色的引用
   public AbastractMessage(IMessage message) {
       this.message = message;
   }
   // 发送消息的方法,调用实现角色的方法
   void sendMessage(String message){
       this.message.send(message);
   }
}

创建修正抽象角色

创建普通消息

public class NomalMessage extends AbastractMessage {
   public NomalMessage(IMessage message) {
       super(message);
   }
}

创建重要消息

public class ImportanceMessage extends AbastractMessage {
   public ImportanceMessage(IMessage message) {
   message = message + "【重要消息】";
       super(message);
   }
   void sendMessage(String message){
       super.sendMessage(message);
   }
}

客户端调用

public static void main(String[] args) {
       IMessage message = new EmailMessage();
       AbastractMessage abastractMessage = new NomalMessage(message);
       abastractMessage.sendMessage("hello world");
       message = new SmsMessage();
       abastractMessage = new ImportanceMessage(message);
       abastractMessage.sendMessage("hello world");
   }

使用邮件消息发送hello world
使用短信消息发送hello world【重要消息】

来源:https://blog.csdn.net/qq_38628046/article/details/126194229

标签:Java,桥接模式,设计模式
0
投稿

猜你喜欢

  • Java实现简单台球游戏

    2022-06-28 23:55:59
  • Android编程简易实现XML解析的方法详解

    2023-12-25 18:51:59
  • Springboot mybatis plus druid多数据源解决方案 dynamic-datasource的使用详解

    2021-08-01 19:27:32
  • Java程序员必须知道的5个JVM命令行标志

    2023-11-11 15:30:36
  • SpringBoot使用SchedulingConfigurer实现多个定时任务多机器部署问题(推荐)

    2021-09-17 07:19:20
  • 对int array进行排序的实例讲解

    2021-12-09 06:51:15
  • SpringBoot+easypoi实现数据的Excel导出

    2023-04-05 12:27:19
  • javaweb页面附件、图片下载及打开(实现方法)

    2023-11-25 05:33:44
  • java 中设计模式(值对象)的实例详解

    2021-12-27 14:15:08
  • Java读写Windows共享文件夹的方法实例

    2022-10-02 02:25:45
  • Android EventBus 3.0.0 使用总结(必看篇)

    2023-09-06 06:32:41
  • Java用邻接矩阵存储图的示例代码

    2021-10-05 21:39:18
  • c#利用Excel直接读取数据到DataGridView

    2023-02-22 13:39:07
  • C#推送信息到APNs的方法

    2023-05-29 05:20:59
  • Java使用Lettuce客户端在Redis在主从复制模式下命令执行的操作

    2023-11-28 21:38:19
  • java中多个@Scheduled定时器不执行的解决方法

    2023-03-19 02:32:34
  • Mybatis批量插入index out of range错误的解决(较偏的错误)

    2022-06-11 01:11:51
  • C#串口通信实现方法

    2023-04-15 06:31:11
  • 浅析Java中接口和抽象类的七大区别

    2022-01-16 21:09:36
  • Intellij IDEA + Android SDK + Genymotion Emulator打造最佳Android开发环境

    2023-06-17 06:47:11
  • asp之家 软件编程 m.aspxhome.com