我无法搞清楚如何组织构造。
是构造函数应该采取所有的变量?
我通过调用构造函数,并通过它firstName
,pizzaSizeInInches
,pizzaType
,cheeseTopping
,pepperoniTopping
,sausageTopping
,onionTopping
,mushroomTopping
值创建PizzaOrder的一个实例。
public static double calculatePizzaCost() {
double cost = 12.99; //cost of the pizza
String toppings = ""; //list of toppings
int numberOfToppings = 0; //number of toppings
String crust = "";
final double TOPPING_PRICE = 1.25;
if (aPizzaOrder.getHandThinDeep() == 'h' || aPizzaOrder.getHandThinDeep() == 'H') {
crust = "Hand-Tossed";
} else if (aPizzaOrder.getHandThinDeep() == 't' || aPizzaOrder.getHandThinDeep() == 'T') {
crust = "Thin-Crust";
} else if (aPizzaOrder.getHandThinDeep() == 'd' || aPizzaOrder.getHandThinDeep() == 'D') {
crust = "Deep-Pan";
}
if (aPizzaOrder.getCheeseTopping()) {
numberOfToppings += 1;
toppings = toppings + "Additional Cheese ";
}
一般的经验法则是,构造函数应该接受没有它你的对象是没有意义的争论。对于如汽车是无用的没有引擎。所有其他属性它们是不错的,或添加更多的功能可以保留的属性。
除此之外,你还可以创建类的层次结构的每一个具有自己的构造检测哪些属性必须被强制初始化。例如。基类被声明为比萨它接受大小在构造函数中的参数。然后你就可以拥有从比萨类,它接受强制性的参数来构建Cheez比萨衍生Cheez比萨饼。
基本上,你可以考虑三种结构,以及它们的组合。 第一个当然是与变量一长串构造函数:
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 Pattern:
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();
}
}
对于这样的问题时,我们应该我们的一些设计模式,这里Builder Pattern
最适合如下。我希望这能帮到您
打包ITEM2;
public class PizzaOrder {
private String firstName;
private int pizzaSizeInInches;
//add other fields
private PizzaOrder(Builder builder){
this.firstName=builder.firstName;
this.pizzaSizeInInches=builder.pizzaSizeInInches;
//set other fields
}
public static class Builder{
private String firstName;
private int pizzaSizeInInches;
//add other fields
public Builder(){
}
public Builder firstName(String firstName){
this.firstName=firstName;
return this;
}
public Builder pizzaSizeInInches(int pizzaSizeInInches) {
this.pizzaSizeInInches = pizzaSizeInInches;
return this;
}
//add other setter method like above
public PizzaOrder build(){
return new PizzaOrder(this);
}
}
public static Builder builder(){
return new Builder();
}
public static void main(String args[]){
PizzaOrder.builder()
.firstName("sanjeev")
.pizzaSizeInInches(10)
.build();
}
}