最近我遇到了一个引起我兴趣的建筑师模式.
所以,我有一个构建实体的EntityBuilder,但它没有返回实体.这是方法签名:
public void build();
相反,在build()方法内部,它将创建的新对象Entity传递给CacheImplementation实例以存储它.
注意:CacheImpl是在构建器的构造函数中注入的.
public void build(){
//create new entity
cacheImplementation.add(entity);
}
这听起来像是最佳做法吗?
稍后编辑0
public interface EntityBuilder {
void setProperty0(PropertyObject propertyObject0);
void setProperty1(PropertyObject propertyObject1);
void setProperty2(PropertyObject propertyObject2);
//...
void build();
}
public class EntityBuilderImpl implements EntityBuilder {
PropertyObject propertyObject0;
PropertyObject propertyObject1;
PropertyObject propertyObject2;
//...
// setters for all properties
@Override
public void build(){
//create new entity
cacheImplementation.add(entity);
}
}
构建器按以下方式使用:
public class EntityProcessor{
private EntityBuilderFactory entityBuilderFactory;//initialized in constructor
void process(EntityDetails entityDetails){
EntityBuilder entityBuilder = this.entityBuilderFactory.getNewEntitytBuilder();
//..
// entityBuilder.set all properties from entityDetails
entityBuilder.build();
}
}
注意:cacheImpl实例仅将实体存储在List<>中.每N秒访问一次.
最佳答案
原文链接:https://www.f2er.com/java/437205.html