constructor 上的 lombok builder 应该只有一个吗?
Does the lombok buiilder on constructor should be only one?
我想我可以像普通的 java 构造函数一样通过参数覆盖构造函数上的构建器。
但是 IDE IntelliSense 没有建议我想要的方法。
constructor上的lombok builder应该只有一个吗?
这是我的代码:
- 建设者
@Builder(builderMethodName = "builderByDto")
public SourceInfo(SourceInfoDto sourceInfoDto) {
this.sourceTitle = sourceInfoDto.getSourceTitle();
this.isbn = sourceInfoDto.getIsbn();
this.eissn = sourceInfoDto.getEissn();
this.issn = sourceInfoDto.getIssn();
this.volume = sourceInfoDto.getVolume();
this.pages = sourceInfoDto.getPages();
this.issue = sourceInfoDto.getIssue();
this.publishedYear = sourceInfoDto.getPublishedYear();
this.publishedDate = sourceInfoDto.getPublishedDate();
}
@Builder(builderMethodName = "builderByLiteRecord")
public SourceInfo(LiteRecordDto liteRecordDto) {
Map<String, String> source = liteRecordDto.getSource();
this.sourceTitle = source.get("sourceTitle");
this.isbn = source.get("isbn");
this.eissn = source.get("eissn");
this.issn = source.get("issn");
this.volume = source.get("volume");
this.pages = source.get("pages");
this.issue = source.get("issue");
this.publishedYear = source.get("publishedYear");
this.publishedDate = source.get("publishedDate");
}
- 初始化
@Builder
public Paper(PaperDto paperDto) {
...
this.sourceInfo = SourceInfo.builderByDto()
.sourceInfoDto(paperDto.getSourceInfo()).build(); // Error
...
}
如果您想要两个基于构造函数的构建器,您还需要两个单独的构建器 classes。您可以按如下方式自定义生成器 class:
@Builder(builderClassName = "BuilderByLiteRecord", builderMethodName = "builderByLiteRecord")
public SourceInfo(LiteRecordDto liteRecordDto) {
...
PS:至少从您在示例中展示的内容来看,构建器模式似乎并不适合这里,至少不适合构造函数。
我会将 @Builder
仅放在 class 上,这样您就可以得到一个包含所有字段的生成器 class。然后使用两个静态方法,每个方法都将您的 DTO classes 之一作为参数,并 return 一个用 DTO 中的数据填充的构建器。
我想我可以像普通的 java 构造函数一样通过参数覆盖构造函数上的构建器。
但是 IDE IntelliSense 没有建议我想要的方法。
constructor上的lombok builder应该只有一个吗?
这是我的代码:
- 建设者
@Builder(builderMethodName = "builderByDto")
public SourceInfo(SourceInfoDto sourceInfoDto) {
this.sourceTitle = sourceInfoDto.getSourceTitle();
this.isbn = sourceInfoDto.getIsbn();
this.eissn = sourceInfoDto.getEissn();
this.issn = sourceInfoDto.getIssn();
this.volume = sourceInfoDto.getVolume();
this.pages = sourceInfoDto.getPages();
this.issue = sourceInfoDto.getIssue();
this.publishedYear = sourceInfoDto.getPublishedYear();
this.publishedDate = sourceInfoDto.getPublishedDate();
}
@Builder(builderMethodName = "builderByLiteRecord")
public SourceInfo(LiteRecordDto liteRecordDto) {
Map<String, String> source = liteRecordDto.getSource();
this.sourceTitle = source.get("sourceTitle");
this.isbn = source.get("isbn");
this.eissn = source.get("eissn");
this.issn = source.get("issn");
this.volume = source.get("volume");
this.pages = source.get("pages");
this.issue = source.get("issue");
this.publishedYear = source.get("publishedYear");
this.publishedDate = source.get("publishedDate");
}
- 初始化
@Builder
public Paper(PaperDto paperDto) {
...
this.sourceInfo = SourceInfo.builderByDto()
.sourceInfoDto(paperDto.getSourceInfo()).build(); // Error
...
}
如果您想要两个基于构造函数的构建器,您还需要两个单独的构建器 classes。您可以按如下方式自定义生成器 class:
@Builder(builderClassName = "BuilderByLiteRecord", builderMethodName = "builderByLiteRecord")
public SourceInfo(LiteRecordDto liteRecordDto) {
...
PS:至少从您在示例中展示的内容来看,构建器模式似乎并不适合这里,至少不适合构造函数。
我会将 @Builder
仅放在 class 上,这样您就可以得到一个包含所有字段的生成器 class。然后使用两个静态方法,每个方法都将您的 DTO classes 之一作为参数,并 return 一个用 DTO 中的数据填充的构建器。