[1] basic concepts
The construction (builder) mode is a design mode for object construction. It can abstract the construction process of complex objects (abstract categories ), different implementation methods of this abstract process can be used to construct objects with different performances (attributes.
[2] Simple Analysis
Let's take a look at the UML structure of the design pattern.
It is a structure chart of the Strategy mode, which allows us to describe more conveniently:
-
Specify an abstract interface for each part of a product object.
-
Implement the builder interface to construct and assemble each part of the product.
-
Define and define the representation it creates.
-
Provides an interface for retrieving Products
-
Construct an object using the builder interface.
-
Indicates a complex object to be constructed. Concreatebuilder creates an internal representation of the product and defines its assembly process.
Use the generator mode in the following scenarios:
-
Class that includes defining components, including interfaces for assembling these components into a final product.
-
- When creating complex objects (the building sequence between these objects is usually stable), the algorithms should be independent of the components of the objects and their assembly methods;
- When the constructor must allow different representations of the constructed object.
[3] How to Use Java to implement this mode
The following is a simple example to show this mode. First, let's look at the code structure:
3.1 create a product class first-product class:
Package COM. andyidea. patterns. product;/*** product -- product class * @ author Andy. chen **/public class pizza {private string dough; private string sauce; private string topping; Public void setdough (string dough) {This. dough = dough;} public void setsauce (string sauce) {This. sauce = sauce;} public void settopping (string topping) {This. topping = topping ;}}
3.2 Create an abstract builder class: pizzabuilder. Java
Package COM. andyidea. patterns. builder; import COM. andyidea. patterns. product. pizza;/*** builder class -- Abstract builder class * @ author Andy. chen **/public abstract class pizzabuilder {protected pizza; Public pizza getpizza () {return pizza;} public void createnewpizzaproduct () {pizza = new pizza ();} public abstract void builddough (); public abstract void buildsauce (); public abstract void buildtopping ();}
3.3 create a specific builder class
Hawaiianpizzabuilder. Java source code:
Package COM. andyidea. patterns. concretebuilder; import COM. andyidea. patterns. builder. pizzabuilder;/*** concretebuilder class -- specific builder class * @ author Andy. chen **/public class hawaiianpizzabuilder extends pizzabuilder {@ overridepublic void builddough () {system. out. println ("Hawaiian-dough"); pizza. setdough ("Hawaiian-dough") ;}@ overridepublic void buildsauce () {system. out. println ("Hawaiian-sauce"); pizza. setsauce ("Hawaiian-sauce") ;}@ overridepublic void buildtopping () {system. out. println ("Hawaiian-topping"); pizza. settopping ("Hawaiian-topping ");}}
Spicypizzabuilder. Java source code:
Package COM. andyidea. patterns. concretebuilder; import COM. andyidea. patterns. builder. pizzabuilder;/*** concretebuilder class -- specific builder class * @ author Andy. chen **/public class spicypizzabuilder extends pizzabuilder {@ overridepublic void builddough () {system. out. println ("Spicy-dough"); pizza. setdough ("Spicy-dough") ;}@ overridepublic void buildsauce () {system. out. println ("Spicy-sauce"); pizza. setsauce ("Spicy-sauce") ;}@ overridepublic void buildtopping () {system. out. println ("Spicy-topping"); pizza. settopping ("Spicy-topping ");}}
3.4 create a ctor class: Waiter. Java
Package COM. andyidea. patterns. director; import COM. andyidea. patterns. builder. pizzabuilder; import COM. andyidea. patterns. product. pizza;/*** ctor class -- conductor class * @ author Andy. chen **/public class waiter {private pizzabuilder; Public void certificate (pizzabuilder Pb) {pizzabuilder = Pb;} public pizza getpizza () {return pizzabuilder. getpizza ();} public void constructpizza () {pizzabuilder. createnewpizzaproduct (); pizzabuilder. builddough (); pizzabuilder. buildsauce (); pizzabuilder. buildtopping ();}}
3.5 test class: builderclient. Java
package com.andyidea.patterns.client;import com.andyidea.patterns.builder.PizzaBuilder;import com.andyidea.patterns.concretebuilder.HawaiianPizzaBuilder;import com.andyidea.patterns.concretebuilder.SpicyPizzaBuilder;import com.andyidea.patterns.director.Waiter;import com.andyidea.patterns.product.Pizza;public class BuilderClient {public static void main(String[] args) {System.out.println("Welcome to Andy.Chen Blog!" +"\n" +"Builder Patterns." +"\n"); Waiter waiter = new Waiter(); PizzaBuilder hawaiian_pizzabuilder = new HawaiianPizzaBuilder(); PizzaBuilder spicy_pizzabuilder = new SpicyPizzaBuilder(); System.out.println("------------HawaiianPizza------------"); waiter.setPizzaBuilder(hawaiian_pizzabuilder); waiter.constructPizza(); System.out.println("------------SpicyPizza------------"); waiter.setPizzaBuilder(spicy_pizzabuilder); waiter.constructPizza(); Pizza pizza = waiter.getPizza();}}
[4] program running result:
Welcome to Andy.Chen Blog!Builder Patterns.------------HawaiianPizza------------Hawaiian-DoughHawaiian-SauceHawaiian-Topping------------SpicyPizza------------Spicy-DoughSpicy-SauceSpicy-Topping
We can see from the above: the benefit of the builder mode is to separate the Construction Code from the representation code. Because the builder hides how the product is assembled, if you need to change the internal representation of a product, you only need to define a specific builder.