Builder Pattern


Builder mode

The Builder Pattern uses a number of simple objects to build a complex object step by step. This type of design pattern is a creative model that provides the best 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 build from its representation so that the same build process can create different representations.

Main solution: Mainly solved in the software system, sometimes faced with the creation of "a complex object", which is usually composed of sub-objects of various parts with certain algorithms; Changes, the various parts of this complex object often face dramatic changes, but the algorithm that combines them is relatively stable.

When to use: Some basic components don't change, and their combinations often change.

How to solve: Separate from constant.

Key Code: Builder: Create and provide instances, Director: Manage dependencies for built instances.

Application example: 1, go to KFC, hamburger, cola, French fries, fried chicken wings, etc. are unchanged, and the combination is constantly changing, resulting in the so-called "package." 2, StringBuilder in JAVA.

Advantages: 1. The builder is independent and easy to expand. 2, easy to control the details of the risk.

Disadvantages: 1. Products must have something in common and the scope is limited. 2. If the internal changes are complicated, 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 depend on each other.

Note: The difference from the factory pattern is that the builder pattern pays more attention to the order in which the parts are assembled.

Implementation

We assume a business case for a fast food restaurant, where a typical package can be a Burger and a Cold drink. The Burger can be a Veg Burger or a Chicken Burger, which is wrapped in a carton. Cold drinks can be coke or pepsi, which are contained in bottles.

We will create an Item interface that represents food items (such as burgers and cold drinks) and an entity class that implements the Item interface, and a that represents the food package. The Packing interface and the entity class that implements the Packing interface, the hamburger is wrapped in a carton and the cold drink is contained in a bottle.

Then we create an Meal class, ArrayList with Item and a create by combining Item MealBuilder for different types of Meal objects. BuilderPatternDemo, our demo class uses MealBuilder to create an Meal.

builder_pattern uml diagram

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 an entity class that implements the Packing interface.

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"; } }

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 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"; } }

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

Pepsi.java
public class Pepsi extends ColdDrink { @ Override public float price ( ) { return 35 . 0 f ; } @ Override public String name ( ) { return " Pepsi " ; } }

Meal.java
import java . util . < span class = "hl-identifier"> ArrayList ; import java . util . List ; public class Meal { private List Item & gt; items = new < span class = "hl-code"> ArrayList & lt; Item & gt; ( ) ; public void addItem ( Item item ) { items . add < span class = "hl-brackets"> ( item ) ; } public float getCost ( ) { float cost = 0 . 0 f ; for (






javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition