composite pattern

Pattern 2008. 4. 15. 21:02

Composite 패턴은 부분에서 전체에 이르는 계층 구조를 구축하거나 Tree형 자료 명세 내역을 생성하기 위해 사용될 수 있다. 

예를 들어 폴더와 파일을 합해서 [폴더 엔트리]로 취급하듯이 그릇과 내용물을 같은 종류로 취급하는 경우다. 이와 같이 특정 [객체들](파일)[그것들을 포함하는 객체들](폴더)을 동일하게 다룰 수 있게 해주는 패턴 Composite 패턴이라고 한다.

 

 File는 아무것도 포함할 수 없고 포함되는 역할을 하며, Folder는 File이나, 다른 Folder를 포함 할 수 있다. File의 operation()과 Folder의 operation()은 각각 다르게 동작한다.
(만약 HTML Parser를 구현한다면 Folder=Element, File=Attribute가 될 것이다.)
사용예) HTML DOM, XML

사용자 삽입 이미지


 

public abstract class AComponent {

             private String name;

             public String getName() {…}

             public void setName(String name) {…}

 

             protected abstract void operation(int depth);

             protected abstract void add(AComponent aComponent);

             protected abstract void remove(AComponent aComponent);

---------------------------------------------------------------------------

public class File extends AComponent {

             public File(String name) {

                           super.setName(name);

             }

             protected void operation(int depth) {

                           for (int i = 0; i < depth; i++)

                                        System.out.print("-");

                           System.out.println(" " + super.getName());

             }

             protected void add(AComponent component) {}

             protected void remove(AComponent component) {}
---------------------------------------------------------------------------

public class Folder extends AComponent {

             private List<AComponent> componentList = null;

             public Folder(String name) {

                           super.setName(name);

                           componentList = new ArrayList<AComponent>();

             }

             protected void operation(int depth) {
                           // 폴더의 이름을 출력함

                           for (int i = 0; i < depth; i++)                                    

                                        System.out.print("-");

                           System.out.println("+" + super.getName());

                           // 해당 폴더안의 자식들을 출력함
                          
for (AComponent c : this.componentList)

                                        c.operation(depth + 2);

             }

             protected void add(AComponent component) {

                           componentList.add(component);

             }

             protected void remove(AComponent component) {

                           componentList.remove(component);

             }

---------------------------------------------------------------------------

public class Client {

             public static void main(String[] args) {

                           Folder root = new Folder("ROOT");

                           root.add(new File("a"));

                           root.add(new File("b"));

                          

                           Folder sub1 = new Folder("SUB1");

                           sub1.add(new File("c"));

                           sub1.add(new File("d"));

                           root.add(sub1);

                          

                           root.add(new File("e"));

                          

                           root.operation(1);

             }

---------------------------------------------------------------------------

[실행결과]

-+ROOT

--- a

--- b

---+SUB1

----- c

----- d
--- e

Posted by 파이팅야
,