嗨,您好!
今天,我将分享我制作的全新设计模式系列的第一个。 构建器设计模式是开发严肃的应用程序时非常有用且通用的模式。 在这篇文章中,我将提供一个很小的构建器模式框架,因此您随时可以回到这里并使用它。
助记符在处理构建器模式时会考虑自定义。 当我确定应该使用它还是最好去工厂时,我总是会考虑它。 这样对我更好。 尝试一下。
UML
这是小框架的外观。 简单,美观和直接。
背后的代码
该代码也非常简单,小巧,简洁且易于解释。 我喜欢编码表达力强的代码,因此无需过多评论。 在此示例中,我这样做了,因为它具有教程角色。 在开发过程中,我为自己建立了一些约定。 我认为这样做非常重要。 就像在开发时在语言上应用正确的语法一样。
例如:如果我使用的是Builder模式,我总是将后缀Builder放在最后。 好吧,您现在可以说或想:什么? 但这实际上对于初学者和经验丰富的开发人员来说是非常非常重要的信息。 他们将自动看到其背后的想法,并尝试不破坏其模式。 实际上,经验丰富的开发人员会喜欢它并尝试继续进行出色的工作,因为他们知道编写此内容的开发人员知道他的所作所为,并且肯定有原因。
因此,请始终保持足够的清晰,并在正确的位置提供正确的信息。 稍后会有其他人感谢您。 但是现在到代码了...
// 1. EXAMPLE: PARTS OF THE CUSTOMIZABLE PRODUCT WE WANT
public interface Part {// DEFINE THE METHODS YOUR PARTS WILL HAVE...void anyMethodNameYouLike();
}// 2. THE BUILDER METHOD WILL ADD
// PARTS RETURNING THE BUILDER ITSELF
public interface BuildContract < B > {B mount(Part part);
}// 3. DEFINE THE BUILDER'S CONTRUCTION METHOD
// WHICH BUILDS AND RETURNS THE FINAL PRODUCT "T"
public interface Builder < T > extends BuildContract < Builder < T > > {T build();
}
一个真实的例子
没有什么比修复和更好地理解它更好的了。 让我们建立一个蛋糕面包店。 您的一位同事想开一家面包店,并要求您为他编写面包店的软件。 我们开始做吧..!
顺便说一句,我衷心地向您推荐,使用UML图表工具是一种可视化机制,可以显示您的想法并提高您的设计技能。 让我们从UML开始:
比喻
现在,让我们使用我们的微型框架,对我们的面包店进行类比。 成分是零件,配方是BuilderContract,而Builder是建造者本身。 Cake是最终的可定制产品。 CakeBuilder是在定制后实际创建产品的类(添加所需数量的零件-成分后)。 客户将是最终客户或接受订单的您的同事。 只是使用或发挥想象力...现在让我们深入研究代码...
成分(部分)
我们示例中的部分是成分。 让我们实现一些成分,以后再用它来制作蛋糕。
// 1. EXAMPLE: PART TO CUSTOMIZATE "INGREDIENTS"
public interface Ingredient {// INGREDIENTS WILL HAVE...void printName();String getUnitPrice();void printCalories();
}
public class LightMilk implements Ingredient {private int deciLiter;private int calories;private String unitPrice;public LightMilk(int deciLiter){this.deciLiter=deciLiter;}public LightMilk(int deciLiter, int calories, String unitPrice) {super();this.deciLiter = deciLiter;this.calories = calories;this.unitPrice = unitPrice;}@Override public void printName() {System.out.printf(" Light Milk");}@Override public String getUnitPrice() {return unitPrice;}@Override public void printCalories() {System.out.printf(" 76kc");}public int getDeciLiter() {return deciLiter;}public void setDeciLiter(int deciLiter) {this.deciLiter = deciLiter;}public int getCalories() {return calories;}public void setCalories(int calories) {this.calories = calories;}public void setUnitPrice(String unitPrice) {this.unitPrice = unitPrice;}
}
public class Sugar implements Ingredient {private int gram;private int calories;private String unitPrice;public Sugar(int deciLiter){this.gram=deciLiter;}public Sugar(int gram, int calories, String unitPrice) {super();this.gram = gram;this.calories = calories;this.unitPrice = unitPrice;}@Override public void printName() {System.out.printf(" Sugar");}@Override public String getUnitPrice() {return unitPrice;}@Override public void printCalories() {System.out.printf(" 40kc");}public int getGram() {return gram;}public void setGram(int gram) {this.gram = gram;}public int getCalories() {return calories;}public void setCalories(int calories) {this.calories = calories;}public void setUnitPrice(String unitPrice) {this.unitPrice = unitPrice;}
}
public class Choco implements Ingredient {private int gram;private int calories;private String unitPrice;public Choco(int gram, int calories, String unitPrice) {super();this.gram = gram;this.calories = calories;this.unitPrice = unitPrice;}public int getGram() {return gram;}public void setGram(int gram) {this.gram = gram;}public int getCalories() {return calories;}public void setCalories(int calories) {this.calories = calories;}public void setUnitPrice(String unitPrice) {this.unitPrice = unitPrice;}@Override public void printName() {System.out.printf(" Chocolate");}@Override public void printCalories() {System.out.printf(" 389kc");}@Override public String getUnitPrice() {return unitPrice;}
}
public class NoSugar implements Ingredient {private int gram;private int calories;private String unitPrice;public NoSugar(int deciLiter){this.gram=deciLiter;}public NoSugar(int gram, int calories, String unitPrice) {super();this.gram = gram;this.calories = calories;this.unitPrice = unitPrice;}@Override public void printName() {System.out.printf(" No Sugar");}@Override public String getUnitPrice() {return unitPrice;}@Override public void printCalories() {System.out.printf(" 0kc");}public int getGram() {return gram;}public void setGram(int gram) {this.gram = gram;}public int getCalories() {return calories;}public void setCalories(int calories) {this.calories = calories;}public void setUnitPrice(String unitPrice) {this.unitPrice = unitPrice;}
}
public class Milk implements Ingredient {private int deciLiter;private int calories;private String unitPrice;public Milk(int deciLiter){this.deciLiter=deciLiter;}public Milk(int deciLiter, int calories, String unitPrice) {super();this.deciLiter = deciLiter;this.calories = calories;this.unitPrice = unitPrice;}@Override public void printName() {System.out.printf(" Milk");}@Override public String getUnitPrice() {return unitPrice;}@Override public void printCalories() {System.out.printf(" 128kc");}public int getDeciLiter() {return deciLiter;}public void setDeciLiter(int deciLiter) {this.deciLiter = deciLiter;}public int getCalories() {return calories;}public void setCalories(int calories) {this.calories = calories;}public void setUnitPrice(String unitPrice) {this.unitPrice = unitPrice;}
}
建筑商合同
这是我们示例中的食谱。
// 2. THE BUILDER METHOD WILL ADD
// INGREDIENTS RETURNING THE BUILDER ITSELF
public interface Recipe < B > {B addIngredient(Ingredient ingredient);
}
// 3. DEFINE THE BUILDER CONTRUCTION METHOD
// WHICH BUILDS AND RETURNS THE FINAL PRODUCT "T"
public interface Builder < T > extends Recipe < Builder < T > > {T build();
}
import java.util.ArrayList;
import java.util.List;
// 4. IMPLEMENT THE BUILDER ACC. TO YOUR NEEDS
public class CakeBuilder implements Builder < Cake > {// IN THIS CASE THE PARTS ARE THE INGREDIENTSprivate List < Ingredient > ingredients=new ArrayList < Ingredient > ( );@Overridepublic Cake build() {if(!ingredients.isEmpty()){// THE FINAL PRODUCT IS A CHOCO-MUFFINreturn new Cake(ingredients);}return new Cake(null);}@Override// BECAUSE I ALWAYS GET A BUILDER BACK, I'M ABLE TO// ADD A LOT OF PARTS BEFORE I CALL "BUILD()"public Builder < Cake > addIngredient(Ingredient ingredient) {if(ingredient!=null){ingredients.add(ingredient);}return this;}
}
产品
在我们的示例中,要构建的产品是蛋糕。
import java.util.List;public class Cake {public Cake(List < Ingredient > ingredients){String muffin = "";if(ingredients==null){System.out.println(" zero cake "+muffin);return;}// PRINT OUT MUFFIN INGREDIENTSSystem.out.printf(" Cake with: ");for (Ingredient ingredient : ingredients) {ingredient.printName();}// PRINT OUT PART PRICESfor (Ingredient ingredient : ingredients) {muffin+=" "+ingredient.getUnitPrice();//NOPMD}System.out.println(" - Price: "+muffin);}public void printResult(){System.out.println(" Cake is ready!");}
}
测试它
最后客户端测试。 在这里我们可以看到它的用法:
// 5. TESTING THE CHOCO-BUILDER
public class Client {public static void main(String[] args) {Builder < Cake > chocoMuffinBuilder = new CakeBuilder();chocoMuffinBuilder.addIngredient(new Choco(10, 23, "3.39"));chocoMuffinBuilder.addIngredient(new Milk(34, 67, "1.57"));chocoMuffinBuilder.addIngredient(new Sugar(34, 67, "2.00"));final Cake chocoMuffin = chocoMuffinBuilder.build();chocoMuffin.printResult();}
}
就这样! 希望你喜欢!
翻译自: https://www.javacodegeeks.com/2014/08/builder-design-pattern-applied.html