Skip to content

Conversation

@guswns3371
Copy link
Member

No description provided.

@guswns3371
Copy link
Member Author

컴포지트 패턴

객체를 트리구조로 구성해서 부분-전체 계층구조를 구현한다. 컴포지트 패턴을 사용하면 클라이언트에서 개별 객체와 복합 객체를 똑같은 방법으로 다룰 수 있다.

image

메뉴와 항목을 같은 구조에 넣어서 부분-전체계층 구조(part-whole hierarchy)를 생성한다

  • 부분-전체 계층구조 : 부분(메뉴 및 메뉴 항목)들이 계층을 이루고 있지만 모든 부분을 묶어서 전체로 다룰 수 있는 구조

image

이러한 복합 구조를 사용하면 복합객체와 개별객체를 대상으로 동일한 작업을 수행할 수 있다. -> 복합/개별 객체를 구분할 필요가 없어진다.

Comment on lines +3 to +35
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 String getDescription() {
throw new UnsupportedOperationException();
}

public double getPrice() {
throw new UnsupportedOperationException();
}

public boolean isVegetarian() {
throw new UnsupportedOperationException();
}

public void print() {
throw new UnsupportedOperationException();
}
}
Copy link
Member Author

@guswns3371 guswns3371 Jan 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

한 클래스(MenuComponent )에 2가지 역할을 넣고 있다.

컴포지트 패턴은 어떤 원소가 복합객체이고 단일 객체인지 클라이언트에게 투명하게 보이게 하기 위해 SRP 디자인 원칙을 위배하였다.

만약 여러 역할을 서로 다른 인터페이스로 분리한다면, 복합/단일 객체가 각각 부적절한 메소드를 호출하는 상황이 일어나지 않게 된다. 하지만 이는 투명성을 떨어뜨리고, 복합/단일 객체를 분리해야하기 때문에 코드에서 분기처리문이 늘어나게 된다 (instanceof)

따라서 상황에 따라 투명성과 안정성 사이에서 적절한 균형을 찾아야 한다.

@guswns3371 guswns3371 merged commit d798025 into main Jan 22, 2023
@guswns3371 guswns3371 deleted the feature/composite branch January 22, 2023 07:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant