Notice
Recent Posts
Recent Comments
Link
250x250
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- spring
- jsp
- BACK-END
- 쿼리
- java
- 자바스크립트
- oracle
- web
- JavaScript
- Next.js
- 프론트엔드
- 코드테스트
- 백엔드
- 알고리즘
- 스프링부트
- 프런트엔드
- jpa
- 오라클
- MySQL
- node.js
- 스프링
- 코드 테스트
- 정리
- 미니정리
- SQL
- 자바
- 서버
- 디자인 패턴
- 프로그래머스
- 데이터베이스
Archives
- Today
- Total
참치코더의 꿈 메모장
디자인 패턴 / 컴포지트 패턴 정리 본문
728x90

컴포지트 패턴
- 객체들을 트리 구조로 구성해서 단일 객체와 복합 객체를 동일하게 다루는 패턴이다.
- 즉 부분- 전체 관계를 표현하는 디자인 패턴이다.
구조
Component (추상화된 공통 인터페이스)
- Leaf와 Composite 모두가 구현해야 하는 공통 인터페이스
Leaf (단일 객체)
- 실제 작업을 수행하는 단일 객체
- 자식이 없음
Composite (복합 객체)
- 여러 Component를 자식으로 가질 수 있는 객체
- 내부에서 자식들의 작업을 위임하거나 합쳐서 수행
- 즉 모든 단일 객체나 복합 객체가 1가지 추상화된 공통 인터페이스를 상속 받아 구현하여
단일 객체나 복합 객체가 동등한 (파일, 또는 디렉터리) 권한을 가지게 하는 방법이다.
- 최종적으로 파일을 실행했을땐 모든 객체를 담은 root 객체를 기준으로 트리구조로 실행된다.
자바 코드 예시
|
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
// Component
public interface FileSystemComponent {
void showDetails();
}
// leaf
public class File implements FileSystemComponent {
private String name;
public File(String name){
this.name = name;
}
@Override
public void showDetails() {
System.out.println("File: " + name);
}
}
//Composite
import java.util.ArrayList;
import java.util.List;
public class Directory implements FileSystemComponent {
private String name;
private List<FileSystemComponent> children = new ArrayList<>();
public Directory(String name){
this.name = name;
}
public void add(FileSystemComponent component){
children.add(component);
}
public void remove(FileSystemComponent component){
children.remove(component);
}
@Override
public void showDetails() {
System.out.println("Directory: " + name);
for (FileSystemComponent component : children){
component.showDetails();
}
}
}
// 실행
public class Main {
public static void main(String[] args){
FileSystemComponent file1 = new File("hello.txt");
FileSystemComponent file2 = new File("data.csv");
Direcvory folder1 = new Directory("Documents");
folder1.add(file1);
folder1.add(file2);
Directory root = new Directory("Root");
root.add(folder1);
root.add(new File("readme.md"));
root.showDetails();
}
}
|
cs |
스프링 부트 코드 예시
|
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
@Entity
public class Menu {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// 자식 메뉴
@ManyToOne(fetch = fetchType.LAZY)
@JoinColumn(name = "parent_id")
private Menu parent;
// 부모 메뉴
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
private List<menu> children = new ArrayList<>();
public void addChild(Menu child){
children.add(child);
child.setParent(this);
}
public void removeChild(Menu child){
children.remove(child);
child.setParent(null);
}
public void print(String prefix) {
System.out.println(prefix + "- " + name);
for(Menu child : children) {
child.print(prefix + " ");
}
}
// getter
// setter
}
// 서비스
@Service
public class MenuService {
@Transactional
public void createMenuTree() {
Menu root = new Menu();
root.setName("홈");
Menu product = new Menu();
product.setName("상품 관리");
Menu order = new Menu();
order.setName("주문 관리");
root.addChild(product);
root.addChild(order);
Menu productList = new Menu();
productList.setName("상품 목록");
Menu productAdd = new Menu();
productAdd.setName("상품 등록");
product.addChild(productList);
product.addChild(productAdd);
root.print(""); // 트리 형태로 출력
}
}
출력
- 홈
- 상품 관리
- 상품 목록
- 상품 등록
- 주문 관리
|
cs |
728x90
'디자인 패턴' 카테고리의 다른 글
| 디자인 패턴 / 프록시 패턴(Proxy) 정리 (0) | 2025.11.02 |
|---|---|
| 상태 패턴(State Pattern) 정리 (0) | 2025.10.26 |
| 디자인 패턴 / 반복자 패턴 정리(코드 위주) (0) | 2025.10.16 |
| 디자인 패턴 / 템플릿 메서드 패턴 (0) | 2025.10.14 |
| 디자인 패턴 / 퍼사드(Facade) 패턴 (0) | 2025.10.12 |
Comments