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

Prototypeパターン

Constクラスも作ってみました。ソースコードに直で文字列が書いてあるのは気持ち悪いので。

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

public final class Const {
	public static class ProductKind {
		public static final String STRONG_MESSAGE = "strong message";
		public static final String WARNING_BOX = "warning box";
		public static final String SLASH_BOX = "slash box";
	}

}
package framework;

import java.util.HashMap;
import java.util.Map;

public class Manager {
	private Map<String, Product> showCase;

	public Manager() {
		showCase = new HashMap<String, Product>();
	}

	public void register(String name, Product proto) {
		showCase.put(name, proto);
	}

	public Product create(String name) throws Exception {
		Product p = showCase.get(name);
		if (p == null) {
			throw new Exception("element not found exception");
		}
		return p.createClone();
	}

}
package framework;

public abstract class Product implements Cloneable {
	public abstract void use(String s);

	public Product createClone() {
		Product p = null;
		try {
			p = (Product) clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return p;
	}

}
package main;

import common.Const;

import framework.Manager;
import framework.Product;

public class Main {

	public static void main(String[] args) throws Exception {
		Manager manager = new Manager();
		UnderLinePen upen = new UnderLinePen('-');
		MessageBox mbox = new MessageBox('*');
		MessageBox sbox = new MessageBox('/');
		manager.register(Const.ProductKind.STRONG_MESSAGE, upen);
		manager.register(Const.ProductKind.WARNING_BOX, mbox);
		manager.register(Const.ProductKind.SLASH_BOX, sbox);

		Product p1 = manager.create(Const.ProductKind.STRONG_MESSAGE);
		p1.use("Hello World!");
		Product p2 = manager.create(Const.ProductKind.WARNING_BOX);
		p2.use("Hello World!");
		Product p3 = manager.create(Const.ProductKind.SLASH_BOX);
		p3.use("Hello World");
	}

}
package main;

import framework.Product;

public class MessageBox extends Product {
	private char decochar;

	public MessageBox(char ch) {
		this.decochar = ch;
	}

	@Override
	public void use(String s) {
		int length = s.getBytes().length;
		for (int i = 0; i < length + 4; i++) {
			System.out.print(decochar);
		}
		System.out.println("");
		System.out.println(decochar + " " + s + " " + decochar);
		for (int i = 0; i < length + 4; i++) {
			System.out.print(decochar);
		}
		System.out.println("");
	}

}
package main;

import framework.Product;

public class UnderLinePen extends Product {
	private char ulchar;

	public UnderLinePen(char ulchar) {
		this.ulchar = ulchar;
	}

	@Override
	public void use(String s) {
		int length = s.getBytes().length;
		System.out.println("\"" + s + "\"");
		System.out.print(" ");
		for (int i = 0; i < length; i++) {
			System.out.print(ulchar);
		}
		System.out.println("");
	}

}
"Hello World!"
 ------------
****************
* Hello World! *
****************
///////////////
/ Hello World /
///////////////