Easy Tutorial
❮ Mac Os Ssh Pem Java Capture Group ❯

Design Pattern: Builder Pattern

Category Programming Techniques

I. Introduction

Today we discuss the Builder pattern, which is quite similar to the Template pattern, but there is a difference. In the Template pattern, the parent class operates on the implementation of the subclass, handling a certain matter within the parent class. However, in the Builder pattern, both the parent and subclass do not need to care about how to handle it, but use another class to complete the organic combination of these methods. The responsibility of this class is the "foreman," which stipulates how to organically combine these methods. In the foreman class (Director), the parent class is combined and then the parent class's operations are called to abstractly implement a matter. This is the essence of facing the interface (abstraction). Of course, this Builder can be an interface or an abstract class, and here we use an abstract class.

II. Builder Pattern Code

Builder Abstract Class:

Builder.java

public abstract class Builder {

    public abstract void makeString(String str);
    public abstract void makeTitle(String title);
    public abstract void makeItems(String[] items);
    public abstract void close();

}

HtmlBuilder Implementation Class:

HtmlBuilder.java

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class HtmlBuilder extends Builder {

    private String filename;
    private PrintWriter pw;
    public void makeTitle(String title) {
        filename="D:\\"+title+".html";
        try {
            pw=new PrintWriter(new FileWriter(filename));
        } catch (IOException e) {
            e.printStackTrace();
        }
        pw.println("<html><head><title>"+title+"</title></head><body>");
        pw.println("<h1>"+title+"</h1>");
    }

    public void makeString(String str) {
        pw.println("<p>"+str+"</p>");
    }

    public void makeItems(String[] items) {
        pw.println("<ul>");
        for(int i=0;i&lt;items.length;i++){
            pw.println("<li>"+items[i]+"</li>");
        }
        pw.println("</ul>");
    }

    public void close() {
        pw.println("</body></html>");
        pw.close();
    }
    public String getResult(){
        return filename;
    }
}

TextBuilder Implementation Class:

TextBuilder.java

public class TextBuilder extends Builder {

    StringBuffer sb=new StringBuffer();

    public void makeTitle(String title) {
        sb.append("=====================");
        sb.append("["+title+"]"+"\n");
    }

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

    public void makeItems(String[] items) {
        for(int i=0;i&lt;items.length;i++){
            sb.append("   ."+items[i]+"\n");
        }
    }

    public void close() {
        sb.append("=====================");
    }

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

}

Director Foreman Class:

Director.java

public class Director {
    private Builder builder;
    public Director(Builder builder){
        this.builder=builder;
    }
    public void construct(){
        String [] items1=new String[]{"Play the national anthem","Raise the national flag"};
        String [] items2=new String[]{"Audience applauds","Orderly evacuation"};
        builder.makeTitle("Today's Headlines");
        builder.makeString("Graduation Ceremony");
        builder.makeItems(items1);
        builder.makeString("The ceremony is over");
        builder.makeItems(items2);
        builder.close();
    }
}

Main Class:

Main.java

public class Main {

    public static void main(String[] args) {
        //String choice="plain";
        String choice="html";
        if(choice.equals("plain")){
            TextBuilder t=new TextBuilder();
            Director d=new Director(t);
            d.construct();
            System.out.println(t.getResult());
        }else if(choice.equals("html")){
            HtmlBuilder html=new HtmlBuilder();
            Director d=new Director(html);
            d.construct();
            System.out.println(html.getResult());
        }else{
            usage();
        }

    }

    private static void usage() {
        System.out.println("Use 'plain' to edit text files");
        System.out.println("Use 'html' to edit web page files");
    }

}

Running Results

Or:

III. Summary

Regarding the Builder pattern, we must distinguish it from the Template method. The essence lies in who takes on the responsibility of the "foreman." In the Template method, the parent class

❮ Mac Os Ssh Pem Java Capture Group ❯