The Builder pattern simulates named optional parameters(Java)
JAVA The pattern named Builder parameters optional
2023-09-11 14:17:56 时间
the Builder pattern is a good choice when designing classes whose constructors or static factories would have more than a handful of parameters.
/** * Created by xfyou 2017/5/27 14:41 14:42. * The Builder pattern simulates named optional parameters */ public class NutritionFacts { private final int servingSize; private final int servings; private final int calories; private final int fat; private final int sodium; private final int carbohydrate; public static class Builder { // Required parameters private final int servingSize; private final int servings; // Optional parameters - initialized to default values private int calories = 0; private int fat = 0; private int carbohydrate = 0; private int sodium = 0; public Builder(int servingSize, int servings) { this.servingSize = servingSize; this.servings = servings; } public Builder calories(int val) { this.calories = val; return this; } public Builder carbohydrate(int val) { this.carbohydrate = val; return this; } public Builder sodium(int val) { this.sodium = val; return this; } public NutritionFacts build() { return new NutritionFacts(this); } } public NutritionFacts(Builder builder) { this.calories = builder.calories; this.carbohydrate = builder.carbohydrate; this.fat = builder.fat; this.servings = builder.servings; this.servingSize = builder.servingSize; this.sodium = builder.sodium; } public int getServingSize() { return servingSize; } public int getServings() { return servings; } public int getCalories() { return calories; } public int getFat() { return fat; } public int getSodium() { return sodium; } public int getCarbohydrate() { return carbohydrate; } }
相关文章
- 新建maven指定jdk版本-eclipse新建maven项目报错The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path的解决方案
- JAVA_the user operation is waiting怎么办
- Java实现 LeetCode 810 黑板异或游戏 (分析)
- Java实现 蓝桥杯 算法提高 菱形
- Java 第十一届 蓝桥杯 省模拟赛 螺旋矩阵
- Java实现 LeetCode 676 实现一个魔法字典(暴力)
- Java实现 LeetCode 999 车的可用捕获量(简单搜索)
- Java实现 洛谷 P1085 不高兴的津津
- java实现金额组合
- java实现文件管理
- Java实现 蓝桥杯 历届试题 国王的烦恼
- Java实现构造无向图的欧拉回路( The Necklace)
- Java实现构造无向图的欧拉回路( The Necklace)
- java实现控制台表格
- (Java实现) 洛谷 P1553 数字反转(升级版)
- java 11 Java Flight Recorder
- 【JAVA】Java循环语句中的continue跳转进入下一次循环是否判断循环条件
- sharepoint2010:The number of items in this list exceeds the list view threshold, which is 20000 items.
- Eclipse 报 “Exception in thread "main" java.lang.OutOfMemoryError: Java heap space ”错误的解决办法
- Java EE spec 不求人
- 【JAVA】java编译错误:编码UTF8/GBK的不可映射字符
- [Angular2 Router] Optional Route Query Parameters - The queryParams Directive and the Query Parameters Observable
- macos:安装java 17.0.6(android studio报错:Unable to locate a Java Runtime.)
- Java java.util.concurrent.Future的一个例子
- 【Codeforces 1083A】The Fair Nut and the Best Path
- Android Studio 4.2.2的报错提示:Could not determine java version from ‘11.0.8‘. The project uses Gradle v
- 成功解决MSB8020 The build tools for v141 (Platform Toolset = ‘v141‘) cannot be found. To build using the
- 《互联网领域Java从来就不是主流》一个完全错误的标题!炒作短时间带来了流量,却损害了作者的声誉,推荐人的信誉,以及读者对于网站的期待。
- Paper:LSTM之父眼中的深度学习十年简史《The 2010s: Our Decade of Deep Learning / Outlook on the 2020s》的解读
- 保留状态的秘密 - 深入了解Java备忘录模式(Memento Pattern)
- 当年的java考试:Java景区预约登记管理系统(maven整合servlet)
- 【DataStructure】The description of Java Collections Framework
- Java开发技术之成为高级java工程师必须学习的三个技术
- java - 详解 Java 17 中新推出的密封类
- 【多线程】Java中是如何保证多线程间的数据共享的?
- 【java】Java 继承
- JAVA SSM框架+Redis 实现单点登录