目录

1 Builder模式

2 经典Builder模式

2.1 抽象建造者角色(Builder)

2.2 具体建造者(ConcreteBuilder)

2.3 指导者(Director)

2.4 产品角色(Product)

2.5 调试代码

3 链式Builder模式

3.1 产品角色(Product)

3.2 调试代码

注:


1 Builder模式

Buidler模式主要分为两种:1、经典Builder模式;2、链式变形Builder模式。

当一个类的构造函数参数个数超过4个,而且这些参数有些是可选的参数,考虑使用构造者模式(这是一个重要的应用场景:必选参数和可选参数)。

2 经典Builder模式

2.1 抽象建造者角色(Builder)

创建一个Product对象的各个部件指定抽象接口,以规范产品对象的各个组成成分的建造。一般而言,此角色规定要实现复杂对象的哪些部分的创建,并不涉及具体的对象部件的创建。

/*** 行高列宽信息(经典Builder模式-抽象Builder)** @author xudongmaster*/
public abstract class Builder {abstract RowHeightColWidthModel build();
}

2.2 具体建造者(ConcreteBuilder)

1)实现Builder的接口以构造和装配该产品的各个部件。即实现抽象建造者角色Builder的方法。

2)定义并明确它所创建的表示,即针对不同的商业逻辑,具体化复杂对象的各部分的创建。

3) 提供一个检索产品的接口。

4) 构造一个使用Builder接口的对象即在指导者的调用下创建产品实例。

package com.xudongbase.designpattern.builder.classic;import lombok.Getter;/*** 行高列宽信息(经典Builder模式-具体Builder)** @author xudongmaster*/
@Getter
public class RowHeightColWidthModelBuilder extends Builder {/*** sheet名称*/private String sheetName;/*** 行号*/private Integer rowIndex;/*** 列号*/private Integer colIndex;/*** 行高*/private Float rowHeight;/*** 列宽*/private Integer colWidth;protected void rowIndex(Integer rowIndex) {this.rowIndex = rowIndex;}protected void colIndex(Integer colIndex) {this.colIndex = colIndex;}protected void rowHeight(Float rowHeight) {this.rowHeight = rowHeight;}protected void colWidth(Integer colWidth) {this.colWidth = colWidth;}public RowHeightColWidthModelBuilder(String sheetName) {this.sheetName = sheetName;}@Overridepublic RowHeightColWidthModel build() {return new RowHeightColWidthModel(this);}
}

2.3 指导者(Director)

调用具体建造者角色以创建产品对象的各个部分。指导者并没有涉及具体产品类的信息,真正拥有具体产品的信息是具体建造者对象。它只负责保证对象各部分完整创建或按某种顺序创建。

package com.xudongbase.designpattern.builder.classic;/*** 行高列宽信息(经典Builder模式-Director)** @author xudongmaster*/
public class RowHeightColWidthModelDirector {private RowHeightColWidthModelBuilder builder;public RowHeightColWidthModelDirector(RowHeightColWidthModelBuilder builder) {this.builder = builder;}public void construct(Integer rowIndex, Float rowHeight, Integer colIndex, Integer colWidth) {builder.rowIndex(rowIndex);builder.rowHeight(rowHeight);builder.colIndex(colIndex);builder.colWidth(colWidth);}
}

2.4 产品角色(Product)

建造中的复杂对象。它要包含那些定义组件的类,包括将这些组件装配成产品的接口。

package com.xudongbase.designpattern.builder.classic;import lombok.Getter;/*** 行高列宽信息(经典Builder模式-Product)** @author xudongmaster*/
@Getter
public class RowHeightColWidthModel {/*** sheet名称*/private String sheetName;/*** 行号*/private Integer rowIndex;/*** 列号*/private Integer colIndex;/*** 行高*/private Float rowHeight;/*** 列宽*/private Integer colWidth;public RowHeightColWidthModel(RowHeightColWidthModelBuilder builder) {this.sheetName = builder.getSheetName();this.rowIndex = builder.getRowIndex();this.colIndex = builder.getColIndex();this.rowHeight = builder.getRowHeight();this.colWidth = builder.getColWidth();}
}

2.5 调试代码

    /*** 测试经典Builder模式*/@Testpublic void testClassicBuilder() {String sheetName = "sheet1";//生成BuilderRowHeightColWidthModelBuilder builder = new RowHeightColWidthModelBuilder(sheetName);//Director装配BuilderRowHeightColWidthModelDirector director = new RowHeightColWidthModelDirector(builder);director.construct(1, 20f, 2, 30);//Builder生成productRowHeightColWidthModel model = builder.build();}

3 链式Builder模式

3.1 产品角色(Product)

Product类内部集成一个静态内部类Builder,通过Builder类建造Product类实例。而且Builder类赋值可以通过链式的方式进行赋值。

package com.xudongbase.designpattern.builder.chained;import lombok.Getter;/*** 行高列宽信息(链式Builder模式)** @author xudongmaster*/
@Getter
public class RowHeightColWidthModel {/*** sheet名称*/private String sheetName;/*** 行号*/private Integer rowIndex;/*** 列号*/private Integer colIndex;/*** 行高*/private Float rowHeight;/*** 列宽*/private Integer colWidth;private RowHeightColWidthModel(Builder builder) {this.sheetName = builder.sheetName;this.rowIndex = builder.rowIndex;this.colIndex = builder.colIndex;this.rowHeight = builder.rowHeight;this.colWidth = builder.colWidth;}public static class Builder {/*** sheet名称*/private String sheetName;/*** 行号*/private Integer rowIndex;/*** 列号*/private Integer colIndex;/*** 行高*/private Float rowHeight;/*** 列宽*/private Integer colWidth;public Builder rowIndex(Integer rowIndex) {this.rowIndex = rowIndex;return this;}public Builder colIndex(Integer colIndex) {this.colIndex = colIndex;return this;}public Builder rowHeight(Float rowHeight) {this.rowHeight = rowHeight;return this;}public Builder colWidth(Integer colWidth) {this.colWidth = colWidth;return this;}public Builder(String sheetName) {this.sheetName = sheetName;}public RowHeightColWidthModel build() {return new RowHeightColWidthModel(this);}}
}

3.2 调试代码

    /*** 测试链式Builder模式*/@Testpublic void testChainedBuilder() {String sheetName = "sheet1";//product内部的Builder生成productcom.xudongbase.designpattern.builder.chained.RowHeightColWidthModel model = new com.xudongbase.designpattern.builder.chained.RowHeightColWidthModel.Builder(sheetName).rowIndex(1).rowHeight(20f).colIndex(2).colWidth(30).build();}

注:

需要查看源码请前往Gitee的xudongbase的design_pattern分支。

xudongbase: 主要是项目中可以用到的共通方法 - Gitee.comhttps://gitee.com/xudong_master/xudongbase/tree/design_pattern/

设计模式-建造者模式(Builder)2种实现方式(经典Builder模式、链式Builder模式)相关推荐

  1. 生产者-消费者模式的三种实现方式

    2.生产者-消费者模式的三种实现方式 1.背景                                                                    生产者生产数据到缓 ...

  2. Java23种设计模式之单例模式的五种实现方式、反射破解单例模式、不能破解枚举单例模式详解

    源码链接(Gitee码云):https://gitee.com/oldou/javadesignpatterns 这里有我整理好的Java23种设计模式的源码以及博客教程,博客教程中介绍了Java23 ...

  3. 单例对象会被jvm的gc时回收吗_设计模式专题02-单例五种创建方式

    单例五种创建方式(下一篇:工厂模式) 什么是单例 保证一个类只有一个实例,并且提供一个访问该全局访问点 单例应用场景 1. Windows的Task Manager(任务管理器)就是很典型的单例模式( ...

  4. Java面试23种设计模式之单例模式的8种实现方式

    单例模式8中实现方式 1.单例模式介绍 2.单例模式的八种方式 3.饿汉式(静态常量),这种单例模式可用,可能造成内存浪费. 4.饿汉式(静态代码块),这种单例模式可用,可能造成内存浪费. 5.懒汉式 ...

  5. Android中夜间模式的三种实现方式

    参考:https://www.jianshu.com/p/f3aaed57fa15 在本篇文章中给出了三种实现日间/夜间模式切换的方案: 使用 setTheme 的方法让 Activity 重新设置主 ...

  6. 单利 java_关于单利模式的几种实现方式

    单例模式是java中非常常见的一种设计模式,也是java学习中很热门的一个知识模块,今天就和大家一起来了解一下单例模式和单例模式的几种实现方式. 单例模式的定义: 单例模式是指某个类只有一个实例,并且 ...

  7. 【App自动化测试】(八)三种等待方式——强制等待、隐式等待、显示等待

    目录 1. 为什么要添加等待? 2. 三种等待方式 3. 强制(直接)等待 4. 隐式等待 4.1 隐式等待说明 4.2 隐式等待无法解决的问题 5. 显式等待 5.1 为什么要使用显示等待机制? 5 ...

  8. 从装饰模式和职责链模式看链式结构模式

    装饰模式(Decorator Pattern) 动态给对象增加功能,从一个对象的外部来给对象添加功能,相当于改变了对象的外观,比用继承的方式更加的灵活. 允许向一个现有的对象添加新的功能,同时又不改变 ...

  9. 如何做代码抽象设计,多种设计模式的应用【四种抽象方式,干货附上代码】

    代码这种东西,机器不管你写的怎么样,肯定能识别, 人就不一样了,前人拉屎后人踩屎,一坨认栽,n坨就有点过分了哈 一般写代码也不用太高大上,很多接口还是很简单的,如果把各种设计模式搞上来那叫过度设计,叫 ...

  10. JAVA-单例模式的几种实现方式

    一.什么是单例模式 单例:保证一个类仅有一个实例,并提供一个访问它的全局访问点. 单例模式是一种常用的软件设计模式之一,其目的是保证整个应用中只存在类的唯一个实例. 比如我们在系统启动时,需要加载一些 ...

最新文章

  1. 使用ConcurrentHashMap一定线程安全?
  2. Lucene in action 笔记 analysis篇
  3. lazada开店平台费用都有哪些,产品如何来做定价?
  4. oracle日志文件大小规则,在线重做Oracle日志文件大小
  5. 运维祈求不宕机_[国庆特辑] 程序员应该求谁保佑才能保证不宕机?
  6. [vue] watch的属性用箭头函数定义结果会怎么样?
  7. 面试精讲之面试考点及大厂真题 - 分布式专栏 16 数据库如何做分库分表,读写分离
  8. 30. 包含min函数的栈
  9. 几乎是苹果6倍!华为可穿戴设备出货量Q1同比猛增282%
  10. 【ElasticSearch】Es 源码之 LifecycleComponent 源码解读
  11. STL学习笔记-- multiset
  12. DotNetTextBoxV3.0在线编辑器控件Ver3.4.6 Open Source免费开源版
  13. 中图分类法----T-0
  14. TeX Live 2021 从卸载到安装指南
  15. Microsoft SQL Server 生成随机数字、字符串、日期、验证码以及 UUID
  16. Drupal view实现排序:未来升序,过去降序
  17. iis搭建ftp服务器及身份验证设置
  18. 靶机10 DC-9(过程超详细)
  19. windows下测试磁盘读写(HD Tune)
  20. 一木禾网盘下载分析及批量获取下载地址的实现(下)

热门文章

  1. 基于linux环境tcp网络编程(在线英英词典)文档【2】
  2. Windows 7 安装VS2008 SP1 失败
  3. 189邮箱smpt服务器,189邮箱登录(常用邮箱客户端设置指南)
  4. linux全自动备份网站到百度云盘,Linux定时备份数据到百度云盘
  5. 基于MC1496乘法器的AM信号调制
  6. 2.4 滑块拼图验证码
  7. 实战一:输出“王者荣耀”的游戏角色
  8. IDEA新手使用教程(详解)
  9. 【开源】EasyDarwin编译全过程:Linux系统下编译运行最新版EasyDarwin的步骤介绍
  10. gradle下载很慢的解决方式