티스토리 뷰

Programing/DesignPattern

Composite 패턴

오통 2016. 8. 24. 15:45

Composite 패턴이란?


  • 단일객체와 복합객체를 동일시해서 재귀적인 구조를 만들기 위한 패턴.
  • 트리구조의 데이터를 구현할 때 유용


 


- 위 class diagram을 컴퓨터 파일 시스템에 빗대어 설명해 보자. 파일 시스템은 file과 directory로 이루어져 있고 tree 형태이다. tree 구조의 특성상 하나의 node는 자식을 가질 수 있고, 그 자식이 다시 자식을 가질 수 있다. 자식을 가지는 node가 directory(Composite)이며 자식은 directory 또는 file 이 될 수 있다. 자식을 가지지  않는 node는 file(leaf) 이다.


- File = Leaf(내용물) = 단일객체
- Directory = Composite(복합물) = 복합객체



예제




// 사용자가 file과 directory를 동일하게 사용하기 위한 추상 클래스
public abstract class Component {
  public abstract String getName();
  public abstract int getSize();
  public abstarct void add();
  public abstact void remove();
}

// 단일객체(Leaf) 인 File Class
public class File extends Component{
  private String name;
  private int size;

  public File(String name, String size) {
    this.name = name;
    this.size = size;
  }

  public String getName() {
    return name;
  }

  public int getSize() {
    return size;
  }
  // File은 add를 구현하지 않음. exception 발생하도록 구현해도 됨.
  public void add() {
  }
  // File은 remove를 구현하지 않음. exception 발생하도록 구현해도 됨.
  public void remove() {
  }
}

// 복합객체(
public class Directory extends Component {
  private String name;
  private ArrayList children;
  public Directory(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }

  // directory의 size를 얻기 위해 getSize() 의 재귀적호출 을 하고 있음.
  public int getSize() {
    int size = 0;
    Iterator it = children.iterator();
    while(it.hasNext()) {
      Component c = (Component)it.next();
      size += c.getSize();
    }
    return size;
  }

  public void add(Component c) {
    children.add(c);
  }

  public void remove(Component c) {
    children.remove(c);
  }
}

public class Main {
  public static void main(String[] args) {
    Component fileA = new File("fileA", 10);
    Component fileB = new File("fileB", 20);
    Compoenet fileC = new File("fileC", 30);

    Component dirA = new Directory("DirA");
    Component dirB = new Directory("DirB");

    dirA.add(fileA);
    dirA.add(fileB);

    dirB.add(fileC);

    dirA.add(dirB);

    System.out.println("fileA.getSize() = " + fileA.getSize());    // 10
    System.out.println("fileB.getSize() = " + fileB.getSize());    // 20
    System.out.println("fileC.getSize() = " + fileC.getSize());    // 30
    System.out.println("dirB.getSize() = " + dirB.getSize());      // 30
    System.out.println("dirA.getSize() = " + dirA.getSize());      // 60
  }
}



결론



Directory와 File을 Component(상위 class)에 의해 동일하게 사용할 수 있고, 복합객체인 Directory에서 재귀적인 호출이 가능하다. 주로 Tree 형태의 Data를 표현할 때 Composite pattern으로 용이하게 구현할 수 있다.


'Programing > DesignPattern' 카테고리의 다른 글

State 패턴  (0) 2016.09.01
Template Method 패턴  (0) 2016.08.30
Singleton 패턴  (0) 2016.08.24
Mediator 패턴  (0) 2016.08.22
Decorator 패턴  (0) 2016.08.17
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
TAG
more
«   2024/05   »
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
글 보관함