The Builder pattern: Building a complex object step by step from multiple simple objects provides the best way to create objects.

The purpose of the Builder pattern is to separate the construction of a complex object from its representation, so that the same construction process can create different representations.

Advantages: independent builder, easy to expand; Easy to control risk details.

The example scenario

In game development, Hero refers to a person, who can be presented in the form of Occupation. For example, he can be transferred to a soldier or a archer. The soldier is equipped with long sword and heavy armor, while the archer is equipped with bow and leather armor.

Let’s use the Builder model to create heroes for different classes, making a distinction between the abstract factory model and the Builder model.

Create a hero and equipment interface class:

package com.cc.builder; @author cc * @date 21-12-19 11:48 */ public interface Hero {String name(); }Copy the code
package com.cc.builder; /** * Equip Equip * @author cc * @date 21-12-19 0:24 */ public interface Equip {String name(); }Copy the code

Then there are the hero and equipment implementation classes:

package com.cc.builder; /** * hero implementation class: Warrior * @author cc * @date 21-12-19 11:57 */ public class Warrior implements Hero {@override public String name() {return  "Warrior"; }}Copy the code
package com.cc.builder; /** * hero implementation class: @author cc * @date 21-12-19 11:58 */ public class Shooter implements Hero {@override public String name() {return  "Shooter"; }}Copy the code
package com.cc.builder; /** * Implement class: @author cc * @date 21-12-19 0:03 */ public class implements () {@override public String name() {return "Sword"; }}Copy the code
package com.cc.builder; /** * Implement class: Bow * @author cc * @date 21-12-19 0:04 */ public class Bow implements () {@override public String name() {return "Bow"; }}Copy the code
package com.cc.builder; /** * Implement class: @author cc * @date 21-12-19 0:29 */ public class HeavyArmor implements () {@override public String name() { return "Heavy Armor"; }}Copy the code
package com.cc.builder; /** * Implement class: @author cc * @date leatherleatherleatherleatherleatherLeatherEd {@override public String name() {@override public String name() { return "Leather Armor"; }}Copy the code

Next comes the class entity class, which contains information about heroes and equipment:

package com.cc.builder; import java.util.ArrayList; import java.util.List; /** * @author cc * @date 21-12-19 12:00 */ public class Occupation {private List<Equip> equips = new ArrayList<>(); private Hero hero; public Occupation(Hero hero) { this.hero = hero; } public void addEquip(Equip) {equips.add(Equip); Public void showInformation() {system.out.println ("Hero: "+ hero.name()); for (Equip equip : equips) { System.out.println(equip.name()); }}}Copy the code

Create a Career Builder class:

package com.cc.builder; OccupationBuilder * @author cc * @date 21-12-19 12:04 */ public class OccupationBuilder {// Create a OccupationBuilder public Occupation prepareWarrior() { Occupation occupation = new Occupation(new Warrior()); occupation.addEquip(new Sword()); occupation.addEquip(new HeavyArmor()); return occupation; } public Occupation prepareShooter() {Occupation = new Occupation(new Shooter()); occupation.addEquip(new Bow()); occupation.addEquip(new LeatherArmor()); return occupation; }}Copy the code

Test it out:

package com.cc.builder; public class Main { public static void main(String[] args) { OccupationBuilder occupationBuilder = new OccupationBuilder(); { Occupation occupation = occupationBuilder.prepareWarrior(); occupation.showInformation(); } System.out.println(""); { Occupation occupation = occupationBuilder.prepareShooter(); occupation.showInformation(); }}}Copy the code

Results:

Hero: Warrior Sword Heavy Armor Hero: Shooter Bow Leather ArmorCopy the code

Summary of builder patterns

Look back to our introduction to the Builder pattern at the beginning

The Builder pattern, which uses multiple simple objects to build a complex object step by step, provides the best way to create objects. The purpose of the Builder pattern is to separate the construction of a complex object from its representation, so that the same construction process can create different representations. Advantages: independent builder, easy to expand; Easy to control risk details.Copy the code
  • Our purpose is to get a professional object, a single hero or equipment are just part of the profession, with the help of the builder pattern, we will each simple object combination has become a complex object, the caller does not need to care about object creation process, only need to know a calling function, the object name or type
  • As you can see in the test module, the same build process can create a concrete representation of a warrior or archer
  • Independence is easy to extend, and when we have new requirements, we can create a new builder class, with little modification to the existing code. In the example, if we want to create a mage class, we can add a function to the OccupationBuilder class or create a New MasterOccupationBuilder.

The difference between the Builder pattern and abstract factory

Factory pattern of Java design patterns in the previous article, we use are fighters in the game, the striker example, in the process of learning the builder pattern, it is easy to confuse it with the abstract factory, in fact, the difference is obvious, look at the code can be found that the abstract factory, each factory function can create an object, They produce multiple products; The Builder pattern, in which all the creation functions add up to one object, corresponds to what is described in the beginning: the Builder pattern uses multiple simple objects to build a complex object step by step.

Usage scenarios for builder patterns and abstract factories

Abstract factory: Objects together to a series of related products, such as a support calculator (add, subtract, multiply or divide each functions) (addition, subtraction, multiplication, and division can be used alone, but in order to produce the calculator products, through the way of the abstract factory their product family together, we make a calculator, this all can have addition, subtraction, multiplication, and division of function.

Builder: A car is a very complex object. It is assembled from many parts, such as wheels, body, doors and engines. A single part is meaningless, but assembling an object is the purpose.

Therefore, the Builder pattern is mainly used in the scenario of creating a complex object step by step, focusing on the details of the construction process; Abstract factories, on the other hand, are used for the creation of a family of products, with no need to care about the construction process, only what factories are producing them.