SMALL

복합체 패턴은 객체 간의 계층적 구조화를 통해 객체를 확장하는 패턴이다.

복합체는 재귀적으로 결합된 계층화된 트리 구조의 객체이다.

 

복합 객체는 객체가 또 다른 객체를 포함하는 것을 말한다.

 

 

복합체 패턴은 크게 4개의 구성 요소로 이루어진다.

  • Component
  • Composite
  • Leaf
  • Client

복합체는 일반 객체, 복합 객체 구분 없이 재귀적 결합이 가능하다.

모두 동일한 객체로 처리하여 트리 구조를 쉽게 활용한다.

 

복합체의 구성 요소인 Composite, leaf는 엄밀히 다른 객체이다.

하지만 복합체는 2개의 객체를 모두 관리하기 위해 동일한 Component 인터페이스를 적용하여, 인터페이스에는 두 객체의 공통된 기능이 모두 포함된다.

복합체 패턴은 Component 인터페이스를 이용하여 Composite 객체와 Leaf 객체를 서로 구별하지 않고 동일한 동작으로 처리한다. 이를 투명성이라고 한다.

투명성은 복합체 패턴의 특징이다. 

 

다만, Component 인터페이스는 투명성을 보장하기 위해서 단일 책임 원칙을 위반한다.

( 두 가지 이상의 기능을 탑재하고 있음)

 

Component

public abstract class Component {
    
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


}

 

Leaf

계층적 트리 구조의 제일 마지막 객체 

public class Leaf extends Component {

    Leaf(String name) {
        this.setName(name);
    }

    private int price;

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
}

 

Composite

public class Composite extends Component {

    Composite(String name) {
        this.setName(name);
    }

    public List<Component> children = new ArrayList<>();

    public void addNode(Component component) {
        this.children.add(component);
    }

    public void removeNode(Component component) {
        this.children.remove(component);
    }

    public boolean isNode() {
        if (this.children.size() > 0) {
            return true;
        } else {
            return false;
        }
    }
}

 

Client

    public static void main(String[] args) {
        Composite root = new Composite("root");

        Composite home = new Composite("home");
        Composite users = new Composite("users");
        Composite temp = new Composite("temp");

        Component img1 = new Leaf("img1");
        Component img2 = new Leaf("img2");
        // Component img3 = new Leaf("img3");
        // Component img4 = new Leaf("img4");

        root.addNode(home);
        root.addNode(users);
        users.addNode(temp);
        temp.addNode(img1);
        temp.addNode(img2);

        Main mm = new Main();
        mm.tree(root, "");

    }
    public void tree(Composite composite, String depth) {

        List<Component> list = composite.children;

        for (Component com : list) {

            if (com instanceof Composite) {
                System.out.println("Folder:" + depth + com.getName());
                Composite temp = (Composite) com;
                if (temp.isNode()) {
                    tree(temp, depth + "  ");
                } else {
                    System.out.println(depth + "No Children");
                }

            } else if (com instanceof Leaf) {
                System.out.println("Leaf:" + depth + com.getName());
            } else {
                System.out.println("??");
            }
        }
    }

 

 

LIST

+ Recent posts