查看“建造者模式”的源代码
←
建造者模式
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
https://www.bilibili.com/video/BV1rV4y1s7JG === 1.不用建造者有什么麻烦? === 假设我们要自己开发一个RabbitMQ消息队列的客户端,有很多需要初始化的参数,你会怎么做?<syntaxhighlight lang="java"> package io.github.jihch; public class RabbitMQClientSample1 { private String host = "127.9.9.1"; private int port = 5672; private int mode; private String exchange; private String queue; private boolean isDurable = true; int connectionTimeout = 1000; private RabbitMQClientSample1(String host, int port, int mode, String exchange, String queue, boolean isDurable, int connectionTimeout) { this.host = host; this.port = port; this.mode = mode; this.exchange = exchange; this.queue = queue; this.isDurable = isDurable; this.connectionTimeout = connectionTimeout; if (mode == 1) {//工作队列模式不需要设置交换机,但queue必填 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) { //路由模式必须设置交换机,但不能设置 queue 队列 if (exchange == null || exchange.trim().equals("")) { throw new RuntimeException("路由模式请设置交换机"); } if (queue != null) { throw new RuntimeException("路由模式无需设置队列名称"); } } //其他各种验证 }// end constructor public void sendMessage(String msg) { System.out.println("正在发送消息:" + msg); } public static void main(String[] args) { RabbitMQClientSample1 client = new RabbitMQClientSample1("192.168.31.210", 5672, 2, "sample-exchange", null, true, 5000); client.sendMessage("Test"); } } </syntaxhighlight>每次使用构造方法创建新的对象都要传入很多参数(想想生产环境如果按数据表抽象,一张表有多少字段?!就算默认值可以为 null,一个个字段对照填写费劲不费劲?!),其中有一些参数在一些情况下使用默认值就可以,并不是必要填写的,还是改用 set 方法灵活赋值吧
返回至
建造者模式
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
Spring Boot 2 零基础入门
Spring Cloud
Spring Boot
设计模式之禅
VUE
Vuex
Maven
算法
技能树
Wireshark
IntelliJ IDEA
ElasticSearch
VirtualBox
软考
正则表达式
程序员精讲
软件设计师精讲
初级程序员 历年真题
C
SQL
Java
FFmpeg
Redis
Kafka
MySQL
Spring
Docker
JMeter
Apache
Linux
Windows
Git
ZooKeeper
设计模式
Python
MyBatis
软件
数学
PHP
IntelliJ IDEA
CS基础知识
网络
项目
未分类
MediaWiki
镜像
问题
健身
国债
英语
烹饪
常见术语
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息