1
I have a class Builder who is responsible for returning an object he builds:
public class ObjetoBuilder {
private Objeto objeto;
public ObjetoBuilder() {
objeto = new Objeto();
}
public ObjetoBuilder adicionarId(Long id) {
if (id != null) objeto.setId(id);
return this;
}
public Objeto constroi() {
return objeto;
}
}
Doubt
It is good practice to have more than one method builds into one Builder? For example:
public class ObjetoBuilder {
...
public Objeto controi() {
return objeto;
}
public List<Objeto> constroiLista(){
return Arrays.asList(objeto);
}
public String constroiJson() {
return new Gson().toJson(objeto);
}
public String controiXml() {
return new XStream().toXML(objeto);
}
}
In case what would this class "helper" be? Is there a pattern where I can find these references?
– ayowole agbedejobi
In my view besides the object being built it is being serialized, in case receiving a plus, my question is whether this plus goes out of the standard or whether it can be done.
– ayowole agbedejobi
Yes, it comes out of the pattern. And yes, it can be done - each case is a case, patterns are guides to help, not to plaster the project. It is better to evaluate case by case.
– Leonardo Alves Machado
Let’s say you have 3 classes that you want to serialize in Json format. It is not good that you implement 3 equal (or almost equal) methods for the same function. Then, you can create an auxiliary class (helper) that contains a method
toJson
that takes any of the objects as a parameter and converts them to Json. So you don’t replicate bugs, simplify testing, decrease chances of failure...– Leonardo Alves Machado