Created
September 20, 2011 07:47
-
-
Save smat/1228584 to your computer and use it in GitHub Desktop.
Builder-pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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