Decorator pattern ๐ฟ
๊ธฐ๋ณธ ๊ฐ์ฒด์ ์ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ์ ๋์ ์ผ๋ก ์ ์ฐํ๊ฒ ์ฒจ๊ฐํ๋ ํจํด
์ฅ์
๋จ์
ํ์ฉ ์ํฉ
GUI ํดํท์์ ์ด๋ค ์ฌ์ฉ์ ์ธํฐํ์ด์ค ์์์๋ง scrolling๊ณผ ๊ฐ์ ํ๋์ด๋ border์ ๊ฐ์ ์์ฑ์ ์ถ๊ฐํ๋ ๊ฒฝ์ฐ, ํ๋์ ๊ฐ์ฒด์ ์์ฑ์ด ์ถ๊ฐ๋จ์ผ๋ก ๋ค๋ฅธ ์ฑ ์์ด ์ถ๊ฐ๋์ด์ผ ํ๋ค.
์ฆ, ์ ์ฒด ํด๋์ค์ ์๋ก์ด ๊ธฐ๋ฅ์ ์ถ๊ฐํ ํ์๋ ์์ง๋ง,
๊ฐ๋ณ์ ์ธ ๊ฐ์ฒด์ ์๋ก์ด ์ฑ
์์ ์ถ๊ฐํ ํ์๊ฐ ์์ ๊ฒฝ์ฐ์ ์ฌ์ฉํ๋ค.
์๋ฐ API ํ์ผ I/O ๊ด๋ จ ๋ถ๋ถ์์๋ ์ฌ์ฉ๋๋ค. ๐
BufferedReader bufferedreader = new BufferedReader(new FileReader(new File("daydream.txt")));
๊ตฌ์กฐ โ
Tree
DefaultTree
Decorator
Red, Blue
DecoratorPattern
๊ตฌํ ๐
interface Tree {
String decorate();
}
class DefaultTree implements Tree {
@Override
public String decorate() {
return "Tree";
}
}
abstract class Decorator implements Tree {
private Tree tree;
public Decorator(Tree tree) {
this.tree = tree;
}
@Override
public String decorate() {
return tree.decorate();
}
}
class Red extends Decorator {
public Red(Tree tree) {
super(tree);
}
public String addRed() {
return " with Red";
}
@Override
public String decorate() {
return super.decorate() + addRed();
}
}
class Blue extends Decorator {
public Blue(Tree tree) {
super(tree);
}
public String addBlue() {
return " with Blue";
}
@Override
public String decorate() {
return super.decorate() + addBlue();
}
}
public class DecoratorPattern {
public static void main(String[] args) {
Tree tree = new DefaultTree();
System.out.println(tree.decorate());
Tree treeWithRed = new Red(new DefaultTree());
System.out.println(treeWithRed.decorate());
Tree treeWithRedWithBlue = new Blue(new Red(new DefaultTree()));
System.out.println(treeWithRedWithBlue.decorate());
}
}
์คํ ๊ฒฐ๊ณผ ๐ง
Tree
Tree with Red
Tree with Red with Blue
์ ์คํ ๊ฒฐ๊ณผ์ฒ๋ผ ์๋ก์ด ๊ธฐ๋ฅ์ ์ ์ฐํ๊ฒ ๋ง๋ค๊ณ ์ถ๊ฐํ ์ ์๋ค.