首页 > 其他分享 >软件设计模式白话文系列(八)桥接模式

软件设计模式白话文系列(八)桥接模式

时间:2022-11-13 18:33:48浏览次数:44  
标签:BuyCert 桥接 BuyService System 模式 软件设计 new public out

软件设计模式白话文系列(八)桥接模式

1、描述

把一个事物的多个维度剥离出来,通过组合方式来达到灵活设计的目的,Java 中,我们一般是通过对象引用来替换继承关系,从而将抽象和实现解耦合。

桥接模式,可能大家只是不了解这个名称,但是我们的实际开发习惯基本都是有使用的。例如 spring 的注入功能(别说没用过哈)。所以说学习优秀的框架,就算没有去熟悉它的源码,精通它的底层逻辑,仅仅只是简单的使用都会在潜意识下提高我们的代码水平。

2、模式结构与实现逻辑

  • 抽象化类:业务抽象类。引用不同维度的接口对象。在日常开发中我们习惯忽略这个类,改进为业务接口类,需引用的接口对象直接在扩展抽象化类直接引用。
  • 扩展抽象化类:抽象化类的子类,就是日常开发中的业务实现类。
  • 实现化类:维度接口类,一般有多个,一个事物维度对一个该类。
  • 具体实现化类:实现化类的实现类。

本质的实现方式就是:业务类(扩展抽象化类)中引用其余业务类的父类或者接口(实现化类),在构造业务类时,根据需求动态传入其余业务类的具体实现类(具体实现类)。

3、实战代码

实战事例说明:

模拟购买私有数字证书业务,私有数字证书分为 RootCA 证书、SubCA 证书、订户证书 三类,购买方式分为支付宝支付、微信支付两种。如果业务实现类通过继承或者实现接口方式来实现逻辑,就不考虑还有其他业务情况,就只是满足每种支付支付方式和每种证书类型创建就只少需要 6 个实现类来完成目标,显然这样会导致系统暴增。然后我们通过桥接模式只需要一个类就解决了。

/**
 * 实现化类
 *
 * @author Eajur.Wen
 * @version 1.0
 * @date 2022-11-13 00:01:54
 */
public interface PayService {
    void pay();
}

/**
 * 实现化类
 *
 * @author Eajur.Wen
 * @version 1.0
 * @date 2022-11-12 23:58:24
 */
public interface CertService {
    void createCert();
}

/**
 * 抽象化类
 * 日常开发中一般没有抽象出这个类的
 *
 * @author Eajur.Wen
 * @version 1.0
 * @date 2022-11-12 23:41:04
 */
public abstract class BuyService {
    protected PayService payService;
    protected CertService certService;

    public BuyService(PayService payService, CertService certService) {
        this.payService = payService;
        this.certService = certService;
    }

    public abstract void BuyCert();
}

/**
 * 具体实现化类
 *
 * @author Eajur.Wen
 * @version 1.0
 * @date 2022-11-13 00:01:54
 */
public class AlipayServiceImpl implements PayService {
    @Override
    public void pay() {
        System.out.print("支付宝支付金额,开始");
    }
}

/**
 * 具体实现化类
 *
 * @author Eajur.Wen
 * @version 1.0
 * @date 2022-11-13 00:01:54
 */
public class WeChatServiceImpl implements PayService {
    @Override
    public void pay() {
        System.out.print("微信支付金额,开始");
    }
}

/**
 * 具体实现化类
 *
 * @author Eajur.Wen
 * @version 1.0
 * @date 2022-11-12 23:59:44
 */
public class RootCAServiceImpl implements CertService{
    @Override
    public void createCert() {
        System.out.println("创建 RootCA 证书");
    }
}

/**
 * 具体实现化类
 *
 * @author Eajur.Wen
 * @version 1.0
 * @date 2022-11-12 23:59:44
 */
public class SubCAServiceImpl implements CertService{
    @Override
    public void createCert() {
        System.out.println("创建 SubCA 证书");
    }
}

/**
 * 具体实现化类
 *
 * @author Eajur.Wen
 * @version 1.0
 * @date 2022-11-12 23:59:44
 */
public class CertServiceImpl implements CertService {
    @Override
    public void createCert() {
        System.out.println("创建订户证书");
    }
}

/**
 * 扩展抽象化类
 *
 * @author Eajur.Wen
 * @version 1.0
 * @date 2022-11-12 23:41:04
 */
public class BuyServiceImpl extends BuyService {

    public BuyServiceImpl(PayService payService, CertService certService) {
        super(payService, certService);
    }

    @Override
    public void BuyCert() {
        payService.pay();
        certService.createCert();
    }
}

/**
 * 测试类
 *
 * @author Eajur.Wen
 * @version 1.0
 * @date 2022-11-13 00:10:43
 */
public class Client {
    public static void main(String[] args) {
        BuyService buyService = new BuyServiceImpl(new AlipayServiceImpl(), new RootCAServiceImpl());
        buyService.BuyCert();
        System.out.println("------------------------");

        BuyService buyService2 = new BuyServiceImpl(new WeChatServiceImpl(), new RootCAServiceImpl());
        buyService2.BuyCert();
        System.out.println("------------------------");

        BuyService buyService3 = new BuyServiceImpl(new WeChatServiceImpl(), new SubCAServiceImpl());
        buyService3.BuyCert();
        System.out.println("------------------------");

        BuyService buyService4 = new BuyServiceImpl(new AlipayServiceImpl(), new SubCAServiceImpl());
        buyService4.BuyCert();
        System.out.println("------------------------");

        BuyService buyService5 = new BuyServiceImpl(new WeChatServiceImpl(), new CertServiceImpl());
        buyService5.BuyCert();
        System.out.println("------------------------");

        BuyService buyService6 = new BuyServiceImpl(new AlipayServiceImpl(), new CertServiceImpl());
        buyService6.BuyCert();
        System.out.println("------------------------");
    }
}

执行结果:

4、日常开发习惯代码

作者平时开发代码时,是没有 BuyService 这个抽象类的,在平时开发中每个业务类都是先定义接口类,再进行实现,如果为了规范实现化类而抽象化出抽象类,会显得代码太过臃肿。

作者一般是结合 lombok 的 @AllArgsConstructor 来实现目的

/**
 * 抽象化类
 *
 * @author Eajur.Wen
 * @version 1.0
 * @date 2022-11-12 23:41:04
 */
public interface BuyService {

    public abstract void BuyCert();
}

/**
 * 扩展抽象化类
 *
 * @author Eajur.Wen
 * @version 1.0
 * @date 2022-11-12 23:41:04
 */
@AllArgsConstructor
public class BuyServiceImpl implements BuyService {
    private PayService payService;
    private CertService certService;

    @Override
    public void BuyCert() {
        payService.pay();
        certService.createCert();
    }
}

/**
 * 测试类
 *
 * @author Eajur.Wen
 * @version 1.0
 * @date 2022-11-13 00:10:43
 */
public class Client {
    public static void main(String[] args) {
        BuyService buyService = new BuyServiceImpl(new AlipayServiceImpl(), new RootCAServiceImpl());
        buyService.BuyCert();
        System.out.println("------------------------");

        BuyService buyService2 = new BuyServiceImpl(new WeChatServiceImpl(), new RootCAServiceImpl());
        buyService2.BuyCert();
        System.out.println("------------------------");

        BuyService buyService3 = new BuyServiceImpl(new WeChatServiceImpl(), new SubCAServiceImpl());
        buyService3.BuyCert();
        System.out.println("------------------------");

        BuyService buyService4 = new BuyServiceImpl(new AlipayServiceImpl(), new SubCAServiceImpl());
        buyService4.BuyCert();
        System.out.println("------------------------");

        BuyService buyService5 = new BuyServiceImpl(new WeChatServiceImpl(), new CertServiceImpl());
        buyService5.BuyCert();
        System.out.println("------------------------");

        BuyService buyService6 = new BuyServiceImpl(new AlipayServiceImpl(), new CertServiceImpl());
        buyService6.BuyCert();
        System.out.println("------------------------");
    }
}

5、适用性

  • 当一个业务存在多个维度且维度会独立变化的时候。
  • 业务不希望使用继承或因为多层次继承而导致系统类暴增时。

标签:BuyCert,桥接,BuyService,System,模式,软件设计,new,public,out
From: https://www.cnblogs.com/eajur/p/16886544.html

相关文章

  • 部署Kubernetes 1.25.4初始ipvs模式
    1、环境准备主机名IP地址系统版本k8s-master01k8s-master01.wang.org​kubeapi.wang.orgkubeapi192.168.100.201Ubuntu2004k8s-master02k8s-master02.wang.org192.168.1......
  • C++设计模式-(创建模式)原型模式
     原型模式主要用于复制当前对象的副本 #include<iostream>classanimal{public:virtual~animal(){}virtualvoideat(){std::cout......
  • 4.流控模式(关联和链路)
    1.流控模式在添加限流规则时,点击高级选项,可以选择三种流控模式:直接:统计当前资源的请求,触发阈值时对当前资源直接限流,也是默认的模式关联:统计与当前资源相关的另一个资......
  • 11.vim模式使用过程中常用快捷键
    一.普通模式#1.命令光标跳转G      #光标跳转至末端gg     #光标跳转至顶端Ngg    #光标跳转至当前文件内的N行$      #光......
  • (作者推荐)【RocketMQ入门到精通】— RocketMQ中级特性能力 | ​长轮询Pull和Push模式
    名言警句任何先进的技术均与魔法无异追本溯源【​​经历了6个月的失踪,我将带着干货终究归来!【RocketMQ入门到精通】​​】RocketMQ消费机制回顾   在众多MQ的体系中,一......
  • 软件设计模式白话文系列(七)适配器模式
    1、描述适配器模式顾名思义就是将某个类的接口转换成客户端期望的另一个接口表示。适配器模式可以消除由于接口不匹配所造成的类兼容性问题。2、适用性客户端需要调用现......
  • 桥接,仅主机和nat 看這个就够了。。一定要懂交换机原理
    简单点就是在虚拟机中有个ovs(虚拟交换机) 大家都来来连接虚拟交换机。。。。通过仅主机和nat都提供dhcp 。按照dhcp的网段分ip。当然也可以自己设置ip但是设置的必......
  • 前端学习-CSS-06-元素显示模式
    学习时间:2022.11.12元素显示模式块级元素特点:独占一行宽度默认等于其父母标签,高度由内容撑开宽高可以设置代表元素:div,p,h系列,ul,li,dl,dt,dd,form,header,n......
  • Java消费者生产者模式,并发控制。
    概论举个例子:有一个固定容量的货架,生产者放商品进来,消费者拿商品出去,为了保证正常放入和正常拿出(数据的正确性,不会出现超过容量的存放,拿到空气)。使用同步块中的wait和n......
  • 设计模式学习(二十四):Spring 中使用到的设计模式
    设计模式学习(二十四):Spring中使用到的设计模式作者:Grey原文地址:博客园:设计模式学习(二十四):Spring中使用到的设计模式CSDN:设计模式学习(二十四):Spring中使用到的设计模式......