This piece of code is a mix of command and builder patterns:
CommandBuilder.create("one value").execute();
CommandBuilder.create("another value").withBigDecimal(BigDecimal.ZERO).withNumber(123).execute();
all it does is a simple printing:
one value
another value 123 0
The goal is to get rid of the execute()
method. Which means that after last withXXX()
method ( or without any withXXX()
) the code would do the printing itself.
Here is the current code:
import java.math.BigDecimal;
public class CommandBuilder {
private final String value;
private Integer number;
private BigDecimal bigDecimal;
private CommandBuilder(String value) {
this.value = value;
}
public static CommandBuilder create(String value){
return new CommandBuilder(value);
}
public CommandBuilder withNumber(Integer number){
this.number = number;
return this;
}
public CommandBuilder withBigDecimal(BigDecimal bigDecimal){
this.bigDecimal = bigDecimal;
return this;
}
public void execute(){
StringBuilder sb = new StringBuilder();
sb.append(value);
if(number != null){
sb.append(" ").append(number);
}
if(bigDecimal != null){
sb.append(" ").append(bigDecimal);
}
System.out.println(sb);
}
}
Is this possible in java?
Aucun commentaire:
Enregistrer un commentaire