PDHBE / study

기술 서적 Study
0 stars 0 forks source link

[GoF 디자인패턴] 데코레이터 (Decorator) 패턴 #13

Open leeyuunsung opened 2 years ago

leeyuunsung commented 2 years ago

데코레이터 패턴 1부 - 패턴 소개

데코레이터 패턴이 필요한 예

public static void main(String[] args) {
    Client client = new Client(new SpamFilteringCommentService());
    client.writeComment("오징어게임");
    client.writeComment("보는게 하는거 보다 재밌을 수가 없지...");
    client.writeComment("http://whiteship.me");
}

데코레이터 패턴 아키텍처

스크린샷 2021-11-27 오후 2 43 22

데코레이터 패턴 2부 - 패턴 적용하기

main

public class App {

    private static boolean enabledSpamFilter = true;

    private static boolean enabledTrimming = true;

    private static boolean enabledUpperCase = true;

    public static void main(String[] args) {
        CommentService commentService = new DefaultCommentService();

        if (enabledSpamFilter) {
            commentService = new SpamFilteringCommentDecorator(commentService);
        }

        if (enabledTrimming) {
            commentService = new TrimmingCommentDecorator(commentService);
        }

        if (enabledUpperCase) {
            commentService = new UpperCaseDecorator(commentService);
        }

        Client client = new Client(commentService);
        client.writeComment("오징어게임");
        client.writeComment("보는게 하는거 보다 재밌을 수가 없지...");
        client.writeComment("http://whiteship.me");
        client.writeComment("whiteship.me");
    }
}

CommentService (Interface)

public interface CommentService {

    void addComment(String comment);
}

CommentDecorator (Concrete class)

public class CommentDecorator implements CommentService {

    private CommentService commentService;

    public CommentDecorator(CommentService commentService) {
        this.commentService = commentService;
    }

    @Override
    public void addComment(String comment) {
        commentService.addComment(comment);
    }
}

DefaultCommentService (Concrete class)

public class DefaultCommentService implements CommentService {
    @Override
    public void addComment(String comment) {
        System.out.println(comment);
    }
}

SpamFilteringCommentDecorator (Concrete class)

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");
    }
}

TrimmingCommentDecorator (Concrete class)

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 Client {
  public static void main(String[] args) {
    // 기본 도로 표시 + 차선 표시 + 교통량 표시 + 교차로 표시
    Display roadWithCrossingLaneAndTraffic =
      new LaneDecorator(               // 차선 표시
      new TrafficDecorator(            // 교통량 표시
      new CrossingDecorator(           // 교차로 표시
      new RoadDisplay())));            // 기본 도로 표시
    roadWithCrossingLaneAndTraffic.draw();
  }
}
leeyuunsung commented 2 years ago

데코레이터 패턴 3부 - 장점과 단점

장점

단점

참고 링크


데코레이터 패턴 4부 - 자바와 스프링에서 찾아보는 패턴

자바

스프링