๋ชฉ์ฐจ
์ด๋ค ์ํฉ์์ ์ฐ์ผ๊น?
๋๊ธ ์์ฑ ์๋น์ค๋ ํํฐ๋ฅผ ๋ฐ์์ ๋์ํ๋ค.
์ด๋ ๊ณต๋ฐฑ์ ์๋ฅด๋ ํํฐ๊ฐ ์์ ๊ฒ์ด๊ณ , ์ด๋ค ๋ฌธ์์ด์ ์๋ฅด๋ ํํฐ๊ฐ ์์ ๊ฒ์ด๋ค.
๋ฌธ์์ด์ ์๋ฅด๋ ํํฐ๋ 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๋ฅผ ๊ตฌํํ๋ ํํฐ๋ค์ ์์ฑํด์ ๋ฐํ์ ์์ ๋์ ์ผ๋ก ์ ์ฉ๋ ์ ์๋๋ก ํ์๋ค!
'๐ฐ Design Pattern' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Flyweight Pattern | ํ๋ผ์ด์จ์ดํธ ํจํด (0) | 2022.08.16 |
---|---|
Facade Pattern | ํผ์ฌ๋ ํจํด (0) | 2022.08.16 |
Composite Pattern | ์ปดํฌ์ง ํจํด (0) | 2022.08.15 |
Bridge Pattern | ๋ธ๋ฆฟ์ง ํจํด (0) | 2022.08.15 |
Adapter Pattern | ์ด๋ํฐํจํด (0) | 2022.08.14 |