ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Java] λ””μžμΈ νŒ¨ν„΄(Decorator νŒ¨ν„΄)
    Android/Java 2021. 1. 8. 11:14
    λ°˜μ‘ν˜•
    • 이 글은 "μΈν”„λŸ° Java λ””μžμΈ νŒ¨ν„΄" κ°•μ˜λ₯Ό λ“£κ³  κ³΅λΆ€ν•œ λ‚΄μš©μ„ λ°”νƒ•μœΌλ‘œ 글을 μž‘μ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

    Decorator νŒ¨ν„΄μ΄λž€?

    • μž‘μ€ 클래슀λ₯Ό μ€‘μ μœΌλ‘œ 상속과 합성을 μ΄μš©ν•˜μ—¬ 큰 클래슀λ₯Ό λ§Œλ“œλŠ” νŒ¨ν„΄μž…λ‹ˆλ‹€.

    • μ€‘μ‹¬λ˜λŠ” ν΄λž˜μŠ€λŠ” λ³€κ²½ 없이 μΆ”κ°€ 클래슀λ₯Ό μƒμ„±ν•˜μ—¬ 상속을 ν•˜λ©΄μ„œ κΈ°λŠ₯이 μΆ”κ°€λ˜λŠ” λ°©μ‹μž…λ‹ˆλ‹€.

    • λͺ¨λ°”일 UI κ°œλ°œμ— 주둜 많이 ν™œμš©λ˜λŠ” μžˆλŠ” νŒ¨ν„΄μž…λ‹ˆλ‹€.


    Decorator μ‚¬μš©

    public abstract class Widget {
    
        public abstract int getColumns();
    
        public abstract int getRows();
    
        public abstract String getRowText(int row);
    
        public void show(){
            for (int i = 0; i < getRows(); i++) {
                System.out.println(getRowText(i));
            }
        }
    }
    public class TextWidget extends Widget {
    
        private String text;
    
        public TextWidget(String text) {
            this.text = text;
        }
    
        @Override
        public int getColumns() {
            return text.length();
        }
    
        @Override
        public int getRows() {
            return 1;
        }
    
        @Override
        public String getRowText(int row) {
            return row == 0? text:null;
        }
    }

    λ‹€μŒμ˜ κΈ°λ³Έ 좔상 클래슀인 Widgetκ³Ό Widget을 상속받은 TextWidget이 μ€‘μ λ˜λŠ” ν΄λž˜μŠ€μž…λ‹ˆλ‹€.
    TextWidget ν΄λž˜μŠ€μ—μ„œλŠ” κ°„λ‹¨νžˆ textλ₯Ό 좜λ ₯ν•˜λŠ” 클래슀이고 변경없이 λ‹€λ₯Έ 클래슀의 상속과 ν•©μ„±λ§ŒμœΌλ‘œ μΆ”κ°€ κΈ°λŠ₯을 κ΅¬ν˜„ν•΄λ³΄λ„λ‘ ν•˜κ² μŠ΅λ‹ˆλ‹€.


    public abstract class Border extends Widget {
    
        protected Widget widget;
    
        public Border(Widget widget) {
            this.widget = widget;
        }
    }

    BorderλΌλŠ” μΆ”μƒν΄λž˜μŠ€λ₯Ό λ§Œλ“€μ–΄μ„œ Widget을 λ©€λ²„λ³€μˆ˜λ‘œ κ°€μ§ˆ 수 μžˆλ„λ‘ ν•©λ‹ˆλ‹€.

    public class SideBorder extends Border {
    
        public SideBorder(Widget widget) {
            super(widget);
        }
    
        @Override
        public int getColumns() {
            return widget.getColumns()+2;
        }
    
        @Override
        public int getRows() {
            return widget.getRows();
        }
    
        @Override
        public String getRowText(int row) {
            return "("+widget.getRowText(row)+")";
        }
    }
    public class FullBorder extends Border{
    
        public FullBorder(Widget widget) {
            super(widget);
        }
    
        @Override
        public int getColumns() {
            return widget.getColumns()+2;
        }
    
        @Override
        public int getRows() {
            return widget.getRows()+2;
        }
    
        @Override
        public String getRowText(int row) {
            if(row == 0 || row == getRows()-1){
                return "+"+makeLine('-',widget.getColumns())+"+";
            }
            return "|"+widget.getRowText(row-1)+"|";
        }
    
        private String makeLine(char ch, int count){
            StringBuilder stringBuilder = new StringBuilder();
            for(int i=0;i<count;i++){
                stringBuilder.append(ch);
            }
            return stringBuilder.toString();
        }
    }

    Borderλ₯Ό μƒμ†ν•˜λŠ” SideBorder와 FullBorderλ₯Ό 톡해 μΆ”κ°€ κΈ°λŠ₯ 클래슀λ₯Ό κ΅¬ν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

    • 메인 클래슀인 TextWidget에 좔가적인 κΈ°λŠ₯인 FullBorder와 SideBorderλ₯Ό 톡해 λ§Žμ€ ν™•μž₯을 ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

    κ²°κ³Όν™”λ©΄

    public class Main{
        public static void main(String[] args) {
    
            new FullBorder(
                    new SideBorder(
                            new FullBorder(
                                    new SideBorder(
                                            new TextWidget("hello world")
                                    )
                            )
                    )
            ).show();
        }
    }

    Main을 μ‹€ν–‰ν•  경우 λ‹€μŒκ³Ό 같은 μ‹€ν–‰ κ²°κ³Όλ₯Ό 얻을 수 μžˆμŠ΅λ‹ˆλ‹€.

    λ°˜μ‘ν˜•
Designed by Tistory.