define

Separating the construction of a complex object from its representation allows the same construction process to create different representations. Detailed, step-by-step building of complex products.

It has two parts, one part is the component construction and product assembly, the other part is the algorithm of the whole construction. In the generator pattern, the emphasis is on algorithms that fix the overall construction, while flexibly extending and switching the way parts are constructed and products are assembled.

nature

Separate the whole build algorithm from the part build

His role

  • The Builder

    Abstract class, which defines all the methods used to generate instances.

  • ConcreteBuilder

    Implements all the methods that the spawn instance will use.

  • Director (Director)

    The mentor uses the Builder interface to build the required objects in a unified process.

  • Client (user)

    Use the Builder interface.

The sample code

Public abstract class Builder{public abstract void makeTitle(String title); public abstract void makeTitle(String title); public abstract void makeString(String string); public abstract void makeItems(String item); public abstract void close(); } public class Director {private Builder Builder; public Director(Builder builder) { this.builder = builder; } /** * public void construct(){builder.makeTitle("Greeting"); Builder.makestring (" From morning to night "); Builder.maketitle (" Good morning "); Builder. MakeString (" evening "); Builder.makeitems (" Good evening "); builder.close(); Public class HtmlBuidler extends Builder{private StringBuffer StringBuffer = new StringBuffer(); @Override public void makeTitle(String title) { stringBuffer.append("<body>\r\n"); stringBuffer.append("<title>" + title + "</title>\r\n"); } @Override public void makeString(String string) { stringBuffer.append("<p>" + string + "</p>\r\n"); } @Override public void makeItems(String item) { stringBuffer.append("<ul><li>" + item + "</li></ul>\r\n"); } public StringBuffer getStringBuffer() { return stringBuffer; } @Override public void close() { stringBuffer.append("</body>\r\n"); Private StringBuffer StringBuffer = new StringBuffer(""); private StringBuffer = new StringBuffer("");  @Override public void makeTitle(String title) { stringBuffer.append("================"); stringBuffer.append("[" + title + "]"); stringBuffer.append("\r\n"); } @Override public void makeString(String string) { stringBuffer.append("#####" + string + "\r\n"); } @Override public void makeItems(String item) { stringBuffer.append("@@@ " + item + "\r\n"); } @Override public void close() { stringBuffer.append("================"); } public StringBuffer getStringBuffer() { return stringBuffer; Public class Client {public static void main(String[] args){TextBuilder TextBuilder = new TextBuilder(); Director director = new Director(textBuilder); director.construct(); StringBuffer stringBuffer = textBuilder.getStringBuffer(); System.out.println("this is the textBuilder"); System.out.println(stringBuffer); HtmlBuidler htmlBuidler = new HtmlBuidler(); director = new Director(htmlBuidler); director.construct(); stringBuffer = htmlBuidler.getStringBuffer(); System.out.println("this is the htmlBuilder"); System.out.println(stringBuffer); }}Copy the code

The results

This is the textBuilder = = = = = = = = = = = = = = = = [the Greeting] # # # # # from morning to night = = = = = = = = = = = = = = = = good morning [] # # # # # @ @ @ night good night = = = = = = = = = = = = = = = = This is the htmlBuilder <body> <title> <p> From morning to evening </p> <body> <title> good morning </title> <p> evening </p> <ul><li> Good evening </li></ul> </body>Copy the code

function

Generator model is build complex are the two main functions of products, and building products are detailed, step-by-step, the generator model focuses on the construction of a complex object step by step to solve the problem, more importantly, the build process is unified, fixed, changes in the sections on the generator, as long as the configuration of different generators, so the same construction process, You can build different products.

advantages

  • Loose coupling. It is possible to use the same build algorithm to build products that behave completely differently, and to achieve the separation of product build and product performance. The generator pattern is precisely what separates the process of building a product from its loose coupling with the performance of a specific product so that the build algorithm can be reused and the performance of a specific product can be flexibly and easily extended and switched.
  • You can easily change the internal representation of the product. In generator mode, since the Builder object is only available to the Director, the specific way parts are created and assembled is hidden by the Builder interface. The Director does not know the implementation details, so if he wants to change the internal representation of the product, All you need to do is switch the concrete representation of the Builder.
  • Better reusability. The generator pattern provides a good separation between the build algorithm and the product-specific implementation, so that the build algorithm can be reused. In the same way, the product-specific implementation can be reused. The same product-specific implementation can be used with different build algorithms.

Generator pattern and template method pattern

Similar in function, the template method pattern fixes the algorithm framework and delays the implementation of some steps in the algorithm to subclasses. In the generator mode, the Director defines the overall construction algorithm and delegates the creation and assembly of specific part objects in the algorithm to the specific Builder implementation. Therefore, in essence, the two patterns are very similar. They are both fixed algorithm framework, and then some specific steps in the algorithm are handed over to other classes to achieve the separation of the overall algorithm and the realization of some specific steps.

But there are big differences between the two models. The first is the purpose of patterns. Generator patterns are used to build complex objects, while template method patterns are used to anchor the framework of algorithms, with different emphasis. The second is the complexity of the use. The generator pattern requires the combination of Director and Builder objects before the construction can begin. After the construction is complete, the final object can be obtained.