読者です 読者をやめる 読者になる 読者になる

Builderパターン

Templateパターン的でもある。Factory形式でインスタンスを得てもいいかもしれない。だめ、ちょっと混乱してきた。

% find . -name "*.java" -print0 |xargs -0 cat                                []
public interface Builder {
	void makeTitle(String str);

	void makeString(String str);

	void makeItems(String[] items);

	void close();
}
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();
	}

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

public class HTMLBuilder implements Builder {
	private String filename;
	private PrintWriter writer;

	@Override
	public void makeTitle(String title) {
		this.filename = title + ".html.txt";
		try {
			writer = new PrintWriter(new FileWriter(filename));
		} catch (IOException e) {
			e.printStackTrace();
		}
		writer.println("<html><head><title>" + title + "</tile></head><body>");
		writer.println("<h1>" + title + "</h1>");
	}

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

	@Override
	public void makeItems(String[] items) {
		writer.println("<ul>");
		for (String str : items) {
			writer.println("<li>" + str + "</li>");
		}
		writer.println("<ul>");
	}

	@Override
	public void close() {
		writer.println("</body></html>");
		writer.close();
		System.out.println("作成完了:" + filename);
	}

}
public class Main {

	public static void main(String[] args) {
		Director d1 = new Director(new TextBuilder());
		d1.construct();
		Director d2 = new Director(new HTMLBuilder());
		d2.construct();
	}

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

public class TextBuilder implements Builder {
	private String title;
	private StringBuffer sb = new StringBuffer();

	@Override
	public void makeTitle(String str) {
		this.title = str;
		yokosen();
		sb.append("『" + str + "』\n");
		sb.append("\n");
	}

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

	@Override
	public void makeItems(String[] items) {
		for (String str : items) {
			sb.append(" ・" + str + "\n");
		}
		sb.append("\n");
	}

	@Override
	public void close() {
		yokosen();
		String filename = title + ".txt.txt";
		try {
			PrintWriter writer = new PrintWriter(new FileWriter(filename));
			writer.print(sb.toString());
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("作成完了:" + filename);
	}

	private void yokosen() {
		sb.append("====================================\n");
	}

}
% find . -name "*.txt" -print0 |xargs -0 cat                                 []
<html><head><title>Greeting</tile></head><body>
<h1>Greeting</h1>
<p>朝から昼にかけて</p>
<ul>
<li>おはようございます。</li>
<li>こんにちは</li>
<ul>
<p>夜に</p>
<ul>
<li>こんばんは</li>
<li>おやすみなさい</li>
<li>さようなら</li>
<ul>
</body></html>
====================================
『Greeting』

■朝から昼にかけて

 ・おはようございます。
 ・こんにちは

■夜に

 ・こんばんは
 ・おやすみなさい
 ・さようなら

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