我正在尝试使用 Jersey Java 创建一个不可变对象。但我不知道默认构造函数在大型应用程序中是否有用。有人可以指导我吗?
这是我的课:-
package com.jersey.jaxb;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.pojomatic.Pojomatic;
import org.pojomatic.annotations.AutoProperty;
@XmlRootElement
@XmlType(name="todo")
@XmlAccessorType(XmlAccessType.FIELD)
@AutoProperty
public class Todo {
@XmlElement(name="summary")
private final String summary;
@XmlElement(name="description")
private final String description;
public String getSummary() {
return summary;
}
public String getDescription() {
return description;
}
private Todo(){
this(new Builder());
}
private Todo(Builder builder){
this.summary = builder.summary;
this.description = builder.description;
}
@Override
public boolean equals(Object o) {
return Pojomatic.equals(this, o);
}
@Override
public int hashCode() {
return Pojomatic.hashCode(this);
}
@Override
public String toString() {
return Pojomatic.toString(this);
}
public static class Builder{
private String description;
private String summary;
public Builder summary(String summary){
this.summary = summary;
return this;
}
public Builder description(String description){
this.description = description;
return this;
}
public Todo build(){
return new Todo(this);
}
}
}
默认构造函数也是如此:-
private Todo(){
this(new Builder());
}
帮助序列化/反序列化或根本没有用。
我不知道它在大型应用程序中会有什么影响,或者我不应该全部使用它。