Skip to content

Instantly share code, notes, and snippets.

@showsky
Created August 15, 2014 04:03
Show Gist options
  • Select an option

  • Save showsky/9b81a8e076a5b015adf8 to your computer and use it in GitHub Desktop.

Select an option

Save showsky/9b81a8e076a5b015adf8 to your computer and use it in GitHub Desktop.
JavaBean + builder pattern + telescoping constructor pattern
public class JavaBean {
private int channelID; // required
private String channelName; // required
private String channelDescription; // optional
private int channelCategory; // optional
public void setChannelID(int id) {
this.channelID = id;
}
public void setChannelName(String channelName) {
this.channelName = channelName;
}
public void setDescription(String channelDescription) {
this.channelDescription = channelDecription;
}
public void setChannelCategory(int category) {
this.channelCategory = category;
}
}
/* thread unsafe and not required */
JavaBean bean = new JavaBean();
bean.setChannelID(123);
bean.setChannelName("test");
bean.setDescription("test");
bean.setChannelCategory(1);
public class JavaBean {
private final int channelID; // required
private final String channelName; // required
private final String channelDescription; // optional
private final int channelCategory; // optional
public static class Builder {
private final int channelID;
private final int channelName;
private int channelDecription = "";
private int channelCategory = -1;
public Budiler(int channelID, String channelName) {
this.channelID = channelID;
this.channelName = channelName;
}
public Builder channelDescription(String channelDescription) {
this.channelDescription = channelDescription;
return this;
}
public Builder channelCategory(int channelCategory) {
this.channelCategory = channelCategory;
return this;
}
public JavaBean build() {
return new JavaBean(this);
}
}
private JavaBean(Builder builder) {
this.channelId = builder.channelID;
this.channelName = builder.channelName;
this.channelDescription = builder.channelDescription;
this.channelCategory = builder.channelCategory;
}
}
/* JavaBean + builder pattern + telescoping constructor pattern */
/* thread safe */
JavaBean bean = new Builder(123, "test")
.channelDescription("test")
.channelCategory(1)
.build();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment