diff --git a/design-pattern/gof/src/decorator/after/App.java b/design-pattern/gof/src/decorator/after/App.java new file mode 100644 index 00000000..f35d6a3b --- /dev/null +++ b/design-pattern/gof/src/decorator/after/App.java @@ -0,0 +1,25 @@ +package decorator.after; + +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"); + } +} diff --git a/design-pattern/gof/src/decorator/after/Client.java b/design-pattern/gof/src/decorator/after/Client.java new file mode 100644 index 00000000..e4c5adc0 --- /dev/null +++ b/design-pattern/gof/src/decorator/after/Client.java @@ -0,0 +1,14 @@ +package decorator.after; + +public class Client { + + private CommentService commentService; + + public Client(CommentService commentService) { + this.commentService = commentService; + } + + public void writeComment(String comment) { + commentService.addComment(comment); + } +} diff --git a/design-pattern/gof/src/decorator/after/CommentDecorator.java b/design-pattern/gof/src/decorator/after/CommentDecorator.java new file mode 100644 index 00000000..ef664ef2 --- /dev/null +++ b/design-pattern/gof/src/decorator/after/CommentDecorator.java @@ -0,0 +1,15 @@ +package decorator.after; + +public class CommentDecorator implements CommentService { + + private CommentService commentService; + + public CommentDecorator(CommentService commentService) { + this.commentService = commentService; + } + + @Override + public void addComment(String comment) { + commentService.addComment(comment); + } +} diff --git a/design-pattern/gof/src/decorator/after/CommentService.java b/design-pattern/gof/src/decorator/after/CommentService.java new file mode 100644 index 00000000..ef3f75c9 --- /dev/null +++ b/design-pattern/gof/src/decorator/after/CommentService.java @@ -0,0 +1,6 @@ +package decorator.after; + +public interface CommentService { + + void addComment(String comment); +} diff --git a/design-pattern/gof/src/decorator/after/DefaultCommentService.java b/design-pattern/gof/src/decorator/after/DefaultCommentService.java new file mode 100644 index 00000000..0bc99d6e --- /dev/null +++ b/design-pattern/gof/src/decorator/after/DefaultCommentService.java @@ -0,0 +1,8 @@ +package decorator.after; + +public class DefaultCommentService implements CommentService { + @Override + public void addComment(String comment) { + System.out.println(comment); + } +} diff --git a/design-pattern/gof/src/decorator/after/SpamFilteringCommentDecorator.java b/design-pattern/gof/src/decorator/after/SpamFilteringCommentDecorator.java new file mode 100644 index 00000000..74cb5d7f --- /dev/null +++ b/design-pattern/gof/src/decorator/after/SpamFilteringCommentDecorator.java @@ -0,0 +1,18 @@ +package decorator.after; + +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"); + } +} diff --git a/design-pattern/gof/src/decorator/after/TrimmingCommentDecorator.java b/design-pattern/gof/src/decorator/after/TrimmingCommentDecorator.java new file mode 100644 index 00000000..1e000087 --- /dev/null +++ b/design-pattern/gof/src/decorator/after/TrimmingCommentDecorator.java @@ -0,0 +1,16 @@ +package decorator.after; + +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("...", ""); + } +} diff --git a/design-pattern/gof/src/decorator/before/Client.java b/design-pattern/gof/src/decorator/before/Client.java new file mode 100644 index 00000000..d9398272 --- /dev/null +++ b/design-pattern/gof/src/decorator/before/Client.java @@ -0,0 +1,22 @@ +package decorator.before; + +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"); + } + +} diff --git a/design-pattern/gof/src/decorator/before/CommentService.java b/design-pattern/gof/src/decorator/before/CommentService.java new file mode 100644 index 00000000..9c83d2a8 --- /dev/null +++ b/design-pattern/gof/src/decorator/before/CommentService.java @@ -0,0 +1,7 @@ +package decorator.before; + +public class CommentService { + public void addComment(String comment) { + System.out.println(comment); + } +} diff --git a/design-pattern/gof/src/decorator/before/SpamFilteringCommentService.java b/design-pattern/gof/src/decorator/before/SpamFilteringCommentService.java new file mode 100644 index 00000000..4ddba94b --- /dev/null +++ b/design-pattern/gof/src/decorator/before/SpamFilteringCommentService.java @@ -0,0 +1,16 @@ +package decorator.before; + +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"); + } +} diff --git a/design-pattern/gof/src/decorator/before/TrimmingCommentService.java b/design-pattern/gof/src/decorator/before/TrimmingCommentService.java new file mode 100644 index 00000000..cd2d15e8 --- /dev/null +++ b/design-pattern/gof/src/decorator/before/TrimmingCommentService.java @@ -0,0 +1,14 @@ +package decorator.before; + +public class TrimmingCommentService extends CommentService { + + @Override + public void addComment(String comment) { + super.addComment(trim(comment)); + } + + private String trim(String comment) { + return comment.replace("...", ""); + } + +}