Skip to content

Instantly share code, notes, and snippets.

@m-tilab
Created January 25, 2023 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-tilab/ca5e5554ce096a090ca6d9c169e70e3b to your computer and use it in GitHub Desktop.
Save m-tilab/ca5e5554ce096a090ca6d9c169e70e3b to your computer and use it in GitHub Desktop.
public class SearchService {
/* Parameter Object example. Default values are abstracted into the Parameter Object
at the time of Object creation */
public String search(ParameterObject parameterObject) {
return getQuerySummary(parameterObject.getType(), parameterObject.getSortBy(),
parameterObject.getSortOrder());
}
private String getQuerySummary(String type, String sortBy, SortOrder sortOrder) {
return "Requesting shoes of type \"" + type + "\" sorted by \"" + sortBy + "\" in \""
+ sortOrder.getValue() + "ending\" order...";
}
}
public class ParameterObject {
public static final String DEFAULT_SORT_BY = "price";
public static final SortOrder DEFAULT_SORT_ORDER = SortOrder.ASC;
private String type;
private String sortBy = DEFAULT_SORT_BY;
private SortOrder sortOrder = DEFAULT_SORT_ORDER;
private ParameterObject(Builder builder) {
type = builder.type;
sortBy = builder.sortBy != null && !builder.sortBy.isBlank() ? builder.sortBy : sortBy;
sortOrder = builder.sortOrder != null ? builder.sortOrder : sortOrder;
}
public static Builder newBuilder() {
return new Builder();
}
//Getters and Setters...
public static final class Builder {
private String type;
private String sortBy;
private SortOrder sortOrder;
private Builder() {
}
public Builder withType(String type) {
this.type = type;
return this;
}
public Builder sortBy(String sortBy) {
this.sortBy = sortBy;
return this;
}
public Builder sortOrder(SortOrder sortOrder) {
this.sortOrder = sortOrder;
return this;
}
public ParameterObject build() {
return new ParameterObject(this);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment