// ❌ 나쁜 예: 계층 구조를 다룰 때 타입별로 다른 처리
public void printMenu() {
// MenuItem과 Menu를 구별해서 처리해야 함
if (current instanceof MenuItem) {
MenuItem item = (MenuItem) current;
System.out.println(item.getName());
}
else if (current instanceof Menu) {
Menu menu = (Menu) current;
// Menu 안의 모든 항목 처리...
}
}
문제점:
public abstract class MenuComponent {
// 공통 메서드
public String getName() {
throw new UnsupportedOperationException();
}
public void print() {
throw new UnsupportedOperationException();
}
// 복합 객체 전용 메서드
public void add(MenuComponent menuComponent) {
throw new UnsupportedOperationException();
}
public void remove(MenuComponent menuComponent) {
throw new UnsupportedOperationException();
}
public MenuComponent getChild(int i) {
throw new UnsupportedOperationException();
}
}
왜 Interface가 아닌 Abstract Class인가?
public class MenuItem extends MenuComponent {
String name;
double price;
boolean vegetarian;
public MenuItem(String name, double price, boolean vegetarian) {
this.name = name;
this.price = price;
this.vegetarian = vegetarian;
}
public String getName() {
return name;
}
public void print() {
System.out.println(" " + getName() + ", " + getPrice());
}
}
public class Menu extends MenuComponent {
ArrayList<MenuComponent> menuComponents = new ArrayList<>();
String name;
public Menu(String name) {
this.name = name;
}
public void add(MenuComponent menuComponent) {
menuComponents.add(menuComponent);
}
public void remove(MenuComponent menuComponent) {
menuComponents.remove(menuComponent);
}
public MenuComponent getChild(int i) {
return menuComponents.get(i);
}
public void print() {
System.out.println("\n" + getName());
System.out.println("---------------------");
// 재귀적으로 모든 자식 출력
for (MenuComponent menuComponent : menuComponents) {
menuComponent.print();
}
}
}
단일 책임 원칙
투명성 (Transparency)
재귀적 구조

Component
(추상 클래스)
|
________|________
| |
Leaf Composite
(개별 객체) (복합 객체)
|
children[]
(Component들)
핵심 특징:
public abstract class MenuComponent {
// 모든 메서드는 기본적으로 예외를 던짐
// 필요한 클래스에서만 오버라이드
public void add(MenuComponent menuComponent) {
throw new UnsupportedOperationException();
}
public void remove(MenuComponent menuComponent) {
throw new UnsupportedOperationException();
}
public MenuComponent getChild(int i) {
throw new UnsupportedOperationException();
}
public String getName() {
throw new UnsupportedOperationException();
}
public double getPrice() {
throw new UnsupportedOperationException();
}
public boolean isVegetarian() {
throw new UnsupportedOperationException();
}
public void print() {
throw new UnsupportedOperationException();
}
}
public class MenuItem extends MenuComponent {
String name;
String description;
boolean vegetarian;
double price;
public MenuItem(String name, String description,
boolean vegetarian, double price) {
this.name = name;
this.description = description;
this.vegetarian = vegetarian;
this.price = price;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public double getPrice() {
return price;
}
public boolean isVegetarian() {
return vegetarian;
}
public void print() {
System.out.print(" " + getName());
if (isVegetarian()) {
System.out.print("(v)");
}
System.out.println(", " + getPrice());
System.out.println(" -- " + getDescription());
}
}
public class Menu extends MenuComponent {
ArrayList<MenuComponent> menuComponents = new ArrayList<>();
String name;
String description;
public Menu(String name, String description) {
this.name = name;
this.description = description;
}
public void add(MenuComponent menuComponent) {
menuComponents.add(menuComponent);
}
public void remove(MenuComponent menuComponent) {
menuComponents.remove(menuComponent);
}
public MenuComponent getChild(int i) {
return menuComponents.get(i);
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public void print() {
System.out.print("\n" + getName());
System.out.println(", " + getDescription());
System.out.println("---------------------");
// 재귀적으로 모든 자식 출력 (핵심!)
for (MenuComponent menuComponent : menuComponents) {
menuComponent.print(); // Leaf든 Composite든 상관없이 호출
}
}
}
public class MenuTestDrive {
public static void main(String[] args) {
// 최상위 메뉴 생성
MenuComponent allMenus = new Menu("전체 메뉴", "모든 메뉴 통합");
// 중간 레벨 메뉴들 생성
MenuComponent pancakeMenu = new Menu("팬케이크 하우스 메뉴", "아침식사");
MenuComponent dinerMenu = new Menu("다이너 메뉴", "점심식사");
MenuComponent cafeMenu = new Menu("카페 메뉴", "저녁식사");
// 최상위 메뉴에 추가
allMenus.add(pancakeMenu);
allMenus.add(dinerMenu);
allMenus.add(cafeMenu);
// 팬케이크 메뉴에 항목 추가
pancakeMenu.add(new MenuItem(
"K&B 팬케이크 세트",
"스크램블 에그와 토스트 포함",
true,
2.99
));
pancakeMenu.add(new MenuItem(
"레귤러 팬케이크 세트",
"계란 후라이와 소시지 포함",
false,
2.99
));
// 다이너 메뉴에 디저트 서브메뉴 추가
MenuComponent dessertMenu = new Menu("디저트 메뉴", "디저트를 즐기세요!");
dessertMenu.add(new MenuItem(
"애플 파이",
"바닐라 아이스크림을 곁들인 애플 파이",
true,
1.59
));
dinerMenu.add(dessertMenu); // 메뉴 안에 메뉴!
// 웨이트리스에게 전체 메뉴 전달
Waitress waitress = new Waitress(allMenus);
// 전체 메뉴 출력
waitress.printMenu();
}
}
public class Waitress {
MenuComponent allMenus;
public Waitress(MenuComponent allMenus) {
this.allMenus = allMenus;
}
public void printMenu() {
allMenus.print(); // 단순! Composite 패턴의 핵심
}
}
전체 메뉴, 모든 메뉴 통합
---------------------
팬케이크 하우스 메뉴, 아침식사
---------------------
K&B 팬케이크 세트(v), 2.99
-- 스크램블 에그와 토스트 포함
레귤러 팬케이크 세트, 2.99
-- 계란 후라이와 소시지 포함
다이너 메뉴, 점심식사
---------------------
디저트 메뉴, 디저트를 즐기세요!
---------------------
애플 파이(v), 1.59
-- 바닐라 아이스크림을 곁들인 애플 파이
카페 메뉴, 저녁식사
---------------------
Composite 구조를 순회하려면 CompositeIterator가 필요합니다.
public class CompositeIterator implements Iterator {
Stack<Iterator> stack = new Stack<>();
public CompositeIterator(Iterator iterator) {
stack.push(iterator);
}
public Object next() {
if (hasNext()) {
Iterator iterator = stack.peek();
MenuComponent component = (MenuComponent) iterator.next();
// Menu면 그 자식들도 순회해야 함
if (component instanceof Menu) {
stack.push(component.createIterator());
}
return component;
}
return null;
}
public boolean hasNext() {
if (stack.empty()) {
return false;
}
Iterator iterator = stack.peek();
if (!iterator.hasNext()) {
stack.pop(); // 현재 레벨 완료
return hasNext(); // 재귀적으로 상위 레벨 확인
}
return true;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
// Menu 클래스에 추가
public Iterator createIterator() {
return new CompositeIterator(menuComponents.iterator());
}
// MenuItem 클래스에 추가 (NullIterator 사용)
public Iterator createIterator() {
return new NullIterator(); // Leaf는 자식이 없음
}
public class Waitress {
MenuComponent allMenus;
public Waitress(MenuComponent allMenus) {
this.allMenus = allMenus;
}
public void printVegetarianMenu() {
Iterator iterator = allMenus.createIterator();
System.out.println("\n채식 메뉴");
System.out.println("-----------");
while (iterator.hasNext()) {
MenuComponent menuComponent = (MenuComponent) iterator.next();
try {
if (menuComponent.isVegetarian()) {
menuComponent.print();
}
} catch (UnsupportedOperationException e) {
// Menu 객체는 isVegetarian() 없음 - 무시
}
}
}
}
이 예시는 투명성을 선택 → 클라이언트 코드가 단순해짐
| 요소 | 역할 | 특징 |
|---|---|---|
| Component | 공통 인터페이스 | Leaf와 Composite의 공통 타입 |
| Leaf | 개별 객체 | 자식을 가질 수 없음 |
| Composite | 복합 객체 | Component들의 컨테이너 |
| Client | 사용자 | Component 타입으로 모든 객체 처리 |