Created
February 7, 2018 04:27
-
-
Save rhari991/1253cfd2a10cad2dde1af4deeb286da9 to your computer and use it in GitHub Desktop.
An example of the builder pattern in Java
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 NewPasteParameters { | |
private final String text; | |
private final String title; | |
private final Format format; | |
private final Visibility visibility; | |
private final ExpiryDate expiryDate; | |
private NewPasteParameters(String text, String title, Format format, Visibility visibility, ExpiryDate expiryDate) { | |
this.text = text; | |
this.title = title; | |
this.format = format; | |
this.visibility = visibility; | |
this.expiryDate = expiryDate; | |
} | |
public String getText() { | |
return text; | |
} | |
public String getTitle() { | |
return title; | |
} | |
public Format getFormat() { | |
return format; | |
} | |
public Visibility getVisibility() { | |
return visibility; | |
} | |
public ExpiryDate getExpiryDate() { | |
return expiryDate; | |
} | |
public static final class Builder { | |
private String text; | |
private String title; | |
private Format format; | |
private Visibility visibility; | |
private ExpiryDate expiryDate; | |
public Builder(String text) { | |
if (text == null || text.trim().isEmpty()) { | |
throw new IllegalArgumentException("Paste text cannot be null or empty"); | |
} | |
this.text = text; | |
} | |
public Builder title(String title) { | |
this.title = title; | |
return this; | |
} | |
public Builder format(Format format) { | |
this.format = format; | |
return this; | |
} | |
public Builder visibility(Visibility visibility) { | |
this.visibility = visibility; | |
return this; | |
} | |
public Builder expiryDate(ExpiryDate expiryDate) { | |
this.expiryDate = expiryDate; | |
return this; | |
} | |
public NewPasteParameters build() { | |
return new NewPasteParameters(text, title, format, visibility, expiryDate); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment