Builder pattern of design pattern

1.Definition and UML modeling diagram
Separating the construction of a complex object from its representation, yes the same construction process can create different representations.

2. Use scenario:
Multiple components or parts can be assembled into one object, but the operation results are the same.
Product classes are very complex, or different calling sequences in product classes have different effects.
Initializing an object is particularly complicated, such as when using multiple constructors, or when there are many parameters and all have default values.
Core classes

(1) Abstract product computer.
(2) Builder Abstract Builder, which regulates the building of products, is generally implemented by subclasses.
(3) MacbookBuilder specific Builder class, specific object creation class.
(4) Unified formation process of Directror.
It is mainly to assemble a computer, mainly to assemble the host, set the operating system, and set the display.
4. Specific use
4.1 Computer Abstract Class
Computer.java public abstract class Computer {
     protected String mBoard;
     protected String mDisplay;
     protected String mOs;

     protected Computer () {
     }

     public void setBoard (String board) {
         mBoard = board;
     }

     public void setDisplay (String display) {
         this.mDisplay = display;
     }

     public abstract void setOs ();

     @Override
     public String toString () {
         return "Computer {" +
                 "mBoard = '" + mBoard +' \ '' +
                 ", mDisplay = '" + mDisplay +' \ '' +
                 ", mOs = '" + mOs +' \ '' +
                 '}';
     }
}

4.2 Specific Product MacBooks
MacBook.java public class MacBook extends Computer {

     protected MacBook () {
     }

     @Override
     public void setOs () {
         mOs = "Mac OS X 12";
     }
}

4.3 Abstract Builder
Builder.java public abstract class Builder {

     abstract void buildBoard (String board);
     abstract void buildDisplay (String display);
     abstract void buildOs ();
     abstract Computer build ();

}

4.4 Specific Builder
MacBookBuilder.java public class MacBookBuilder extends Builder {

     private Computer mComputer = new MacBook ();
     @Override
     void buildBoard (String board) {
         mComputer.setBoard (board);
     }

     @Override
     void buildDisplay (String display) {
         mComputer.setDisplay (display);
     }

     @Override
     void buildOs () {
         mComputer.setOs ();
     }

     @Override
     Computer build () {
         return mComputer;
     }
}

4.5 Director class, responsible for the specific construction Computer
Director.java public class Director {
     Builder mBuilser = null;

     public Director (Builder builer) {
         this.mBuilser = builer;
     }

     public void construct (String board, String display) {
         mBuilser.buildDisplay (display);
         mBuilser.buildBoard (board);
         mBuilser.buildOs ();
     }
}

4.6 Specific use
Test.java public class Test {

     public static void main (String [] args) {
         Builder builder = new MacBookBuilder ();
         Director pcDirector = new Director (builder);
         pcDirector.construct ("Intel motherboard", "Retina display");

         Computer computer = builder.build ();
         System.out.println (computer.toString ());
     }

}
5. Summary
In order to construct a complex object flexibly, the object will have multiple member variables. When called externally, it is not necessary or convenient to create all the member variables at once. In this case, multiple constructors are used to construct the object. It is difficult to maintain. At this time, the Builder design pattern solves this problem. The object is created in the buid () method, and the builder is passed in. The builder maintains the member variables of the passed-in object.

Author: