// Product class
class Pizza {
private String dough;
private String sauce;
private String topping;
// Private constructor to enforce the use of Builder
private Pizza(PizzaBuilder builder) {
this.dough = builder.dough;
this.sauce = builder.sauce;
this.topping = builder.topping;
}
@Override
public String toString() {
return "Pizza with " + dough + " dough, "
+ sauce + " sauce, and " + topping + " topping.";
}
public static class PizzaBuilder {
private String dough;
private String sauce;
private String topping;
public PizzaBuilder dough(String dough) {
this.dough = dough;
return this;
}
public PizzaBuilder sauce(String sauce) {
this.sauce = sauce;
return this;
}
public PizzaBuilder topping(String topping) {
this.topping = topping;
return this;
}
public Pizza build() {
return new Pizza(this);
}
}
}
public class Main {
public static void main(String[] args) {
Pizza myPizza = new Pizza.PizzaBuilder()
.dough("Thin Crust")
.sauce("Tomato")
.topping("Cheese")
.build();
System.out.println(myPizza);
String orderType = "Veggie";
Pizza.PizzaBuilder pizzaBuilder
= new Pizza.PizzaBuilder().dough("Regular");
pizzaBuilder.sauce("Pesto");
if (orderType.equals("Veggie")) {
pizzaBuilder.topping("Vegetables");
} else {
pizzaBuilder.topping("Pepperoni");
}
Pizza customPizza = pizzaBuilder.build();
System.out.println(customPizza);
}
}
Pizza with Thin Crust dough, Tomato sauce, and Cheese topping.
Pizza with Regular dough, Pesto sauce, and Vegetables topping.
import java.util.HashMap;
import java.util.Map;
// Product class
public class HttpRequest {
private String method;
private String url;
private Map<String, String> headers;
private Map<String, String> parameters;
private String body;
// private constructor
private HttpRequest(Builder builder) {
this.method = builder.method;
this.url = builder.url;
this.headers = builder.headers;
this.parameters = builder.parameters;
this.body = builder.body;
}
@Override
public String toString() {
return "HttpRequest [method=" + method + ", url=" + url +
", headers=" + headers + ", parameters=" + parameters +
", body=" + body + "]";
}
public static class Builder {
private String method;
private String url;
private Map<String, String> headers = new HashMap<>();
private Map<String, String> parameters = new HashMap<>();
private String body;
public Builder(String method, String url) {
this.method = method;
this.url = url;
}
public Builder addHeader(String key, String value) {
this.headers.put(key, value);
return this; }
public Builder addParameter(String key, String value) {
this.parameters.put(key, value);
return this; }
public Builder setBody(String body) {
this.body = body;
return this; }
public HttpRequest build() {
return new HttpRequest(this); }
}
}
public class Main {
public static void main(String[] args) {
HttpRequest getRequest = new HttpRequest.Builder(
"GET", "<https://example.com/api>")
.addHeader("Authorization", "Bearer token")
.addParameter("query", "builder-pattern")
.build();
HttpRequest postRequest = new HttpRequest.Builder(
"POST", "<https://example.com/api>")
.addHeader("Authorization", "Bearer token")
.setBody("{ \\"name\\": \\"John\\", \\"age\\": 30 }")
.build();
System.out.println(getRequest);
System.out.println(postRequest);
}
}
HttpRequest [method=GET, url=https://example.com/api,
headers={Authorization=Bearer token}, parameters={query=builder-pattern},
body=null]
HttpRequest [method=POST, url=https://example.com/api,
headers={Authorization=Bearer token}, parameters={},
body={ "name": "John", "age": 30 }]