我现有的模型类总是使用这样的构建器模式:
public class Model {
public static class Builder {
private boolean isValid;
private List<String> errorMessagesOrNull;
public Builder setIsValid(final boolean isValid) {
this.isValid = isValid;
return this;
}
public Builder setErrorMessages(final List<String> errorMessages) {
this.errorMessagesOrNull = errorMessages;
return this;
}
public List<String> getErrorMessages() {
return this.errorMessagesOrNull == null ? new ArrayList<>() : this.errorMessagesOrNull;
}
public Model Build() {
return new Model(this);
}
}
private boolean isValid;
private List<String> errorMessages;
private Model(final Builder builder) {
this.isValid = builder.isValid;
this.errorMessages = builder.getErrorMessages();
}
public boolean getIsValid() {
return isValid;
}
public List<String> getErrorMessages() {
return errorMessages;
}
}
如您所见,模型类始终具有isValid
和errorMessages
。我想编写一个抽象类来最小化这些模型类的重复逻辑。
所以我想起了这个抽象类:
public abstract class AbstractModel<T extends AbstractModel<T>> {
public static abstract class Builder<T> {
private boolean isValid;
private List<String> errorMessagesOrNull;
public Builder<T> setIsValid(final boolean isValid) {
this.isValid = isValid;
return this;
}
public Builder<T> setErrorMessages(final List<String> errorMessages) {
this.errorMessagesOrNull = errorMessages;
return this;
}
public List<String> getErrorMessages() {
return this.errorMessagesOrNull == null ? new ArrayList<>() : this.errorMessagesOrNull;
}
public abstract T Build();
}
private boolean isValid;
private List<String> errorMessages;
private AbstractModel(final Builder<T> builder) {
this.isValid = builder.isValid;
this.errorMessages = builder.getErrorMessages();
}
public boolean getIsValid() {
return isValid;
}
public List<String> getErrorMessages() {
return errorMessages;
}
}
但它并没有像我预期的那样真正起作用。当我扩展抽象类时:
public class Model extends AbstractModel<Model> {
// Empty here since all fields are extended
}
我做不了类似的事情:
Model model = new Model.Builder.setIsValid(true).Build();
我希望抽象类具有Builder
静态类,这样我每次都不需要编写静态类Builder
。
请告知。
答案 0 :(得分:2)
我认为你的逻辑存在巨大的缺陷。程序本身根本没有任何意义。为什么要首先使用Model
类构建Builder
?我认为最好向您展示您应该如何编写您的程序,而不是仅仅“躲避”它。好吧,让我们从Model
类开始。
假设没有Model
就无法构建Builder
类。那么将Builder
类添加到Model
类中是否有意义?简短的回答:不,它不会。相反,Builder
类应该包含Model
类作为非静态内部类。
/**
* The {@code Builder} can construct new instances of the {@code Model} class.
*
* @see Model
*/
public class Builder
{
private final String[] log;
/**
* The {@code Model} class can do something. You can only construct it through a {@code Builder}.
*
* @see Builder
*/
public class Model
{
private final Builder builder;
/**
* Constructs a new {@code Model} with the specified argument.
*
* @param builder the {@code Builder} that constructed the model.
*/
public Model(final Builder builder)
{
this.builder = builder;
}
/**
* Returns the associated {@code Builder}.
*
* @return the builder that constructed the model.
*/
public Builder getBuilder()
{
return this.builder;
}
}
/**
* Constructs a new instance of the {@code Builder} class with the specified argument.
*
* @param log the log of the {@code Builder}.
*/
public Builder(final String... log)
{
this.log = log;
}
/**
* Tries to {@code build} a new instance of the {@code Model} class.
*
* @return the constructed {@code Model}.
*/
public Model build()
{
return new Model(this);
}
/**
* Returns the log of the {@code Builder}.
*
* @return an log.
*/
public String[] getLog()
{
return this.log;
}
/**
* Determines whether or not the {@code Builder} is valid.
*
* @return {@code true} when the specified {@code log} is not {@code null}; {@code false} otherwise.
*/
public boolean isValid()
{
return this.log != null;
}
}
Builder
以外的任何课程都不能构建Model
。但是,如果您构造Builder
类的新实例并获得调用build
方法的结果,则您将可以访问所有public
变量和方法。
如果您知道要构建Model
,您可以这样做:
Builder.Model model = new Builder().build();
如果您不想要Builder.
前缀,只需添加导入Model
类的导入语句。
import organisation.projectname.pathToBuilder.Builder.Model;