기타/Java

[Java]Design Pattern-Decorator Pattern

stonesy 2021. 10. 28. 12:47
728x90
public interface AbstractTextLine {
    public abstract void print();
}
public class TextLine implements AbstractTextLine{
    String text;

    public void setter(String text){
        this.text = text;
    }

    @Override
    public void print(){
        System.out.println(text);
    }
}
public abstract class DecoTextLine implements AbstractTextLine{
    protected TextLine textLine;
    public abstract void decorate();
    @Override
    public void print(){
        textLine.print();
    }
}
public class A_Deco extends DecoTextLine{
    public A_Deco(TextLine textLine){
        this.textLine = textLine;
    }
    @Override
    public void decorate(){
        textLine.text = textLine.text+"*";
    }
}
public class E_Deco extends DecoTextLine{
    public E_Deco(TextLine textLine){
        this.textLine = textLine;
    }
    @Override
    public void decorate(){
        textLine.text = "="+textLine.text+"=";
    }
}
public class G_Deco extends DecoTextLine{
    public G_Deco(TextLine textLine){
        this.textLine = textLine;
    }
    @Override
    public void decorate(){
        textLine.text = "<"+textLine.text+">";
    }
}
public class main {
    public static void main(String[] args){
        TextLine myText = new TextLine();
        myText.setter("Hello World!");

        A_Deco aDeco = new A_Deco(myText);
        aDeco.decorate();
        E_Deco eDeco = new E_Deco(myText);
        eDeco.decorate();
        G_Deco gDeco = new G_Deco(myText);
        gDeco.decorate();
        gDeco.decorate();

        myText.print();
    }
}
728x90

'기타 > Java' 카테고리의 다른 글

[Design Pattern]Command Pattern  (0) 2021.11.16
[Java]Design Pattern-Singleton  (0) 2021.11.03
[Java]Design Pattern-Simple Factory  (0) 2021.10.28
[Java]Design Pattern-Abstract Factory  (0) 2021.10.27
[Java]Design Pattern-Factory Method  (0) 2021.10.25