Skip to content

Instantly share code, notes, and snippets.

@smat
Created September 20, 2011 07:47
Show Gist options
  • Save smat/1228584 to your computer and use it in GitHub Desktop.
Save smat/1228584 to your computer and use it in GitHub Desktop.
Builder-pattern
public class Test {
private String text;
public String getText() {
return text;
}
public class Builder {
boolean isBuilt = false;
public Builder withText(String text) {
Test.this.text = text;
return this;
}
public Test build() {
if (isBuilt) {
throw new IllegalArgumentException("Can only be built once");
}
isBuilt = true;
return Test.this;
}
}
public static Builder getBuilder() {
return new Test().new Builder();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment