โ๏ธ Java Design-Pattern โ ซ - Composite
โ๏ธ Java Design-Pattern โ
ซ - Composite
๐
ใJAVA ์ธ์ด๋ก ๋ฐฐ์ฐ๋ ๋์์ธ ํจํด : ์ฝ๊ฒ ๋ฐฐ์ฐ๋ GoF์ 23๊ฐ์ง ๋์์ธ ํจํดใ
๋ฅผ ์ฝ๊ณ ์ ๋ฆฌํ ๊ธ์ ๋๋ค.
Composite
ํจํด์ด๋?
Composite
๋ ํผํฉ๋ฌผ, ๋ณตํฉ๋ฌผ์ด๋ผ๋ ๋ป์ผ๋ก, ๊ทธ๋ฆ๊ณผ ๋ด์ฉ๋ฌผ์ ๋์ผ์ํ์ฌ ์ฌ๊ท์ ์ธ ๊ตฌ์กฐ๋ฅผ ๋ง๋๋ ๋์์ธ ํจํด์ด๋ค.
์์ ํ๋ก๊ทธ๋จ
- ๋ค์์ ํ์ผ๊ณผ ๋๋ ํฐ๋ฆฌ๋ฅผ ๋์์ ์ผ๋ก ํํํ๋ ํ๋ก๊ทธ๋จ์ด๋ค.
File
ย ํด๋์ค๋ ํ์ผ์ ๋ํ๋ด๋ฉฐ,ยDirectory
ย ํด๋์ค๋ ๋๋ ํฐ๋ฆฌ๋ฅผ ๋ํ๋ธ๋ค.ยEntry
ย ํด๋์ค๋ ๊ทธ ๋์ ์ทจํฉํ๋ ํํ์ธ ๋๋ ํฐ๋ฆฌ ์ํธ๋ฆฌ๋ฅผ ๋ํ๋ธ๋ค.
Entry
ย ํด๋์ค
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public abstract class Entry {
public abstract String getName();
public abstract int getSize();
public void printList() {
printList("");
}
protected abstract void printList(String prefix);
@Override
public String toString() {
return getName() + "(" + getSize() + ")";
}
}
- ๋๋ ํฐ๋ฆฌ ์ํธ๋ฆฌ๋ฅผ ํํํ๋ ์ถ์ ํด๋์ค์ด๋ค.
- ํ์ ํด๋์ค๋กย
File
,ยDirectory
ย ํด๋์ค๊ฐ ๋ง๋ค์ด์ง๋ค. - ๋๋ ํฐ๋ฆฌ ์ํธ๋ฆฌ๋ ์ด๋ฆ๊ณผ ์ฌ์ด์ฆ๋ฅผ ๊ฐ๊ณ ์์ผ๋ฉฐ, ๊ฐ๊ฐ์ ์ป๋ย
getter
ย ๋ฉ์๋๋ ํ์ ํด๋์ค์ ๊ตฌํ์ ๋งก๊ธด๋ค. printList
ย ๋ฉ์๋๋ ์ธ์ ์ฌ๋ถ์ ๋ฐ๋ผ ์ค๋ฒ๋ก๋ฉ ๋์ด ์๋ค.
File
ย ํด๋์ค
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class File extends Entry {
private String name;
private int size;
public File(String name, int size) {
this.name = name;
this.size = size;
}
@Override
public String getName() {
return name;
}
@Override
public int getSize() {
return size;
}
@Override
protected void printList(String prefix) {
System.out.println(prefix + "/" + this);
}
}
printList
ย ๋ฉ์๋์ฒ๋ผ ๊ฐ์ฒด๋ฅผ ์ถ๋ ฅํ๋ฉด ์๋์ผ๋ก ํด๋น ๊ฐ์ฒด์ยtoString()
์ด ํธ์ถ๋๋ค.Entry
ย ํด๋์ค์์ ์ ์ธ๋ ์ถ์ ๋ฉ์๋๋ฅผ ๋ชจ๋ ๊ตฌํํ์ผ๋ฏ๋กยFile
ย ํด๋์ค๋ ์ถ์ ํด๋์ค๊ฐ ์๋๋ค.
Directory
ย ํด๋์ค
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.ArrayList;
import java.util.List;
public class Directory extends Entry {
private String name;
private List<Entry> directory = new ArrayList<>();
public Directory(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
@Override
public int getSize() {
int size = 0;
for (Entry entry : directory) {
size += entry.getSize();
}
return size;
}
@Override
protected void printList(String prefix) {
System.out.println(prefix + "/" + this);
for (Entry entry : directory) {
entry.printList(prefix + "/" + name);
}
}
public Entry add(Entry entry) {
directory.add(entry);
return this;
}
}
Directory
ย ํด๋์ค๋ ์ฌ์ด์ฆ๋ฅผ ๋์ ์ผ๋ก ๊ณ์ฐํด์ ๊ตฌํ๋ฏ๋กยsize
ย ํ๋๊ฐ ๋ฐ๋ก ์๋ค.size += entry.getSize();
๋ฅผ ํตํด ์ฌ์ด์ฆ๋ฅผ ๊ตฌํ๋๋ฐ,ยentry
๊ฐยFile
ย ํด๋์ค์ ์ธ์คํด์ค์ด๋ ,ยDirectory
ย ํด๋์ค์ ์ธ์คํด์ค์ด๋ ยEntry
์ ํ์ ํด๋์ค์ ์ธ์คํด์ค์ด๋ฏ๋ก ์์ฌํ๊ณ ยgetSize
ย ๋ฉ์๋๋ฅผ ํธ์ถํ ์ ์๋ค.- ์ด์ธ์๋ย
add
,ยprintList
ย ๋ฑ์ ๋ฉ์๋๋ยentry
๊ฐ ํ์ผ์ธ์ง ๋๋ ํ ๋ฆฌ์ธ์ง ์กฐ์ฌํ์ง ์๊ณ ๊ทธ๋ฆ๊ณผ ๋ด์ฉ๋ฌผ์ ๋์ผ์ํ์ฌ ์ฒ๋ฆฌํ๋ค.
Main
ย ํด๋์ค
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class Main {
public static void main(String[] args) {
System.out.println("Making root entries...");
Directory rootDir = new Directory("root");
Directory binDir = new Directory("bin");
Directory tmpDir = new Directory("tmp");
Directory usrDir = new Directory("usr");
rootDir.add(binDir);
rootDir.add(tmpDir);
rootDir.add(usrDir);
binDir.add(new File("vi", 10000));
binDir.add(new File("latex", 20000));
rootDir.printList();
System.out.println();
System.out.println("Making user entries...");
Directory youngjin = new Directory("youngjin");
Directory gildong = new Directory("gildong");
Directory dojun = new Directory("dojun");
usrDir.add(youngjin);
usrDir.add(gildong);
usrDir.add(dojun);
youngjin.add(new File("diary.html", 100));
youngjin.add(new File("Composite.java", 200));
gildong.add(new File("memo.txt", 300));
dojun.add(new File("game.doc", 400));
dojun.add(new File("junk.mail", 500));
rootDir.printList();
}
}
Main
ย ํด๋์ค์ ํ์ผ๊ณผ ๋๋ ํฐ๋ฆฌ ๊ณ์ธต์ ๊ทธ๋ฆฌ๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
root
โโโ bin
โ โโโ vi
โ โโโ latex
โโโ tmp
โโโ usr
โโโ yongjin
โ โโโ diary.html
โ โโโ Composite.java
โโโ gildong
โ โโโ memo.txt
โโโ dojun
โโโ game.doc
โโโ junk.mail
Composite
ย ํจํด์ ๋ฑ์ฅ์ธ๋ฌผ
File
ย ํด๋์ค
- ๋ด์ฉ๋ฌผ์ ๋ํ๋ด๋ ์ย
Leaf
ย ์ญํ ์ ํ๋ค. - ์ด ์์๋ ๋ค๋ฅธ ๊ฒ์ ๋ฃ์ ์ ์๋ค.
Directory
ย ํด๋์ค
- ์ ์ญํ ์ด๋ ๋ณตํฉ์ฒด ์ญํ ์ ๋ฃ์ ์ ์๋ ๋ณตํฉ์ฒดย
Composite
ย ์ญํ ์ ํ๋ค.
Entry
ย ํด๋์ค
- ์ ์ญํ ๊ณผ ๋ณตํฉ์ฒด ์ญํ ์ ๋์ผ์ํ๊ธฐ ์ํ ์์ ํด๋์คย
Component
ย ์ญํ ์ ํ๋ค.
Main
ย ํด๋์ค
- ํด๋น ํจํด์ ์ฌ์ฉ์์ธ ์๋ขฐ์ย
Client
ย ์ญํ ์ ํ๋ค.
์ฑ ์์ ์ ์ํ๋ ํํธ
๋ณต์์ ๋จ์ ๋์ผ์ํ๊ธฐ
Composite
ย ํจํด์ ๊ทธ๋ฆ๊ณผ ๋ด์ฉ๋ฌผ์ ๋์ผ์ํ๋ ํจํด์ธ๋ฐ, ์ด๋ ๋ณต์์ ๋จ์๋ฅผ ๋์ผ์ํ๋ค๊ณ ๋งํ ์ ์๋ค.- ์๋ฅผ ๋ค์ด ํค๋ณด๋ ์ ๋ ฅ, ํ์ผ ์ ๋ ฅ, ๋คํธ์ํฌ ์ ๋ ฅ ๋ฑ์ ํ๋ก๊ทธ๋จ ๋์ ํ ์คํธ๋ฅผ ๋ชจ์์ ํ ๋ ํด๋น ํจํด์ ์ฌ์ฉํ ์ ์๋ค.
- ์
๋ ฅ ํ
์คํธ๋ฅผ ๋ชจ์ย
InputTest
๋ผ๋ ์ ๋ ฅ ํ ์คํธ๋ก ๋ง๋ค ์ ์๊ณ , ๋์ผํ๊ฒ ์ถ๋ ฅ ํ ์คํธ๋ฅผ ๋ง๋ค ์ ์๋ค. - ์ฌ์ง์ดย
InputOutputTest
๋ผ๋ ์ ์ถ๋ ฅ ํ ์คํธ๊น์ง ๋ง๋ค ์ ์๋ค.
add
๋ ์ด๋์ ๋์ด์ผ ํ ๊น?
- ์์ ํ๋ก๊ทธ๋จ๊ณผย
GoF
ย ์ฑ ์์๋ยadd
,ยremove
,ยgetChild
ย ๋ฑ ์์์ ์กฐ์ํ๋ ๋ฉ์๋๋ฅผ ๋ณตํฉ์ฒด ์ญํ ์์ ์ ์ํ์๋ค. - ๋ง์ฝ ์ ์ญํ ์์ ์์์ ์กฐ์ํ๋ ์์ฒญ์ด ๋ฐ์ํ๋ฉด ์ค๋ฅ ์ฒ๋ฆฌ๊ฐ ํ์ํ๋ค.
- ๊ทธ๋ ๋ค๋ฉดย
Directory
ย ํด๋์ค์ยEntry
ย ํด๋์ค ์ค์์๋ ์์์ ์กฐ์ํ๋ ๋ฉ์๋๋ฅผ ์ด๋์ ๋๋ ๊ฒ์ด ๋ ์ข์๊น? - ์ด๋ ๊ทธ๋ฆ๊ณผ ๋ด์ฉ๋ฌผ์ ๋์ผ์ํ ๊ฒฐ๊ณผ๋ก ์ป์ด์ง๋ ๊ฒ์ ๋ฌด์์ธ๊ฐ?์ ๋ํ ๋๋ต์ด๋ค.
- ๊ฒฐ๋ก ์ ์ผ๋ก ์ค๊ณ์๋ ํด๋น ํด๋์ค๊ฐ ๊ฐ์ ธ์ผ ํ ์ฑ ๋ฌด๋ฅผ ๋ช ํํ ํด์ผํ๋ฉฐ, ์ด๋ฌํ ๋ฐฉํฅ์ ๋ ์ ํฉํ ์์น์ ๋ฉ์๋๋ฅผ ์์น์ํค๋ ๊ฒ์ด ์ ์ ํ๋ค.
์ฌ๊ท์ ๊ตฌ์กฐ๋ ๋ชจ๋ ์ฅ๋ฉด์์ ๋ฑ์ฅํ๋ค.
- ์ผ๋ฐ์ ์ผ๋ก ํธ๋ฆฌ ๊ตฌ์กฐ๋ก ๋ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ย
Composite
ย ํจํด์ ํด๋นํ๋ค. - ๋ฌธ์ฅ์ ๊ธ๋จธ๋ฆฌ ๊ธฐํธ ํญ๋ชฉ ์์ ๋ค์ ํญ๋ชฉ์ด ํฌํจ๋๋ ๊ฒ์ ๊ทธ ์๋ก ๋ค ์ ์๋ค.
This post is licensed under CC BY 4.0 by the author.