liguofeng29’s blog

個人勉強用ブログだっす。

デザインパターン - Decorator

【サンプル】

 
Display.java
package decorator;
 
// 飾りと中身を同一視するためのクラス
public abstract class Display {
 
public abstract int getColumns();
public abstract int getRows();
public abstract String getText(int rowNo);
 
public void show() {
for(int i = 0; i < getRows(); i++){
System.out.println(getText(i));
}
}
}
 
 
 
 

 

Border.java
package decorator;
 
// 飾り抽象クラス
public abstract class Border extends Display{
 
protected Display display;
 
protected Border(Display display) {
this.display =  display;
}
 
}
 
 

 

StringDisplay.java
package decorator;
 
// 中身クラス
public class StringDisplay extends Display{
 
private String text;
 
public StringDisplay(String text) {
this.text = text;
}
 
@Override
public int getColumns() {
return text.getBytes().length;
}
 
@Override
public int getRows() {
return 1;
}
 
@Override
public String getText(int rowNo) {
return text;
}
 
}
 
 
 
SideBorder.java
package decorator;
 
// 飾りクラス、文字列両辺に指定文字で囲む
public class SideBorder extends Border{
 
private char borderChar;
 
protected SideBorder(Display display,  char borderChar) {
super(display);
this.borderChar = borderChar;
}
 
@Override
public int getColumns() {
return super.display.getColumns() + 2;
}
 
@Override
public int getRows() {
return super.display.getRows();
}
 
// 両辺へ指定文字を飾る
@Override
public String getText(int rowNo) {
return this.borderChar + super.display.getText(rowNo) + this.borderChar;
}
 
}
 
 
 
FullBorder.java
package decorator;
 
//飾りクラス、文字列周辺を指定文字で囲む
public class FullBorder extends Border {
private char borderChar;
 
protected FullBorder(Display display, char borderChar) {
super(display);
this.borderChar = borderChar;
}
 
@Override
public int getColumns() {
return super.display.getColumns() + 2;
}
 
@Override
public int getRows() {
return super.display.getRows() + 2;
}
 
@Override
public String getText(int rowNo) {
 
if (rowNo == 0) {
return makeLine();
} else if(rowNo == super.display.getRows() + 1) {
return makeLine();
} else {
return this.borderChar + super.display.getText(rowNo - 1) + this.borderChar;
}
}
 
private String makeLine() {
String line = "";
for(int i = 0; i < this.getColumns(); i++) {
line += this.borderChar;
}
 
return line;
}
}
 
 

 

Decorator.java
package decorator;
 
public class DecoratorTest {
 
public static void main(String[] args) {
 
// 文字そのまま表示する
Display d1 = new StringDisplay("Hello World.");
d1.show();
 
System.out.println("");
System.out.println("");
 
// #で両辺を囲む
Display d2 = new SideBorder(new StringDisplay("Hello World."), '#');
d2.show();
 
System.out.println("");
System.out.println("");
 
// #で一蹴囲む
Display d3 = new FullBorder(new StringDisplay("Hello World."), '#');
d3.show();
 
System.out.println("");
System.out.println("");
 
// 3回飾る
Display d4 = new FullBorder(
new SideBorder(
new FullBorder(
new StringDisplay("Hello World."), '-'),
'#'),
'@');
d4.show();
 
}
 
}
 
 

 

実行結果
 
 
 
 
##############
##############
 
 
@@@@@@@@@@@@@@@@@@
@#--------------#@
@#--------------#@
@@@@@@@@@@@@@@@@@@

文字の幅が違うため、結果が・・・ ・・・・・・・・・・・・ ・・・・・・・・・・・・・・・