简体   繁体   English

我们应该在 Java 中将常量与构建器方法一起使用吗?

[英]Should we use constants with builder methods in Java?

Say, I am creating an object using the builder pattern as follows:说,我正在使用构建器模式创建一个对象,如下所示:

CfnResolverProps props = CfnResolverProps.builder()
.apiId(“TestApiId”)
.typeName("Query")
.fieldName(“list”)
.dataSourceName(“SomeName”)
.build()

So, here, is it a good idea to make static constants variables for all the TestApiId, Query, list, SomeName?那么,在这里,为所有 TestApiId、Query、list、SomeName 创建静态常量变量是个好主意吗? Or with builder pattern, it does not matter?还是用builder模式,没关系? For me, here, it doesn't matter much because, here it is already readable code as builderPattern tells the name of the fields explicitly.对我来说,这里并不重要,因为这里它已经是可读的代码,因为 builderPattern 明确地告诉了字段的名称。 What are the guidelines people use to use constants in Java?人们在 Java 中使用常量的准则是什么?

The idea behind using constant is that it's immutable and not going to change.使用常量背后的想法是它是不可变的,不会改变。

To address your question, do you think the string literals that are used in the construction of object CfnResolverProps are going to be used else where as well?为了解决您的问题,您认为在对象CfnResolverProps的构造中使用的字符串文字是否也将在其他地方使用?

If the answer is yes, you should create constants for those literals.如果答案是肯定的,您应该为这些文字创建常量。 If the answer is no, it's okay to keep the code as-is.如果答案是否定的,则可以保持代码原样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM