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

Java设计模式-创建型模式-建造者模式

建造者模式

  • 建造者模式
    • 案例
    • 与工厂模式的区别:
    • @Builder 注解

建造者模式

建造者模式是将一个复杂对象的构件与表示分离,使得同样的构件过程可以创建不同的表示。
建造者模式将内部构件的创建和组装分割开,一般使用链式编程,代码整洁优雅

案例

建造者模式比较简单,这里就直接上代码了

以 RabbitMQClient 为例实现建造者模式

public class RabbitMQClient {// 私有构造,目标类的构造方法要传入一个Builder对象private RabbitMQClient(Builder builder){}// builder类位于目标类的内部,并且使用static修饰public static class Builder{// 保证不可变对象的密闭性private String host = "127.0.0.1";private int port = 5672;private int mode;private String exchange;private String queue;private boolean isDurable = true;int connectTime = 1000;public String getHost() {return host;}public Builder setHost(String host) {this.host = host;return this;}public int getPort() {return port;}public Builder setPort(int port) {this.port = port;return this;}public int getMode() {return mode;}public Builder setMode(int mode) {this.mode = mode;return this;}public String getExchange() {return exchange;}public Builder setExchange(String exchange) {this.exchange = exchange;return this;}public String getQueue() {return queue;}public Builder setQueue(String queue) {this.queue = queue;return this;}public boolean isDurable() {return isDurable;}public Builder setDurable(boolean durable) {isDurable = durable;return this;}public int getConnectTime() {return connectTime;}public Builder setConnectTime(int connectTime) {this.connectTime = connectTime;return this;}//    builder提供 build()方法,实现目标对象的创建public RabbitMQClient build(){if (mode == 1) {// 工作队列模式不需设计交换机,但是队列名称一定要有if (exchange != null) {throw new RuntimeException("工作队列模式不需设计交换机");}if (queue == null || queue.trim().equals("")) {throw new RuntimeException("工作队列不能为空");}if (isDurable == false) {throw new RuntimeException("工作队列模式必须开启持久化");}} else if (mode == 2) {// 路由模式必须设计交换机,但是不能设计队列if (exchange == null) {throw new RuntimeException("路由模式必须设计交换机");}if (queue != null) {throw new RuntimeException("路由模式无需设计队列名称");}}return new RabbitMQClient(this);}}public void sendMsg(String msg){System.out.println("发送消息:"  + msg);}}

测试代码如下:

@Test
public void builderTest(){RabbitMQClient client = new RabbitMQClient.Builder().setHost("192.168.11.111").setMode(1).setPort(5672).setQueue("queue-test").build();client.sendMsg("this is test");
}

与工厂模式的区别:

工厂模式注重整体对象的创建,建造者模式注重构件的创建,然后再将构件组装成一个完整的对象。
一般情况下,建造者模式创建的对象更复杂
工厂模式是生产工厂,建造者模式是组装工厂

这里引申一下建造者模式简单构件方法——使用lombok 包下面的 @Builder 注解

@Builder 注解

使用@Builder 注解 可以方便快捷使用 建造者模式
下面使使用案例

@Builder
public class RabbitMQClient2 {private String host = "127.0.0.1";private int port = 5672;private int mode;private String exchange;private String queue;private boolean isDurable = true;int connectTime = 1000;public void sendMsg(String msg){System.out.println("发送消息:"  + msg);}}

测试代码:

/*** lombok @Builder 注解使用*/
@Test
public void builder2Test(){RabbitMQClient2 client = RabbitMQClient2.builder().host("192.168.11.111").mode(1).port(5672).queue("queue-test").build();client.sendMsg("this is test");
}
http://www.lryc.cn/news/229241.html

相关文章:

  • PyQt中QFrame窗口中的组件不显示的原因
  • git 命令行回退版本
  • IntelliJ IDEA 安装 GitHub Copilot插件 (最新)
  • viewpage选择器
  • vue中如何将json数组指定的key赋值给el-form-item并均匀的分成2列
  • 笔记本分屏怎么操作?3个方法提高工作效率!
  • Android 使用poi生成Excel ,word并保存在指定路径内
  • 嵌入式杂记 -- MCU的大小端模式
  • 对这套BI零售数据分析方案心动,是零售人天性
  • vuekeyclock 集成
  • ARM Linux 基础学习 / 配置交叉编译工具链 / 编译 Linux 应用和驱动 / 编译内核
  • 通讯协议学习之路(实践部分):SPI开发实践
  • 【系统安装】ubuntu20.04启动盘制作,正经教程,小白安装教程,百分百成功安装
  • 2023云计算发展趋势
  • C# .NET Core API Controller以及辅助专案
  • asp.net图书管理系统
  • 概念解析 | LoRA:低秩矩阵分解在神经网络微调中的作用
  • 前端---CSS的盒模型
  • Linux可以投屏到电视吗?用网页浏览器就能投屏到电视!
  • 云汇优想:抖音矩阵系统有哪些类型?
  • XSS 漏洞的理解
  • cocosCreator 之内存管理和释放
  • 飞天使-template模版相关知识
  • 一、Hadoop3.1.3集群搭建
  • QML16、从 C++ 定义 QML 类型
  • 【中间件篇-Redis缓存数据库06】Redis主从复制/哨兵 高并发高可用
  • LeetCode(12)时间插入、删除和获取随机元素【数组/字符串】【中等】
  • 前端面试题 计算机网络
  • windows aseprite编译指南(白嫖)
  • 生活污水处理一体化处理设备有哪些