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

Facadeパターン

Java

シンプルでしたね。

% find . -name "*.java" -print0 |xargs -0 cat                                                                               []
import pagemaker.PageMaker;

public class Main {

	public static void main(String[] args) {
		PageMaker.makeWelcomPage("test@example.com", "hoge");
	}

}
package pagemaker;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

class Database {
	private Database() {

	}

	public static Properties getProperties(String dbname) {
		String filename = dbname + ".txt";
		Properties pop = new Properties();
		try {
			pop.load(new FileInputStream(filename));
		} catch (IOException e) {
			System.out.println("error");
		}
		return pop;
	}
}
package pagemaker;

import java.io.IOException;
import java.io.Writer;

class HtmlWriter {
	private Writer writer;

	public HtmlWriter(Writer writer) {
		this.writer = writer;
	}

	public void title(String title) throws IOException {
		writer.write("<html>");
		writer.write("<head>");
		writer.write("<title>" + title + "</title>");
		writer.write("</head>");
		writer.write("<body>\n");
		writer.write("<h1>" + title + "</h1>");
	}

	public void paragraph(String msg) throws IOException {
		writer.write("<p>" + msg + "</p>\n");
	}

	public void link(String href, String caption) throws IOException {
		paragraph("<a href='" + href + "'>" + caption + "</a>");
	}

	public void mailto(String mailAddr, String userName) throws IOException {
		link("mailto:" + mailAddr, userName);
	}

	public void close() throws IOException {
		writer.write("</body>\n");
		writer.write("</html>");
		writer.close();
	}
}
package pagemaker;
public class Main {

	public static void main(String[] args) {
		PageMaker.makeWelcomPage("test@example.com", "hoge");
	}

}
package pagemaker;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Properties;

public class PageMaker {
	private PageMaker() {

	}

	public static void makeWelcomPage(String mailAddr, String filename) {
		try {
			Properties mailProp = Database.getProperties("maildata");
			String userName = mailProp.getProperty(mailAddr);
			HtmlWriter writer = new HtmlWriter(new OutputStreamWriter(
					System.out));
			writer.title("Welcome to " + userName + "'s page!");
			writer.paragraph(userName + "のページへようこそ");
			writer.paragraph("メールを待ってまーす");
			writer.mailto(mailAddr, userName);
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
<html><head><title>Welcome to New Pill's page!</title></head><body>
<h1>Welcome to New Pill's page!</h1><p>New Pillのページへようこそ</p>
<p>メールを待ってまーす</p>
<p><a href='mailto:test@example.com'>New Pill</a></p>
</body>
</html>