В основном вы можете рассмотреть три структуры и их комбинации.Первая, конечно, конструктор с длинным списком переменных:
public class Pizza {
private String firstName, pizzaType;
private String cheeseTopping, pepperoniTopping, sausageTopping;
private int pizzaSizeInInches;
public Pizza(String firstName, String pizzaType, String cheeseTopping, String pepperoniTopping,
String sausageTopping, int pizzaSizeInInches) {
this.firstName = firstName;
this.pizzaType = pizzaType;
this.cheeseTopping = cheeseTopping;
this.pepperoniTopping = pepperoniTopping;
this.sausageTopping = sausageTopping;
this.pizzaSizeInInches = pizzaSizeInInches;
}
}
Вторая базовая структура использует setter
методы для длинного списка полей:
public class Pizza {
private String firstName, pizzaType;
private String cheeseTopping, pepperoniTopping, sausageTopping;
private int pizzaSizeInInches;
public Pizza() {};
String getFirstName() { return firstName;}
void setFirstName(String firstName) {this.firstName = firstName;}
String getPizzaType() { return pizzaType; }
void setPizzaType(String pizzaType) { this.pizzaType = pizzaType; }
String getCheeseTopping() { return cheeseTopping; }
void setCheeseTopping(String cheeseTopping) { this.cheeseTopping = cheeseTopping; }
String getPepperoniTopping() {return pepperoniTopping; }
void setPepperoniTopping(String pepperoniTopping) { this.pepperoniTopping = pepperoniTopping; }
String getSausageTopping() { return sausageTopping; }
void setSausageTopping(String sausageTopping) { this.sausageTopping = sausageTopping;}
int getPizzaSizeInInches() {return pizzaSizeInInches; }
void setPizzaSizeInInches(int pizzaSizeInInches) { this.pizzaSizeInInches = pizzaSizeInInches; }
}
Примечание: наличие методов setter
, возвращающих this
, например:
Pizza setCheeseTopping(String cheeseTopping) {
this.cheeseTopping = cheeseTopping;
return this;
}
Pizza setPepperoniTopping(String pepperoniTopping) {
this.pepperoniTopping = pepperoniTopping;
return this;
}
, упрощает вызов длинного списка setters
путем их цепочки: pizza.setCheeseTopping("Melt").setPepperoniTopping("Hot");
Разумная комбинация двух вышеупомянутых подходов состоит в том, чтобы конструктор инициализировал обязательные поля и использовал setters
для необязательных полей или полей со значением по умолчанию:
public class Pizza {
private String firstName, pizzaType;
private String cheeseTopping, pepperoniTopping, sausageTopping;
private int pizzaSizeInInches;
public Pizza(String firstName, String pizzaType) {
this.firstName = firstName;
this.pizzaType = pizzaType;
}
String getFirstName() { return firstName;}
String getPizzaType() { return pizzaType;}
String getCheeseTopping() { return cheeseTopping; }
Pizza setCheeseTopping(String cheeseTopping) {
this.cheeseTopping = cheeseTopping;
return this;
}
String getPepperoniTopping() {return pepperoniTopping; }
Pizza setPepperoniTopping(String pepperoniTopping) {
this.pepperoniTopping = pepperoniTopping;
return this;
}
String getSausageTopping() { return sausageTopping; }
Pizza setSausageTopping(String sausageTopping) {
this.sausageTopping = sausageTopping;
return this;
}
int getPizzaSizeInInches() {return pizzaSizeInInches; }
Pizza setPizzaSizeInInches(int pizzaSizeInInches) {
this.pizzaSizeInInches = pizzaSizeInInches;
return this;
}
}
Третий подход дляинициализация длинного списка переменных заключается в использовании шаблона Builder :
public class Pizza {
private String firstName, pizzaType, cheeseTopping, pepperoniTopping, sausageTopping;
private int pizzaSizeInInches;
public Pizza(PizzaBuilder builder) {
this.firstName = builder.firstName;
this.pizzaType = builder.pizzaType;
this.pizzaSizeInInches = builder.pizzaSizeInInches;
this.cheeseTopping = builder.cheeseTopping;
this.pepperoniTopping = builder.pepperoniTopping;
this.sausageTopping = builder.sausageTopping;
}
String getFirstName() { return firstName;}
String getPizzaType() { return pizzaType;}
String getCheeseTopping() { return cheeseTopping; }
String getPepperoniTopping() {return pepperoniTopping; }
String getSausageTopping() { return sausageTopping; }
int getPizzaSizeInInches() {return pizzaSizeInInches; }
static class PizzaBuilder {
private String firstName, pizzaType;
private String cheeseTopping, pepperoniTopping, sausageTopping;
private int pizzaSizeInInches;
PizzaBuilder setFirstName(String firstName) {
this.firstName = firstName;
return this;
}
PizzaBuilder setPizzaType(String pizzaType) {
this.pizzaType = pizzaType;
return this;
}
PizzaBuilder setPizzaSizeInInches(int pizzaSizeInInches) {
this.pizzaSizeInInches = pizzaSizeInInches;
return this;
}
PizzaBuilder setCheeseTopping(String cheeseTopping) {
this.cheeseTopping = cheeseTopping;
return this;
}
PizzaBuilder setPepperoniTopping(String pepperoniTopping) {
this.pepperoniTopping = pepperoniTopping;
return this;
}
PizzaBuilder setSausageTopping(String sausageTopping) {
this.sausageTopping = sausageTopping;
return this;
}
Pizza buid (){
return new Pizza(this);
}
}
//make new Pizza
public static void main(String[] args) {
Pizza pizza = new PizzaBuilder().
setFirstName("Jack").setPizzaType("Annanas").
setPizzaSizeInInches(10).setCheeseTopping("Melt").
setPepperoniTopping("Hot").setSausageTopping("None").
buid();
}
}