組裝復雜的實例
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) { // 因為接收的參數(shù)是 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 角色是負責實現(xiàn) Builder 角色的接口的類术陶。此外,ConcreteBuilder 角色中還定義了獲取最終生成結(jié)果的方法煤痕。
-
Direcor(建工)
Direcor 角色負責使用 Builder 角色的接口來生成實例瞳别。它只調(diào)用在 Builder 角色中被定義的方法征候。
Client(使用者)
拓展思路
Q:誰知道什么
A:Direcor 類不知道自己使用的是 Builder 的哪個子類。這是因為“只有不知道子類才能替換”祟敛。