• 结构型设计模式之桥接模式


    桥接模式

    概述

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

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

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

    在这里插入图片描述

    应用场景

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

    适用业务场景:

    1.在抽象和具体实现之间需要增加更多的灵活性的场景。
    
    2.一个类存在两个(或多个)独立变化的维度,而这两个(或多个)维度都需要独立进行扩展。
    
    3.不希望使用继承,或因为多层继承导致系统类的个数剧增。
    
    • 1
    • 2
    • 3
    • 4
    • 5

    优缺点

    优点:

    1.分离抽象部分及其具体实现部分
    
    2.提高了系统的扩展性
    
    3.实现细节对客户透明
    
    • 1
    • 2
    • 3
    • 4
    • 5

    缺点:

    1.增加了系统的理解与设计难度
    
    2.需要正确地识别系统中两个独立变化的维度
    
    • 1
    • 2
    • 3

    主要角色

    1.抽象(Abstraction)

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

    2.修正抽象(RefinedAbstraction)

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

    3.实现(Implementor)

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

    1.具体实现(Concretelmplementor)

    实现的具体实现。

    在这里插入图片描述

    桥接模式的基本使用

    创建实现角色

    public interface IImplementor {
        void operationImpl();
    }
    
    • 1
    • 2
    • 3

    创建具体实现角色

    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");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    创建抽象角色

    public abstract class Abstraction {
    
        protected IImplementor mImplementor;
    
        public Abstraction(IImplementor implementor) {
            this.mImplementor = implementor;
        }
    
        public void operation() {
            this.mImplementor.operationImpl();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    创建修正抽象角色

    public class RefinedAbstraction extends Abstraction {
        public RefinedAbstraction(IImplementor implementor) {
            super(implementor);
        }
    
        @Override
        public void operation() {
            super.operation();
            System.out.println("RefinedAbstraction operation");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    客户端调用

        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();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    ConcreteImplementorA operationImpl
    RefinedAbstraction operation
    ConcreteImplementorB operationImpl
    RefinedAbstraction operation
    
    • 1
    • 2
    • 3
    • 4

    桥接模式实现消息发送

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

    创建实现角色

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

    public interface IMessage {
        void send(String message);
    }
    
    • 1
    • 2
    • 3

    创建具体实现角色

    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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    创建抽象角色

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

    public abstract class AbastractMessage {
        // 持有实现角色的引用
        private IMessage message;
    
        // 构造函数,传入实现角色的引用
        public AbastractMessage(IMessage message) {
            this.message = message;
        }
        // 发送消息的方法,调用实现角色的方法
        void sendMessage(String message){
            this.message.send(message);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    创建修正抽象角色

    创建普通消息

    public class NomalMessage extends AbastractMessage {
        public NomalMessage(IMessage message) {
            super(message);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    创建重要消息

    public class ImportanceMessage extends AbastractMessage {
        public ImportanceMessage(IMessage message) {
        	message = message + "【重要消息】";
            super(message);
        }
    
        void sendMessage(String message){
            super.sendMessage(message);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    客户端调用

        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");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    使用邮件消息发送hello world
    使用短信消息发送hello world【重要消息】
    
    • 1
    • 2
  • 相关阅读:
    SpringMVC处理Ajax请求及处理和响应json格式的数据
    部署在阿里云ECS服务器上的微服务项目中获取到的时间和windows的时间不一样的问题
    仿游戏热血江湖游戏类22(FLD_FJ_中级附魂)
    《谷歌眼镜》新书作者:眼镜需要成为AR的载体吗?
    Stable DIffusion 炫酷应用 | AI嵌入艺术字+光影光效
    go语言|数据结构:二叉树可视化(svg树形图改进版)
    StarkNet System Architecture 系统架构
    SpringBoot-AOP-Logback用切面拦截操作日志
    5G核心网网元服务异常检测
    Java注解
  • 原文地址:https://blog.csdn.net/qq_38628046/article/details/126194229