[英]Effective Java. Serializable Builder pattern (how to add public no-arg constructor?)
I want to create a class in Joshua Bloch's style using Builder pattern.我想使用 Builder 模式以 Joshua Bloch 的风格创建 class。 But I want to use this class as DTO object and transfer it from EJB.
但我想将此 class 用作 DTO object 并从 EJB 传输。 So it need to have public no-arg constructor.
所以它需要有公共的无参数构造函数。 How I can achieve this java-bean style in builder pattern?
如何在构建器模式中实现这种 java-bean 样式? In Effective Java object have only one constructor which accepts
Builder
object as argument.在有效的 Java object 中只有一个构造函数接受
Builder
object 作为参数。 If I add there public no-arg constructor, then I lost advantage of builder over Java-bean pattern.如果我在那里添加公共无参数构造函数,那么我就失去了构建器优于 Java-bean 模式的优势。
Not sure what is making you to use builder pattern here but here is what my experience says to your issue. 不知道是什么让您使用构建器模式,但这是我的经验对您的问题的回答。
According to me, all you need is some kind of transformer which accepts obj1 from some layer for processing and then return it. 据我说,您所需要的只是某种转换器,该转换器从某一层接受obj1进行处理,然后将其返回。
I would recommend builder to be used only in case where your object construction is a multi-step process (for example, user registration). 我建议仅在您的对象构造是一个多步骤过程(例如,用户注册)的情况下使用builder。
If you use Lombok to create @Builder
implementation.如果您使用 Lombok 创建
@Builder
实现。 You can use @Jacksonized
annotation for serialization and deserialization.您可以使用
@Jacksonized
注解进行序列化和反序列化。 read more in the official docs在官方文档中阅读更多内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.