I want to implement builder pattern the way joshua bloch says but I am also using jackson's objectMapper to convert convert one object to another, which internally using setter method to set the value.
Is it good practice to have both in one class ? or if not how can I achieve the best of both worlds ?
Both constructs are fulfilling different purposes.
- Builder pattern would be used by you if you are creating the object using a constructor.
- Jackson's ObjectMapper would be used when you are reading from json (you'd not be calling constructor)
Both of these are completely different use cases which are radically different in terms of usage. So I think they should not clash.
The only care that must be taken is that if you have implemented both, you should really try to refrain from using setters anywhere. You should stick to the builders only. So a well disciplined usage of both at the same time should not pose any problem according to me.
Hope this helps.
Good luck
here's a practice to copy one object from another:
public class Contact {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void clone(Contact c) {
setFirstName(c.getFirstName());
setLastName(c.getLastName());
}
}
I have never tried this but apparently jackson 2.x supports the builder pattern
http://wiki.fasterxml.com/JacksonFeatureBuilderPattern
@JsonDeserialize(builder=ValueBuilder.class)
public class Value {
private final int x, y;
protected Value(int x, int y) {
this.x = x;
this.y = y;
}
}
public class ValueBuilder {
private int x, y;
// can use @JsonCreator to use non-default ctor, inject values etc
public ValueBuilder() { }
// if name is "withXxx", works as is: otherwise use @JsonProperty("x") or @JsonSetter("x")!
public ValueBuilder withX(int x) {
this.x = x;
return this; // or, construct new instance, return that
}
public ValueBuilder withY(int y) {
this.y = y;
return this;
}
public Value build() {
return new Value(x, y);
}
}
I want to implement builder pattern the way joshua bloch says but I am also using jackson's objectMapper to convert convert one object to another, which internally using setter method to set the value.
Is it good practice to have both in one class ? or if not how can I achieve the best of both worlds ?
Both constructs are fulfilling different purposes.
- Builder pattern would be used by you if you are creating the object using a constructor.
- Jackson's ObjectMapper would be used when you are reading from json (you'd not be calling constructor)
Both of these are completely different use cases which are radically different in terms of usage. So I think they should not clash.
The only care that must be taken is that if you have implemented both, you should really try to refrain from using setters anywhere. You should stick to the builders only. So a well disciplined usage of both at the same time should not pose any problem according to me.
Hope this helps.
Good luck
here's a practice to copy one object from another:
public class Contact {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void clone(Contact c) {
setFirstName(c.getFirstName());
setLastName(c.getLastName());
}
}
I have never tried this but apparently jackson 2.x supports the builder pattern
http://wiki.fasterxml.com/JacksonFeatureBuilderPattern
@JsonDeserialize(builder=ValueBuilder.class)
public class Value {
private final int x, y;
protected Value(int x, int y) {
this.x = x;
this.y = y;
}
}
public class ValueBuilder {
private int x, y;
// can use @JsonCreator to use non-default ctor, inject values etc
public ValueBuilder() { }
// if name is "withXxx", works as is: otherwise use @JsonProperty("x") or @JsonSetter("x")!
public ValueBuilder withX(int x) {
this.x = x;
return this; // or, construct new instance, return that
}
public ValueBuilder withY(int y) {
this.y = y;
return this;
}
public Value build() {
return new Value(x, y);
}
}
0 commentaires:
Enregistrer un commentaire