This is the 21st day of my participation in the Gwen Challenge.More article challenges

Preface:

In everyday life, we may encounter this type of requirement, for example, there are many attributes in an entity class, but we only want a few of them to combine into a new object. The Builder pattern provides the best way to create objects. Builder mode is easy to use, but when writing code, remember not to abuse design mode, otherwise it may backfire!

Builder model

Introduction: The Builder pattern is also called the generator pattern. This type of design pattern is the creation pattern, which uses multiple simple objects to build a complex object step by step. The construction of a complex object is separated from its representation, so that the same construction process can create different representations

Role: The Builder pattern is mainly used to solve the creation problem of complex objects in software systems. For example, the creation of complex objects is formed by the sub-objects of each part with certain algorithms. When the requirements change, complex objects face big changes, which is not conducive to the stability of the system. The builder is used to package the algorithms of each part.

In a particularly good example, let’s say we order at McDonald’s and we have the following items to order

  1. Spicy Chicken Burger, Orleans Chicken Burger, Angus Cow Burger
  2. Coke, orange juice, coffee
  3. French fries, chicken nuggets, chicken legs
  4. Spicy chicken burger + Coke + Fries =A set meal
  5. Orleans Drumstick, Coke and fries = combo B
  6. Angus burger + Coke + Fries = combo C
  7. .

3 * 3 * 3=27 entity classes to represent each entity class, which is not friendly. In this case, we can use the Builder pattern to optimize the above situation.

Code implementation:

Step 1: Define Product

/** * public class Meal {private String food; private String drink; public String getFood() { return food; } public void setFood(String food) { this.food = food; } public String getDrink() { return drink; } public void setDrink(String drink) { this.drink = drink; }}Copy the code

Step 2: Define the Builder: Create an abstract interface specified by the parts of the Product object

/ / public class MealBuilder {Meal = new Meal(); / / public class MealBuilder {Meal = new Meal(); public abstract void buildFood(); public abstract void buildDrink(); public Meal getMeal(){ return meal; }}Copy the code

Step 3: Define a ConcreteBuilder: Implement abstract interfaces that build and assemble parts.

Public class MealA extends MealBuilder{public void buildDrink() {matrice.setdrink (" coke "); } public void buildFood() {matrice.setfood (" fries "); }} public class MealB extends MealBuilder{public void buildDrink() {matrice.setdrink (); } public void buildFood() {matrice.setfood (" chicken wing "); }}Copy the code

Step 4, define the Director: Build an object that uses the Builder interface. It is primarily used to create a complex object. It mainly has two functions, one is: isolated the production process of the customer and the object, the other is: responsible for controlling the production process of the product object.

Public class KFCServer {private MealBuilder MealBuilder; Public KFCServer(MealBuilder MealBuilder) {this. MealBuilder = MealBuilder; } public Meal construct(){// Prepare food mealBuilder.buildfood (); // Prepare the drink mealBuilder.builddrink (); Return mealBuilder.getmeal (); return mealBuilder.getmeal (); }}Copy the code

Step 5: Test

Public class Test {public static void main(String[] args) {MealA a = new MealA(); // create package A object KFCServer waitera = new KFCServer(A); // mealA = waitera.construct(); System.out.print(" package A component :"); System.out.println(" food: "+ meala.getfood ()+"; "+ meala.getDrink () "); MealB b = new MealB(); KFCServer waiterb = new KFCServer(b); Meal mealB = waiterb.construct(); System.out.print(" components of package B :"); System.out.println(" food: "+ mealb.getFood ()+"; "+ mealb. getDrink()); }}Copy the code

Ok! This is the end of today’s article, I hope it can be helpful to you, there are wrong places I hope you can put forward, grow together;

Neat makes for great code, and there’s only so much detail