This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging
The Builder Pattern uses multiple simple objects to build a complex object step by step. This type of design pattern is a creation pattern that provides an optimal way to create objects. A Builder class constructs the final object step by step. The Builder class is independent of other objects.
introduce
describe | parsing |
---|---|
intentions | Separating a complex build from its representation allows the same build process to create different representations. |
Mainly to solve | The main solution is in the software system, sometimes faced with the creation of “a complex object”, which is usually composed of various parts of the sub-objects with a certain algorithm; As demand changes, |
When to use | Some of the basic components do not change, and their composition often changes. |
How to solve | Separate change from invariance. |
The key code | The builders |
Examples of application | 1, go to KFC, hamburgers, coke, French fries, fried chicken wings and so on are unchanged, and its combination is often changed, resulting in the so-called “set meal”. 2. JAVA StringBuilder. |
advantages | 1, the builder is independent, easy to expand. 2, easy to control the details of risk. |
disadvantages | 1. Products must have something in common and have a limited scope. 2, if the internal changes are complex, there will be many build classes. |
Usage scenarios | 1. Objects that need to be generated have complex internal structures. 2. The internal properties of the objects that need to be generated depend on each other. |
Matters needing attention | The difference from the factory pattern is that the builder pattern is more concerned with the order in which parts are assembled. |
implementation
Consider the business case of a fast food restaurant, where a typical meal could be a Burger and a Cold drink. A Burger, “Burger,” can be a Veg Burger or a Chicken Burger, and they come in a carton. Cold drinks can be Coke (COKE) or Pepsi (Pepsi) and they come in bottles.
- We will create one that represents food items such as hamburgers and cold drinks
Item
Interface and ImplementationItem
Interface entity class, and a representation of food packagingPacking
Interface and ImplementationPacking
In the physical class of interfaces, burgers come in cartons and cold drinks come in bottles. - And then we create one
Meal
Class,Item
的ArrayList
And one through combinationItem
To create different types ofMeal
The object’sMealBuilder
.BuilderPatternDemo
Class USESMealBuilder
To create aMeal
.
Step 1- Create an interface
Create an interface that represents food items and food packaging.
// Item.java
public interface Item {
public String name();
public Packing packing();
public float price();
}
Copy the code
// Packing.java
public interface Packing {
public String pack();
}
Copy the code
Step 2- Implement the wrapper interface
Create an entity class that implements the Packing interface.
// Wrapper public class Wrapper implements Packing { @Override public String pack() { return "Wrapper"; }}Copy the code
// Bottle.java public class Bottle implements Packing { @Override public String pack() { return "Bottle"; }}Copy the code
Step 3- Create abstract classes that implement Item: Hamburger and cold drink
Create an abstract class that implements the Item interface and provides the default functionality.
// Burger.java public abstract class Burger implements Item { @Override public Packing packing() { return new Wrapper(); } @Override public abstract float price(); }Copy the code
// ColdDrink.java
public abstract class ColdDrink implements Item {
@Override
public Packing packing() {
return new Bottle();
}
@Override
public abstract float price();
}
Copy the code
Step 4- Create entity class
Create an entity class that extends Burger and ColdDrink.
// VegBurger. Java public class VegBurger extends Burger {@override public float price() {return 25.0f; } @Override public String name() { return "Veg Burger"; }}Copy the code
Java public class ChickenBurger extends Burger {@override public float price() {return 50.5f; } @Override public String name() { return "Chicken Burger"; }}Copy the code
// coke.java public class Coke extends ColdDrink {@override public float price() {return 30.0f; } @Override public String name() { return "Coke"; }}Copy the code
// Pepsi.java public class Pepsi extends ColdDrink {@override public float price() {return 35.0f; } @Override public String name() { return "Pepsi"; }}Copy the code
Step 5- Create the package class
Create a Meal class with the Item object defined above.
// Meal.java import java.util.ArrayList; import java.util.List; public class Meal { private List<Item> items = new ArrayList<Item>(); public void addItem(Item item){ items.add(item); } public float getCost(){float cost = 0.0f; for (Item item : items) { cost += item.price(); } return cost; } public void showItems(){ for (Item item : items) { System.out.print("Item : "+item.name()); System.out.print(", Packing : "+item.packing().pack()); System.out.println(", Price : "+item.price()); }}}Copy the code
Step 6- Set the scenario class
Create a MealBuilder class. The actual Builder class is responsible for creating the Meal object.
// MealBuilder.java public class MealBuilder { public Meal prepareVegMeal (){ Meal meal = new Meal(); meal.addItem(new VegBurger()); meal.addItem(new Coke()); return meal; } public Meal prepareNonVegMeal (){ Meal meal = new Meal(); meal.addItem(new ChickenBurger()); meal.addItem(new Pepsi()); return meal; }}Copy the code
Step 7- Call the scenario class
BuiderPatternDemo uses MealBuilder to demonstrate Builder patterns.
// BuilderPatternDemo.java public class BuilderPatternDemo { public static void main(String[] args) { MealBuilder mealBuilder = new MealBuilder(); Meal vegMeal = mealBuilder.prepareVegMeal(); System.out.println("Veg Meal"); vegMeal.showItems(); System.out.println("Total Cost: " +vegMeal.getCost()); Meal nonVegMeal = mealBuilder.prepareNonVegMeal(); System.out.println("\n\nNon-Veg Meal"); nonVegMeal.showItems(); System.out.println("Total Cost: " +nonVegMeal.getCost()); }}Copy the code
Step 8- Output
Execute the program and print the result:
Veg Meal Item: Veg Burger, Packing: Wrapper, Price: 25.0 Item: Coke, Packing: Bottle, Price: 30.0 55.0 Non-veg Meal Item: Chicken Burger, Packing: Wrapper, Price: 50.5 Item: Pepsi, Packing: Bottle, Price: 35.0 the Total Cost: 85.5Copy the code