욱'S 노트

Composite - 계층구조 만들기 본문

Methdology/Design Pattern

Composite - 계층구조 만들기

devsun 2012. 4. 16. 18:05
When Using It

재귀적인 계층구조를 만들고 싶을때

Class Diagram
 





Sample Code



public abstract class Component {

public abstract void operation();

public abstract void add(Component component);

public abstract void remove(Component component);

}


public class Leaf extends Component {

@Override

public void operation() {

System.out.println(this.toString());

}


@Override

public void add(Component component) {

throw new RuntimeException("This is Leaf");

}


@Override

public void remove(Component component) {

throw new RuntimeException("This is Leaf");

}

}


public class Composite extends Component {

private List<Component> list = new ArrayList<Component>();

@Override

public void operation() {

for ( Component component : list ) {

System.out.println(component.toString());

}

}


@Override

public void add(Component component) {

list.add(component);

}


@Override

public void remove(Component component) {

list.remove(component);

}

}


Caution

특별한 주의사항은 없다.
Comments