@ AbstractClass (추상 클래스)
- 이 클래스에서는 탬플릿 메소드를 구현(정의)합니다.
package Template;
public abstract class AbstractDisplay {
public final void decorate(){
char topChar = getTopCharacter();
char leftChar = getLeftCharacter();
String str = getString();
char rightChar = getRightCharacter();
char bottomChar = getBottomCharacter();
for(int i=0; i<str.length() + 2; i++){
System.out.print(topChar);
}
System.out.println();
System.out.print(leftChar);
if (isUpperCase()){
System.out.print(str.toUpperCase());
}else{
System.out.print(str.toLowerCase());
}
System.out.print(rightChar);
System.out.println();
for(int i=0; i<str.length() + 2; i++){
System.out.print(bottomChar);
}
}
protected boolean isUpperCase() {
return true;
}
protected abstract char getTopCharacter();
protected abstract char getLeftCharacter();
protected abstract String getString();
protected abstract char getRightCharacter();
protected abstract char getBottomCharacter();
}
@ ConcreateClass(구현 클래스)
- 추상 클래스에서 정의된 메소드를 구체적으로 구현 합니다.
package Template;
public class StringDisplay extends AbstractDisplay {
@Override
protected char getBottomCharacter() {
return '#';
}
@Override
protected char getLeftCharacter() {
return '(';
}
@Override
protected char getRightCharacter() {
return ')';
}
@Override
protected String getString() {
return "sgdata";
}
@Override
protected char getTopCharacter() {
return '#';
}
}
@ Main Class
package Template;
public class Main {
public static void main(String[] args) {
AbstractDisplay decorator = new StringDisplay();
decorator.decorate();
}
}