设计模式之Builder模式(建造者模式)

啊啊啊,希望时间过的再快些,快进到论文答辩完,顶不住啦

建造者模式是用来组装具有复杂结构的实例的模式,先看示例,我们要写一个编写文档的程序,该文档有以下结构:标题,字符串,多条项目。上代码:先将建造者Builder按照文档结构进行抽象:

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

然后是监工类Director:

public class Director {
    private Builder builder;

    public Director(Builder builder) {
        this.builder = builder;
    }

    /**
     * 编写文档的方法
     */
    public void construct() {
        builder.makeTitle("Greeting");
        builder.makeString("从早上到下午");
        builder.makeItems(new String[]{
                "早上好",
                "下午好"
        });
        builder.makeString("晚上");
        builder.makeItems(new String[]{
                "晚上好",
                "晚安",
                "再见"
        });
        builder.close();
    }
}

监工类设计好了建造的过程,现在只等具体建造者实现它的建造细节,接下来是两个具体建造者TextBuilder和HTMLBuilder,功能分别是使用纯文本编写文档并返回和使用HTML编写文档并返回该文件名字:

public class TextBuilder extends Builder {

    private StringBuffer buffer = new StringBuffer();

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

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

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

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

    public String getResult() {
        return buffer.toString();
    }
}
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author lenovo
 * @title: HTMLBuilder
 * @projectName LearnDesignMode
 * @description: TODO
 * @date 2022/5/618:49
 */
public class HTMLBuilder extends Builder {

    private String filename;
    private PrintWriter writer;

    @Override
    public void makeTitle(String title) {
        filename = title + ".html";
        try {
            writer = new PrintWriter(new FileWriter(filename));
        } catch (IOException e) {
            e.printStackTrace();
        }
        writer.println(""</span> <span class="token operator">+</span> title <span class="token operator">+</span> <span class="token string">"");
        writer.println("

" + title + "

"
); } @Override public void makeString(String str) { writer.println("

" + str + "

"
); } @Override public void makeItems(String[] items) { writer.println("
    "); for(int i=0; i<items.length; ++i) { writer.println("
  • " + items[i] + "
  • "
    ); } writer.println("
"
); } @Override public void close() { writer.println(""); writer.close(); } public String getResult() { return filename; } }

测试一下:

public class Main {
    public static void main(String[] args) {

        TextBuilder textBuilder = new TextBuilder();
        Director director = new Director(textBuilder);
        director.construct();
        String result = textBuilder.getResult();
        System.out.println(result);

        HTMLBuilder htmlBuilder = new HTMLBuilder();
        director = new Director(htmlBuilder);
        director.construct();
        String filename = htmlBuilder.getResult();
        System.out.println(filename + "文件编写完成");

    }
}

运行结果:

========================
[Greeting]

**从早上到下午

    .早上好
    .下午好

**晚上

    .晚上好
    .晚安
    .再见

========================

Greeting.html文件编写完成

还有生成的html文件内容如下:

<html><head><title>Greetingtitle>head><body>
<h1>Greetingh1>
<p>从早上到下午p>
<ul>
<li>早上好li>
<li>下午好li>
ul>
<p>晚上p>
<ul>
<li>晚上好li>
<li>晚安li>
<li>再见li>
ul>
body>html>

总结一下:Builder模式中有以下角色:
Builder(建造者),ConcreteBuilder(具体建造者),Director(监工),Client(使用者)。其中,Builder负责定义用于生成实例的API,即定义了建造的步骤有哪些,ConcreteBuilder负责实现Builder,根据实际情况将建造步骤的细节落地为代码,Director负责使用Builder的接口来组合实例,只调用在Builder中定义的方法,不关心ConcreteBuilder的具体实现,细节依赖抽象而不是抽象依赖细节,最后就是Client来使用Director,生成了想要的实例。

你可能感兴趣的:(JavaLearn,设计模式,建造者模式,java)