简体   繁体   English

为什么在 Lombok 的 @Builder 中有包私有构造函数?

[英]Why have package private constructor in @Builder in Lombok?

Recently, I came across Lombok's builder and frankly loved it.最近,我遇到了 Lombok 的 builder 并且坦率地喜欢它。 Then I came to know that Builder pattern is described at many resources for instance Effective Java.然后我开始知道 Builder 模式在许多资源中都有描述,例如 Effective Java。 Somewhat like this 有点像这样

Now, in this pattern, the constructor is kept private so that the instance of the class could only be created by only using the builder.现在,在这种模式中,构造函数是私有的,因此类的实例只能通过使用构建器来创建。

But, in Lombok's documentation for the builder Builder's Documentation the constructor visibility is kept package private.但是,在 Lombok 的 builder Builder's Documentation 文档中,构造函数的可见性是包私有的。 What could be the reasons behind keeping it as package private?将其作为包私有的原因可能是什么?

The answer to your question described in detail at https://softwareengineering.stackexchange.com/questions/362265/builder-with-constructor-or-factory-method .https://softwareengineering.stackexchange.com/questions/362265/builder-with-constructor-or-factory-method中详细描述了您的问题的答案。 I see that your question is more specific to Project Lombok's design so you might want to join the community.我发现您的问题更针对 Project Lombok 的设计,因此您可能想加入社区。 But in general I agree with the accepted answer and the way Lombok implemented the builder pattern using a factory method.但总的来说,我同意接受的答案以及 Lombok 使用工厂方法实现构建器模式的方式。

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

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