组装复杂的实例
Builder 模式
首先建造组成这个物体的各个部分,然后分阶段将它们组装起来
示例程序
Builder.java(Builder)
package builder;
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();
}
Director.java(Direcor)
package builder;
public class Director {
private Builder builder;
public Director(Builder builder) { // 因为接收的参数是 Builder 类的子类
this.builder = builder; // 所以可以将其保存在 builder 字段中
}
public void construct() {
builder.makeTitle("Greeting");
builder.makeString("从上午至下午");
builder.makeItems(new String[]{
"早上好。",
"下午好。"
});
builder.makeString("晚上");
builder.makeItems(new String[]{
"晚上好。",
"晚安。",
"再见。"
});
builder.close();
}
}
TextBuilder.java(ConcreteBuilder)
package builder;
public class TextBuilder extends Builder {
private StringBuffer buffer = new StringBuffer();
public void makeTitle(String title) {
buffer.append("===============================\n");
buffer.append("【" + title + "】\n");
buffer.append("\n");
}
public void makeString(String string) {
buffer.append("◼︎" + string + "\n");
buffer.append("\n");
}
public void makeItems(String[] items) {
for (int i = 0; i < items.length; i++) {
buffer.append(" -" + items[i] + "\n");
}
buffer.append("\n");
}
public void close() {
buffer.append("===============================\n");
}
public String getResult() {
return buffer.toString();
}
}
HTMLBuilder.java(ConcreteBuilder)
package builder;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class HTMLBuilder extends Builder {
private String filename;
private PrintWriter writer;
public void makeTitle(String title) {
filename = title + ".html";
try {
writer = new PrintWriter(new FileWriter(filename));
} catch (IOException e) {
e.printStackTrace();
}
writer.println("<html><head><title>" + title + "</title></head><body>");
writer.println("<h1>" + title + "</h1>");
}
public void makeString(String string) {
writer.println("<p>" + string + "</p>");
}
public void makeItems(String[] items) {
writer.println("<ul>");
for (int i = 0; i < items.length; i++) {
writer.println("<li>" + items[i] + "</li>");
}
writer.println("</ul>");
}
public void close() {
writer.println("</body></html>");
}
public String getResult() {
return filename;
}
}
Main.java(Client)
package builder;
public class Main {
public static void main(String[] args) {
if (args.length != 1) {
usage();
System.exit(0);
}
if (args[0].equals("plain")) {
TextBuilder textBuilder = new TextBuilder();
Director director = new Director(textBuilder);
director.construct();
String result = textBuilder.getResult();
System.out.println(result);
} else if (args[0].equals("html")) {
HTMLBuilder htmlBuilder = new HTMLBuilder();
Director director = new Director(htmlBuilder);
director.construct();
String filename = htmlBuilder.getResult();
System.out.println(filename + "文件编写完成。");
} else {
usage();
System.exit(0);
}
}
public static void usage() {
System.out.println("Usage: java Main plain 编写纯文本文档");
System.out.println("Usage: java Main html 编写 HTML 文档");
}
}
Builder 模式中的登场角色
-
Builder(建造者)
Builder 角色负责定义用户生成实例的接口。Builder 角色中准备了用于生成实例的方法。
-
ConcreteBuilder(具体的建造者)
ConcreteBuilder 角色是负责实现 Builder 角色的接口的类。此外,ConcreteBuilder 角色中还定义了获取最终生成结果的方法。
-
Direcor(建工)
Direcor 角色负责使用 Builder 角色的接口来生成实例。它只调用在 Builder 角色中被定义的方法。
Client(使用者)
拓展思路
Q:谁知道什么
A:Direcor 类不知道自己使用的是 Builder 的哪个子类。这是因为“只有不知道子类才能替换”。