design patterns : decorator
This commit is contained in:
25
design-pattern/gof/src/decorator/after/App.java
Normal file
25
design-pattern/gof/src/decorator/after/App.java
Normal file
@@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
14
design-pattern/gof/src/decorator/after/Client.java
Normal file
14
design-pattern/gof/src/decorator/after/Client.java
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
15
design-pattern/gof/src/decorator/after/CommentDecorator.java
Normal file
15
design-pattern/gof/src/decorator/after/CommentDecorator.java
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package decorator.after;
|
||||||
|
|
||||||
|
public interface CommentService {
|
||||||
|
|
||||||
|
void addComment(String comment);
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package decorator.after;
|
||||||
|
|
||||||
|
public class DefaultCommentService implements CommentService {
|
||||||
|
@Override
|
||||||
|
public void addComment(String comment) {
|
||||||
|
System.out.println(comment);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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("...", "");
|
||||||
|
}
|
||||||
|
}
|
||||||
22
design-pattern/gof/src/decorator/before/Client.java
Normal file
22
design-pattern/gof/src/decorator/before/Client.java
Normal file
@@ -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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package decorator.before;
|
||||||
|
|
||||||
|
public class CommentService {
|
||||||
|
public void addComment(String comment) {
|
||||||
|
System.out.println(comment);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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("...", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user