Builder模式(设计模式)

作用:
    组装具有复杂结构的实例,组装的过程被隐藏在Director角色中
理解:
    当创建的对象特别的复杂,把这个复杂的对象进行拆分。这个复杂对象的各个部分经常会需要面临剧烈的变化,但是他们的组合在一起的算法是相对稳定
的。Builder模式就是一种“封装机制”来隔离“复杂对象的各个部分”的变化,从而保持系统中的“稳定构建算法”不随需求发生改变而改变。

Builder模式.png

小案例
Builder.java

package example1;
/**
 * @Author:DarknessShadow
 * @description:
 * @date 2020/5/13 10:19
 */
public abstract class Builder {

    public abstract void makeTitle(String title);

    public abstract void makeString(String string);

    public abstract void makeItems(String[] items);

    public abstract void close();
}

HtmlBuilder.java

package example1;
import java.io.FileWriter;
import java.io.PrintWriter;
/**
 * @Author:DarknessShadow
 * @description:
 * @date 2020/5/13 10:41
 */
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 (Exception e) {
            e.printStackTrace();
        }
        writer.println("" + title + "");
        writer.println("

" + title + "

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

" + string + "

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

TextBuilder.java

package example1;
/**
 * @Author:DarknessShadow
 * @description:
 * @date 2020/5/13 10:25
 */
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 string) {
        buffer.append("@" + string + "\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();
    }
}

Director.java

package example1;
/**
 * @Author:DarknessShadow
 * @description:
 * @date 2020/5/13 10:20
 */
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[]{       // 条目
                "good morning",
                "good afternoon",
        });
        builder.makeString("晚上");
        builder.makeItems(new String[]{       // 条目
                "good night",
                "good bye",
        });
        builder.close();
    }
}

Main.java

package example1;
/**
 * @Author:DarknessShadow
 * @description:
 * @date 2020/5/13 10:47
 */
public class Main {
    public static void main(String[] args) {
        String arg = "plain";
        if ("plain".equals(arg)) {
            TextBuilder textBuilder = new TextBuilder();
            Director director = new Director(textBuilder);
            director.construct();
            String result = textBuilder.getResult();
            System.out.println(result);
        } else if ("html".equals(arg)) {
            HTMLBuilder htmlBuilder = new HTMLBuilder();
            Director director = new Director(htmlBuilder);
            director.construct();
            String filename = htmlBuilder.getResult();
            System.out.println(filename + "文件编写完成");
        }
    }
}

代码下载

你可能感兴趣的:(Builder模式(设计模式))