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

Decoratorパターン

row - 1でハマる。

% find . -name "*.java" -print0 |xargs -0 cat                                []
public abstract class Border extends Display {
	protected Display display;

	protected Border(Display display) {
		this.display = display;
	}

}
public abstract class Display {
	public abstract int getColumns();

	public abstract int getRows();

	public abstract String getRowText(int row);

	public final void show() {
		for (int i = 0; i < getRows(); i++) {
			System.out.println(getRowText(i));
		}
	}

}
public class FullBorder extends Border {
	public FullBorder(Display display) {
		super(display);
	}

	@Override
	public int getColumns() {
		return 1 + display.getColumns() + 1;
	}

	@Override
	public int getRows() {
		return 1 + display.getRows() + 1;
	}

	@Override
	public String getRowText(int row) {
		if (row == 0) {
			return "+" + makeLine('-', display.getColumns()) + '+';
		} else if (row == display.getRows() + 1) {
			return "+" + makeLine('-', display.getColumns()) + '+';
		} else {
			return "|" + display.getRowText(row - 1) + "|";
		}
	}

	private String makeLine(char ch, int count) {
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < count; i++) {
			buf.append(ch);
		}
		return buf.toString();
	}

}
import java.util.Arrays;

public class Main {

	public static void main(String[] args) {
		Display d1 = new StringDisplay("Hello World.");
		Display d2 = new SideBorder(d1, '#');
		Display d3 = new FullBorder(d2);
		d1.show();
		d2.show();
		d3.show();

		Display d4 = new MultiStringDisplay(Arrays.asList("aaaa", "bbbbbb",
				"ccccccccccc"));
		Display d5 = new SideBorder(d4, '#');
		Display d6 = new FullBorder(d5);
		d4.show();
		d5.show();
		d6.show();
	}

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

public class MultiStringDisplay extends Display {
	List<String> list = new ArrayList<String>();
	private int maxLen = 0;

	public MultiStringDisplay(List<String> list) {
		for (String str : list) {
			this.list.add(str);
			int l = str.getBytes().length;
			if (maxLen < l) {
				maxLen = l;
			}
		}
	}

	@Override
	public int getColumns() {
		return maxLen;
	}

	@Override
	public int getRows() {
		return list.size();
	}

	@Override
	public String getRowText(int row) {
		String format = "%1$" + maxLen + "s";
		return String.format(format, list.get(row));
	}
}
public class SideBorder extends Border {
	private char borderChar;

	public SideBorder(Display display, char ch) {
		super(display);
		this.borderChar = ch;
	}

	@Override
	public int getColumns() {
		return 1 + display.getColumns() + 1;
	}

	@Override
	public int getRows() {
		return display.getRows();
	}

	@Override
	public String getRowText(int row) {
		return borderChar + display.getRowText(row) + borderChar;
	}

}
public class StringDisplay extends Display {

	private String string;

	public StringDisplay(String string) {
		this.string = string;
	}

	@Override
	public int getColumns() {
		return string.getBytes().length;
	}

	@Override
	public int getRows() {
		return 1;
	}

	@Override
	public String getRowText(int row) {
		if (row == 0) {
			return string;
		} else {
			return null;
		}
	}

}
Hello World.
#Hello World.#
+--------------+
|#Hello World.#|
+--------------+
       aaaa
     bbbbbb
ccccccccccc
#       aaaa#
#     bbbbbb#
#ccccccccccc#
+-------------+
|#       aaaa#|
|#     bbbbbb#|
|#ccccccccccc#|
+-------------+