Builder Pattern - Builder

Builder Pattern - Builder

The Builder pattern is a creational pattern for creating a complex object step by step, which allows users to control the construction process of an object more finely without knowing the internal construction details. This pattern is to decouple the process of constructing a complex object from its components, so that the construction process and the representation of the components are isolated

Builder interface

Defines the builder's methods.

public interface Builder {
    void makeTitle(String title);

    void makeString(String str);

    void makeItems(String[] items);

    void close();
}

TextBuilder 类

This is a concrete implementation of the Builder interface, which is used to build Markdown-like content based on input

public class TextBuilder implements Builder {
    private StringBuffer out = new StringBuffer();

    @Override
    public void makeTitle(String title) {
        out.append("==============================\n");
        out.append("『 " + title + " 』\n");
        out.append("\n");
    }

    @Override
    public void makeString(String str) {
        out.append("■ " + str + "\n");
        out.append("\n");
    }

    @Override
    public void makeItems(String[] items) {
        for (int i = 0; i < items.length; i++) {
            out.append(" ・" + items[i] + "\n");
        }
        out.append("\n");
    }

    @Override
    public void close() {
        out.append("==============================\n");
    }

    public String getResult() {
        return out.toString();
    }
}

  

Director class

This is a commander that uses Builder to assemble content

public class Director {
    private Builder builder;

    public Director(Builder builder) { // Because the received parameter is a subclass of the Builder class
        this.builder = builder; // so it can be saved in the builder field
    }

    public void construct() { // write documentation
        builder.makeTitle("Greeting");              // 标题
        builder.makeString("from morning to afternoon"); // string
        builder.makeItems(new String[]{ // items
                "Good morning.",
                "good afternoon.",
        });
        builder.makeString("night"); // other strings
        builder.makeItems(new String[]{ // other items
                "good evening.",
                "Good night.",
                "goodbye.",
        });
        builder.close(); // complete the document
    }
}

Main

This class is used to run tests

public class Main {
    public static void main(String[] args) {
        // builder
        TextBuilder textbuilder = new TextBuilder ();
        
        // conductor
        Director director = new Director(textbuilder);
        
        // Construct
        director.construct();
        
        // build content
        System.out.println (textbuilder.getResult ());
    }
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325851599&siteId=291194637