我现在已经修了两门“设计模式”课程。 1在下面具有此模式(请注意缺少接口)。
class Coffee{
private String name;
private int acidity;
private int sweetness;
private String roastType;
private int aromatic;
private String quality;
private int priceInDollarsPer100g;
public Coffee(Builder builder) {
this.name = builder.name;
this.acidity = builder.acidity;
this.sweetness = builder.sweetness;
this.roastType = builder.roastType;
this.aromatic = builder.aromatic;
this.quality = builder.quality;
this.priceInDollarsPer100g = builder.priceInDollarsPer100g;
}
public static class Builder{
private Builder builder;
private String name;
private int acidity;
private int sweetness;
private String roastType;
private int aromatic;
private String quality;
private int priceInDollarsPer100g;
// Only 1 field is mandatory
public Builder(String name){
this.name = name;
}
public Builder setAcidity(int acidity){
this.acidity = acidity;
return this;
}
public Builder setSweetness(int sweetness) {
this.sweetness = sweetness;
return this;
}
public Builder setRoastType(String roastType) {
this.roastType = roastType;
return this;
}
public Builder setAromatic(int aromatic) {
this.aromatic = aromatic;
return this;
}
public Builder setQuality(String quality) {
this.quality = quality;
return this;
}
public Builder setPriceInDollarsPer100g(int priceInDollarsPer100g) {
this.priceInDollarsPer100g = priceInDollarsPer100g;
return this;
}
public Coffee build(){
return new Coffee(this);
}
}
}
但是在接下来的课程中,完全没有提及这种模式。如果我将其调整为“界面/说明性/静态乘积方式”,则会得到以下信息:
package Builder.BuilderPattern2;
interface CoffeeAttributes {
void setName(String name);
void setAcidity(int acidity);
void setSweetness(int sweetness);
void setRoastType(String roastType);
void setAromatic(int aromatic);
void setQuality(String quality);
void setPriceInDollarsPer100g(int priceInDollarsPer100g);
}
class Coffee implements CoffeeAttributes {
private String name;
private int acidity;
private int sweetness;
private String roastType;
private int aromatic;
private String quality;
private int priceInDollarsPer100g;
public void setName(String name) {
this.name = name;
}
public void setAcidity(int acidity) {
this.acidity = acidity;
}
public void setSweetness(int sweetness) {
this.sweetness = sweetness;
}
public void setRoastType(String roastType) {
this.roastType = roastType;
}
public void setAromatic(int aromatic) {
this.aromatic = aromatic;
}
public void setQuality(String quality) {
this.quality = quality;
}
public void setPriceInDollarsPer100g(int priceInDollarsPer100g) {
this.priceInDollarsPer100g = priceInDollarsPer100g;
}
}
interface CoffeeBuilder{
public void buildName();
public void buildAcidity();
public void buildSweetness();
public void buildRoastType();
public void buildAromatic();
public void buildQuality();
public void buildPriceInDollarsPer100g();
public Coffee getCoffee();
}
class ZestyFrenchRoast implements CoffeeBuilder{
private Coffee coffee;
public ZestyFrenchRoast(){
this.coffee = new Coffee();
}
public void buildName() {
this.coffee.setName("Zesty French Flave");
}
public void buildAcidity() {
this.coffee.setAcidity(3);
}
public void buildSweetness() {
this.coffee.setSweetness(10);
}
public void buildRoastType() {
this.coffee.setRoastType("French");
}
public void buildAromatic() {
this.coffee.setAromatic(7);
}
public void buildQuality() {
this.coffee.setQuality("Medium");
}
public void buildPriceInDollarsPer100g() {
this.coffee.setPriceInDollarsPer100g(3);
}
public Coffee getCoffee() {
return this.coffee;
}
}
class CoffeeMaker{
private CoffeeBuilder coffeeBuilder;
public CoffeeMaker(CoffeeBuilder coffeeBuilder){
this.coffeeBuilder = coffeeBuilder;
}
public Coffee getCoffee(){
return this.coffeeBuilder.getCoffee();
}
public void makeCoffee(){
this.coffeeBuilder.buildName();
this.coffeeBuilder.buildAcidity();
this.coffeeBuilder.buildAromatic();
this.coffeeBuilder.buildPriceInDollarsPer100g();
this.coffeeBuilder.buildQuality();
this.coffeeBuilder.buildRoastType();
this.coffeeBuilder.buildSweetness();
}
}
public class BuilderPattern2{
public static void main(String[] args) {
CoffeeBuilder coffeeToBuild = new ZestyFrenchRoast();
CoffeeMaker coffeeMaker = new CoffeeMaker(coffeeToBuild);
coffeeMaker.makeCoffee();
Coffee coffee = coffeeMaker.getCoffee();
System.out.println("The barista hands you a delicious cup of some kind of " +
"coffee but because there are no getters in this pattern we have no idea " +
"what it is. " + "Please pick it up at " + coffee);
}
}
RE:https://www.geeksforgeeks.org/builder-design-pattern/
第二个版本中没有吸气剂,因此显然您不会在对象中放置文本,这将是一个抽象的东西。我不知所措地看到这两种模式具有相同的名称!
答案 0 :(得分:0)
第一个示例显示了基本的Builder模式。它可以用来避免创建多个构造函数。如果有时您需要添加姓名和学生证,或者有时您需要添加姓名,地址和电话号码,那么单个对象生成器对此非常有用。
第二个示例是 Factory Builder ! 它可以用来为不同类型的用户定义特权。这些特权通常不会更改,但是在您拥有的用户类型中会有所不同。那就是工厂进来的地方。 基本上,它是用于创建PRODUCT的,并且该产品不会经常更改。