builder pattern
Builder Pattern uses multiple simple objects to build a complex object step by step. This type of design pattern is a creational pattern, which provides an optimal way to create objects.
A Builder class will construct the final object step by step. The Builder class is independent of other objects.
Introduction
Intent: Separate a complex construction from its representation, so that the same construction process can create different representations.
Main solution: Main solution: In software systems, sometimes we are faced with the creation of "a complex object", which is usually composed of sub-objects of each part using a certain algorithm; due to The various parts of this complex object often face drastic changes as requirements change, but the algorithms that put them together are relatively stable.
When to use: When some basic components will not change, but their combinations often change.
How to solve: Separate change from immutability.
Key code: Builder: creates and provides instances, director: manages the dependencies of built instances.
Application examples: 1. Go to KFC, burgers, Coke, French fries, fried chicken wings, etc. are the same, but their combinations change frequently, generating a so-called "set meal" . 2. StringBuilder in JAVA.
Advantages: 1. The builder is independent and easy to expand. 2. Easily control detailed risks.
Disadvantages: 1. The products must have something in common and the scope is limited. 2. If the internal changes are complex, there will be many construction classes.
Usage scenarios: 1. The object that needs to be generated has a complex internal structure. 2. The internal properties of the objects that need to be generated are interdependent.
Note: The difference from factory mode is that builder mode pays more attention to the order of parts assembly.
Implementation
We assume a business case of a fast food restaurant, in which a typical set meal can be a burger (Burger) and a cold drink (Cold drink). Burgers can be vegetarian burgers or chicken burgers, and they are packaged in cartons. Cold drinks can be Coke or Pepsi, which come in bottles.
We will create a Item interface that represents food items (such as burgers and cold drinks) and an entity class that implements the Item interface, as well as a ## that represents food packaging #Packing Interface and entity class that implements Packing interface, hamburgers are packaged in cartons, and cold drinks are packaged in bottles.
Then we create aMeal class with an ArrayList of Item and a different one created by combining the Item A Meal object of type MealBuilder. BuilderPatternDemo, our demo class uses MealBuilder to create a Meal.
Step 1
Create an interface that represents food items and food packaging.
Item.java
public interface Item { public String name(); public Packing packing(); public float price(); }
Packing.java
public interface Packing { public String pack(); }
Step 2
Create the Packing interface entity class.
Wrapper.java
public class Wrapper implements Packing { @Override public String pack() { return "Wrapper"; } }
Bottle.java
public class Bottle implements Packing { @Override public String pack() { return "Bottle"; } }
Step 3
Create the Item interface An abstract class that provides default functionality.
Burger.java
public abstract class Burger implements Item { @Override public Packing packing() { return new Wrapper(); } @Override public abstract float price(); }
ColdDrink.java
public abstract class ColdDrink implements Item { @Override public Packing packing() { return new Bottle(); } @Override public abstract float price(); }
Step 4
Create an extension of Burger and ColdDrink's entity class.
VegBurger.java
public class VegBurger extends Burger { @Override public float price() { return 25.0f; } @Override public String name() { return "Veg Burger"; } }
ChickenBurger.java
public class ChickenBurger extends Burger { @Override public float price() { return 50.5f; } @Override public String name() { return "Chicken Burger"; } }
Coke.java
public class Coke extends ColdDrink { @Override public float price() { return 30.0f; } @Override public String name() { return "Coke"; } }
Pepsi.java
public class Pepsi extends ColdDrink { @Override public float price() { return 35.0f; } @Override public String name() { return "Pepsi"; } }
Step 5
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()); } } }
Step 6
Create a MealBuilder class. The actual builder class is responsible for creating Meal objects.
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; } }
Step 7
BuiderPatternDemo Use MealBuider to demonstrate the builder pattern (Builder Pattern).
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()); } }
Step 8
Verify the output.
Veg Meal Item : Veg Burger, Packing : Wrapper, Price : 25.0 Item : Coke, Packing : Bottle, Price : 30.0 Total Cost: 55.0 Non-Veg Meal Item : Chicken Burger, Packing : Wrapper, Price : 50.5 Item : Pepsi, Packing : Bottle, Price : 35.0 Total Cost: 85.5