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

AbstractFactoryパターン

Java

ややこしい。確かに理解しずらいねぇ。Java楽しくなってきた!

% find . -name "*.java" -print0 |xargs -0 cat                                []
package factory;

public abstract class Factory {
	public static Factory getFactory(String className) {
		Factory factory = null;
		try {
			factory = (Factory) Class.forName(className).newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return factory;
	}

	public abstract Link createLink(String caption, String url);

	public abstract Tray createTray(String caption);

	public abstract Page createPage(String title, String author);
}
package factory;

public abstract class Item implements MakeHtml {
	protected String caption;

	public Item(String caption) {
		this.caption = caption;
	}
}
package factory;

public abstract class Link extends Item implements MakeHtml {

	protected String url;

	public Link(String caption, String url) {
		super(caption);
		this.url = url;
	}

}
package factory;

public interface MakeHtml {
	public String makeHTML();
}
package factory;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

public abstract class Page implements MakeHtml {
	protected String title;
	protected String author;
	protected List<Item> content = new ArrayList<Item>();

	public Page(String title, String author) {
		this.title = title;
		this.author = author;
	}

	public void add(Item item) {
		content.add(item);
	}

	public void output() {
		String fileName = title + ".html";
		try {
			Writer writer = new FileWriter(fileName);
			writer.write(makeHTML());
			writer.close();
			System.out.println(fileName + "を作成しました。");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
package factory;

import java.util.ArrayList;
import java.util.List;

public abstract class Tray extends Item implements MakeHtml {
	protected List<Item> tray = new ArrayList<Item>();

	public Tray(String caption) {
		super(caption);
	}

	public void add(Item item) {
		tray.add(item);
	}

}
package listfactory;

import factory.Factory;
import factory.Link;
import factory.Page;
import factory.Tray;

public class ListFactory extends Factory {

	@Override
	public Link createLink(String caption, String url) {
		return new ListLink(caption, url);
	}

	@Override
	public Tray createTray(String caption) {
		return new ListTray(caption);
	}

	@Override
	public Page createPage(String title, String author) {
		return new ListPage(title, author);
	}

}
package listfactory;

import factory.Link;

public class ListLink extends Link {
	public ListLink(String caption, String url) {
		super(caption, url);
	}

	@Override
	public String makeHTML() {
		return "  <li><a href'" + url + "'>" + caption + "</a></li>\n";
	}

}
package listfactory;

import factory.Item;
import factory.Page;

public class ListPage extends Page {

	public ListPage(String title, String author) {
		super(title, author);
	}

	@Override
	public String makeHTML() {
		StringBuffer sb = new StringBuffer();
		sb.append("<html><head><title>" + title + "</title></head>\n");
		sb.append("<body>\n");
		sb.append("<h1>" + title + "</h1>");
		sb.append("<ul>");
		for (Item item : content) {
			sb.append(item.makeHTML());
		}
		sb.append("</ul>\n");
		sb.append("<hr><address>" + author + "</address>");
		sb.append("</body></html>\n");
		return sb.toString();
	}

}
package listfactory;

import factory.Item;
import factory.Tray;

public class ListTray extends Tray {
	public ListTray(String caption) {
		super(caption);
	}

	@Override
	public String makeHTML() {
		StringBuffer sb = new StringBuffer();
		sb.append("<li>\n");
		sb.append(caption + "\n");
		sb.append("<ul>\n");
		for (Item item : tray) {
			sb.append(item.makeHTML());
		}
		sb.append("</ul>\n");
		sb.append("</li>\n");
		return sb.toString();
	}

}
import factory.Factory;
import factory.Link;
import factory.Page;
import factory.Tray;

public class Main {

	public static void main(String[] args) {
		Factory factory = Factory.getFactory("listfactory.ListFactory");
		Link asahi = factory.createLink("asahi", "http://hoge.com");
		Link yomiuri = factory.createLink("yomiuri", "http://hoge.com");
		Link yahoo = factory.createLink("yahoo", "http://hoge.com");
		Link google = factory.createLink("google", "http://hoge.com");

		Tray trayNews = factory.createTray("News");
		trayNews.add(asahi);
		trayNews.add(yomiuri);

		Tray traySearch = factory.createTray("Search");
		traySearch.add(yahoo);
		traySearch.add(google);
		traySearch.add(trayNews);

		Page page = factory.createPage("Links", "author");
		page.add(trayNews);
		page.add(traySearch);

		page.output();
	}

}
% find . -name "*.html" -print0 |xargs -0 cat                                []
<html><head><title>Links</title></head>
<body>
<h1>Links</h1><ul><li>
News
<ul>
  <li><a href'http://hoge.com'>asahi</a></li>
  <li><a href'http://hoge.com'>yomiuri</a></li>
</ul>
</li>
<li>
Search
<ul>
  <li><a href'http://hoge.com'>yahoo</a></li>
  <li><a href'http://hoge.com'>google</a></li>
<li>
News
<ul>
  <li><a href'http://hoge.com'>asahi</a></li>
  <li><a href'http://hoge.com'>yomiuri</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<hr><address>author</address></body></html>