๐ŸŽฐ Design Pattern

Decorator Pattern | ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ํŒจํ„ด

์—ฐ_์šฐ๋ฆฌ 2022. 8. 16. 21:22
๋ฐ˜์‘ํ˜•

๋ชฉ์ฐจ

     

     

     

     

    ์–ด๋–ค ์ƒํ™ฉ์—์„œ ์“ฐ์ผ๊นŒ?

    ๋Œ“๊ธ€ ์ž‘์„ฑ ์„œ๋น„์Šค๋Š” ํ•„ํ„ฐ๋ฅผ ๋ฐ›์•„์„œ ๋™์ž‘ํ•œ๋‹ค. 

    ์ด๋•Œ ๊ณต๋ฐฑ์„ ์ž๋ฅด๋Š” ํ•„ํ„ฐ๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๊ณ , ์–ด๋–ค ๋ฌธ์ž์—ด์„ ์ž๋ฅด๋Š” ํ•„ํ„ฐ๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๋‹ค.

    ๋ฌธ์ž์—ด์„ ์ž๋ฅด๋Š” ํ•„ํ„ฐ๋Š” new Client(new SpamFilter()); ๋กœ ์‚ฌ์šฉํ•˜๊ณ 

    ๊ณต๋ฐฑ์„ ์ž๋ฅด๋Š” ํ•„ํ„ฐ๋Š” new Client(new TrimFilter()); ๋กœ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค.

     

    ๊ทธ๋ ‡๋‹ค๋ฉด ๋ฌธ์ž์—ด๊ณผ ๊ณต๋ฐฑ ๋‘˜๋‹ค ์ž๋ฅด๋Š” ํ•„ํ„ฐ๋Š” ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์„๊นŒ??

    SpamAndTrimFilter? 

     

    ์—ฌ๊ธฐ์„œ ํ•„ํ„ฐ์˜ ๊ธฐ๋Šฅ์ด ๋” ์ถ”๊ฐ€๋œ๋‹ค๋ฉด..?

    SpamAndTrimAnd...And....Filter..?

     

    ๊ฐ ํ•„ํ„ฐ๋“ค์˜ ๊ธฐ๋Šฅ์€ ๊ทธ๋Œ€๋กœ ๋†”๋‘๋ฉด์„œ, ๋Ÿฐํƒ€์ž„ ์‹œ์— ์ด ํ•„ํ„ฐ๋“ค์ด ์กฐํ•ฉ๋˜๋ฉฐ ๋™์ž‘ํ•˜๊ฒŒ ํ•  ์ˆ˜๋Š” ์—†์„๊นŒ??

     

     

     

    ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ํŒจํ„ด์ด๋ž€?

    ๋Ÿฐํƒ€์ž„์—์„œ ๊ธฐ์กด ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์œผ๋ฉด์„œ ๋ถ€๊ฐ€์ ์ธ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ํŒจํ„ด์ด๋‹ค.

     

    ๋Ÿฐํƒ€์ž„์—์„œ ์œ ์—ฐํ•˜๊ฒŒ ๊ฐ์ฒด๋ฅผ ์กฐ๋ฆฝํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค!

     

     

     

    ์žฅ์ 

    - ์ƒˆ๋กœ์šด ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ๊ธฐ์กด ๊ธฐ๋Šฅ์„ ์กฐํ•ฉํ•  ์ˆ˜ ์žˆ๋‹ค

    - ์ปดํŒŒ์ผ ์‹œ์ ์ด ์•„๋‹Œ ๋Ÿฐํƒ€์ž„์— ๋™์ ์œผ๋กœ ๊ธฐ๋Šฅ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค

     

     

    ๋‹จ์ 

    - ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์กฐ๋ฆฝ ์ฝ”๋“œ๊ฐ€ ๋ณต์žกํ•˜๋‹ค

     

     

     

    ๊ตฌํ˜„์ฝ”๋“œ

    ๊ธฐ์กด์ฝ”๋“œ

    public class CommentService {
        public void addComment(String comment) {
            System.out.println(comment);
        }
    }
    -------------------------------------------------------------------
    public class TrimmingCommentService extends CommentService {
        @Override
        public void addComment(String comment) {
            super.addComment(trim(comment));
        }
    
        private String trim(String comment) {
            return comment.replace("...", "");
        }
    }
    -------------------------------------------------------------------
    public class SpamFilteringCommentService extends CommentService {
        @Override
        public void addComment(String comment) {
            boolean isSpam = isSpam(comment);
            if (!isSpam) {
                super.addComment(comment);
            }
        }
        private boolean isSpam(String comment) {
            return comment.contains("http");
        }
    }
    public class Client {
    
        private CommentService commentService;
    
        public Client(CommentService commentService) {
            this.commentService = commentService;
        }
    
        private void writeComment(String comment) {
            commentService.addComment(comment);
        }
    
        public static void main(String[] args) {
            Client client = new Client(new SpamFilteringCommentService());   //โญ
            client.writeComment("์˜ค์ง•์–ด๊ฒŒ์ž„");
            client.writeComment("๋ณด๋Š”๊ฒŒ ํ•˜๋Š”๊ฑฐ ๋ณด๋‹ค ์žฌ๋ฐŒ์„ ์ˆ˜๊ฐ€ ์—†์ง€...");
            client.writeComment("http://whiteship.me");
        }
    
    }

    โญ ๋ถ€๋ถ„์ด ๊ณ„์† ๋ณ€ํ•˜๊ฒŒ๋œ๋‹ค.

    ์ŠคํŒธ๋ฌธ์ž์—ด์„ ๊ฑฐ๋ฅด๊ณ  ์‹ถ๋‹ค๋ฉด SpamFilter... ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•˜๊ณ ์‹ถ๋‹ค๋ฉด TrimFilter๊ฐ€ ๋™์ž‘๋˜๊ฒŒ ํ•˜๊ณ ์‹ถ๋‹ค!

     

     

     

    ๋ณ€๊ฒฝ์ฝ”๋“œ

    public interface CommentService {
        void addComment(String comment);
    }
    public class DefaultCommentService implements CommentService {
        @Override
        public void addComment(String comment) {
            System.out.println(comment);
        }
    }
    public class CommentDecorator implements CommentService {
    
        private CommentService commentService;
    
        public CommentDecorator(CommentService commentService) {
            this.commentService = commentService;
        }
    
        @Override
        public void addComment(String comment) {
            commentService.addComment(comment);
        }
    }
    public class SpamFilteringCommentDecorator extends CommentDecorator {
    
        public SpamFilteringCommentDecorator(CommentService commentService) {
            super(commentService);
        }
    
        @Override
        public void addComment(String comment) {
            if (isNotSpam(comment)) {
                super.addComment(comment);
            }
        }
    
        private boolean isNotSpam(String comment) {
            return !comment.contains("http");
        }
    }
    ------------------------------------------------------------------------
    public class TrimmingCommentDecorator extends CommentDecorator {
    
        public TrimmingCommentDecorator(CommentService commentService) {
            super(commentService);
        }
    
        @Override
        public void addComment(String comment) {
            super.addComment(trim(comment));
        }
    
        private String trim(String comment) {
            return comment.replace("...", "");
        }
    }
    public class App {
    
        private static boolean enabledSpamFilter = true;
    
        private static boolean enabledTrimming = true;
    
        public static void main(String[] args) {
            CommentService commentService = new DefaultCommentService();
    
            if (enabledSpamFilter) {
                commentService = new SpamFilteringCommentDecorator(commentService);
            }
    
            if (enabledTrimming) {
                commentService = new TrimmingCommentDecorator(commentService);
            }
    
            Client client = new Client(commentService);
            client.writeComment("์˜ค์ง•์–ด๊ฒŒ์ž„");
            client.writeComment("๋ณด๋Š”๊ฒŒ ํ•˜๋Š”๊ฑฐ ๋ณด๋‹ค ์žฌ๋ฐŒ์„ ์ˆ˜๊ฐ€ ์—†์ง€...");
            client.writeComment("http://whiteship.me");
        }
    }

    DefaultCommentService -> CommentService

    CommentDecorator -> CommentService

    SpamFilter -> CommentDecorator

    TrimFilter -> CommentDecorator

     

    ๋””ํดํŠธ๊ฐ€ ์•„๋‹Œ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๋Š” ์„œ๋น„์Šค๋“ค์ด ๋“ค์–ด์˜ค๋Š” CommentDecorator๋ฅผ ์ƒ์„ฑํ•˜๊ณ 

    CommentDecorator๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํ•„ํ„ฐ๋“ค์„ ์ƒ์„ฑํ•ด์„œ ๋Ÿฐํƒ€์ž„ ์‹œ์— ๋™์ ์œผ๋กœ ์ ์šฉ๋  ์ˆ˜ ์žˆ๋„๋ก ํ•˜์˜€๋‹ค!

     

     

     

    ๋ฐ˜์‘ํ˜•
    • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
    • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
    • ํŠธ์œ„ํ„ฐ ๊ณต์œ ํ•˜๊ธฐ
    • ๊ตฌ๊ธ€ ํ”Œ๋Ÿฌ์Šค ๊ณต์œ ํ•˜๊ธฐ
    • ์นด์นด์˜คํ†ก ๊ณต์œ ํ•˜๊ธฐ