Java结构型设计模式之桥接模式详细讲解
作者:丨Jack_Chen丨 时间:2023-08-26 16:25:56
桥接模式
概述
桥接模式(Bridge Pattern)也称为桥梁模式、接口(Interfce)模式或柄体(Handle and Body)模式,属于结构型模式。
它是将抽象部分与它的具体实现部分分离,使它们都可以独立地变化。
桥接模式主要目的是通过组合的方式建立两个类之间的联系,而不是继承。桥接模式的核心在于解耦抽象和实现。
应用场景
当一个类内部具备两种或多种变化维度时,使用桥接模式可以解耦这些变化的维度,使高层代码架构隐定。
适用业务场景:
1.在抽象和具体实现之间需要增加更多的灵活性的场景。
2.一个类存在两个(或多个)独立变化的维度,而这两个(或多个)维度都需要独立进行扩展。
3.不希望使用继承,或因为多层继承导致系统类的个数剧增。
优缺点
优点:
1.分离抽象部分及其具体实现部分
2.提高了系统的扩展性
3.实现细节对客户透明
缺点:
1.增加了系统的理解与设计难度
2.需要正确地识别系统中两个独立变化的维度
主要角色
1.抽象(Abstraction)
该类持有一个对实现角色的引用,抽象角色中的方法需要实现角色来实现。抽象角色一般为抽象类(构造函数规定子类要传入一个实现对象)。
2.修正抽象(RefinedAbstraction)
抽象的具体实现,对抽象的方法进行完善和扩展。
3.实现(Implementor)
确定实现维度的基本操作,提供给抽象使用。该类一般为接口或抽象类。
1.具体实现(Concretelmplementor)
实现的具体实现。
桥接模式的基本使用
创建实现角色
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
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实现简单台球游戏
![](https://img.aspxhome.com/file/2023/1/62371_0s.png)
Android编程简易实现XML解析的方法详解
![](https://img.aspxhome.com/file/2023/3/88663_0s.jpg)
Springboot mybatis plus druid多数据源解决方案 dynamic-datasource的使用详解
Java程序员必须知道的5个JVM命令行标志
SpringBoot使用SchedulingConfigurer实现多个定时任务多机器部署问题(推荐)
![](https://img.aspxhome.com/file/2023/5/103855_0s.png)
对int array进行排序的实例讲解
![](https://img.aspxhome.com/file/2023/1/68101_0s.png)
SpringBoot+easypoi实现数据的Excel导出
javaweb页面附件、图片下载及打开(实现方法)
java 中设计模式(值对象)的实例详解
Java读写Windows共享文件夹的方法实例
Android EventBus 3.0.0 使用总结(必看篇)
![](https://img.aspxhome.com/file/2023/9/129039_0s.jpg)
Java用邻接矩阵存储图的示例代码
![](https://img.aspxhome.com/file/2023/1/64171_0s.png)
c#利用Excel直接读取数据到DataGridView
C#推送信息到APNs的方法
Java使用Lettuce客户端在Redis在主从复制模式下命令执行的操作
![](https://img.aspxhome.com/file/2023/7/60797_0s.jpg)
java中多个@Scheduled定时器不执行的解决方法
Mybatis批量插入index out of range错误的解决(较偏的错误)
![](https://img.aspxhome.com/file/2023/2/84332_0s.png)
C#串口通信实现方法
浅析Java中接口和抽象类的七大区别
![](https://img.aspxhome.com/file/2023/6/76266_0s.png)
Intellij IDEA + Android SDK + Genymotion Emulator打造最佳Android开发环境
![](https://img.aspxhome.com/file/2023/4/60314_0s.png)