组合模式
组合模式:将对象组合成树形结构以表示"部分-整体"的层次结构。"Composite使得用户对单个对象和组合对象的使用具有一致性。"
public abstract class AComponent {
//组合类
public class Composite extends AComponent{
private ArrayList children = new ArrayList();
public Composite(String name) {
super(name); }
public void Add(AComponent c) {
this.component = c; children.add(c); }
public void Remove(AComponent c) {
children.remove(c); }
public void Display(int i) {
Iterator iter = children.iterator();
AComponent c = null;
c = (AComponent) iter.next();
while(iter.hasNext()&&iter.next().equals(null))
{ System.out.println("abc------"+c.name); } } }
//单对象类
public class Leaf extends AComponent{
public Leaf(String name) {
super(name); }
public void Add(AComponent c) {
System.out.println("不能添加子项!"); }
public void Remove(AComponent c) {
System.out.println("不能移除子项!"); }
public void Display(int i) {
System.out.println("abc"+new String()+name); } }
//测试类
public class Test {