Concept: Separates the construction of a complex object from its representation so that the same construction process can create different representations.
The concept is a bit abstract and not very easy to understand.Below is an example of an individual's understanding of the builder pattern.
Milk tea shops, for example, sell all kinds of milk tea and drinks, such as red beans, authentic, chocolate. The steps for making milk tea are similar, starting with ingredients, adding sugar, boiling water and small milk tea shops.
Completed by a milk tea mm.Large milk tea shops have milk tea mm for all kinds of flavors. We go to buy milk tea and just tell the cashier what flavor to have and pay for it.The builder mode is for the ingredients needed to make milk tea and
The process separates and the milk tea mm is the builder.
The code is as follows:
package com.tc.gof.builder; /** * Abstract milk tea needs three steps to complete the process of placing ingredients, adding sugar and boiling water * */ public abstract class Tea { // First step add ingredients protected abstract void addMaterial(); // Step 2 Adding Sugar protected abstract void addSugar(); // Step 3 Add boiling water protected abstract void addWater(); /** * Default steps * @return */ public final Tea defaultTea(){ <span style="line-height: 21.6px; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;">addMaterial</span><span style="line-height: 21.6px; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;">();</span> <span style="line-height: 21.6px; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;">addSugar</span><span style="line-height: 21.6px; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;">();</span> <span style="line-height: 21.6px; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;">addWater</span><span style="line-height: 21.6px; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;">();</span> return this; } }
package com.tc.gof.builder; /** * Red bean milk tea * */ public class RedBeanTea extends Tea { @Override protected void addMaterial() { System.out.println("Add red beans"); } @Override protected void addSugar() { System.out.println("Adding sugar"); } @Override protected void addWater() { System.out.println("water"); } }
package com.tc.gof.builder; /** *Strawberry-flavored milk tea * */ public class StrawberryTea extends Tea { @Override protected void addMaterial() { System.out.println("Add strawberry ingredients"); } @Override protected void addSugar() { System.out.println("Adding sugar"); } @Override protected void addWater() { System.out.println("water"); } }
package com.tc.gof.builder; /** * mm for making black bean milk tea * */ public class RedBeanBuilder extends AbstractBuilder { private Tea tea = new RedBeanTea(); @Override public void buildPart() { // Add ingredients tea.addMaterial(); // Adding sugar tea.addSugar(); // water tea.addWater(); } @Override public Tea buildProduct() { System.out.println("Done with red bean flavor"); return tea; } }
package com.tc.gof.builder; /** * mm for making strawberry-flavored milk tea * */ public class StrawberryBuilder extends AbstractBuilder { private Tea tea = new StrawberryTea(); @Override public void buildPart() { // Add ingredients tea.addMaterial(); // Add water here first tea.addWater(); // Ask the customer if they want sugar tea.addSugar(); } @Override public Tea buildProduct() { System.out.println("Strawberry flavor done"); return tea; } }
package com.tc.gof.builder; /** * Cashier * */ public class Cashier { // Two milk tea mm private AbstractBuilder redBeanBuilder = new RedBeanBuilder(); private AbstractBuilder strawberryBuilder = new StrawberryBuilder(); /** * Buy red bean flavored * @return */ public Tea getRedBeanTea(){ redBeanBuilder.buildPart(); return redBeanBuilder.buildProduct(); } /** * Buy strawberry-flavored * @return */ public Tea getStrawberryTea(){ strawberryBuilder.buildPart(); return strawberryBuilder.buildProduct(); } }
package com.tc.gof.builder; public class BuilderTest { public static void main(String[] args) { Cashier cashier = new Cashier(); System.out.println("=======Buy red bean flavored======="); cashier.getRedBeanTea(); System.out.println("=======Buy milk tea flavored======="); cashier.getStrawberryTea(); } }
The output is as follows:
=======Purchase red bean flavor======
Add red beans
Adding sugar
water
Done with red bean flavor
========Buy milk tea======
Add strawberry ingredients
water
Adding sugar
Strawberry flavor done
Summary: The builder model is an extension of the factory model, where the factory class provides the ability to produce individual products, while the builder model allows for centralized and unified management of individual products.
Difference: The factory model focuses on the entire product.
The builder model focuses on the process of creating components of a product.