组合模式一步步实现

//未用模式
/*class Leaf{
	private String name;
	public Leaf(String name) {
		super();
		this.name = name;
	}
	public void printStruct(int d) 
		for(int i=0;i<d;i++) {
			System.out.print(" ");
			System.out.println("-"+name);
	}
}
class Composite{
	private Collection<Composite> childComposite = new ArrayList<Composite>();
	private Collection<Leaf> childLeaf = new ArrayList<Leaf>();
	private String name;
	public Composite(String name) {
		super();
		this.name = name;
	}
	public void AddComposite(Composite c) {
		this.childComposite.add(c);
	}
	public void AddLeaf(Leaf leaf) {
		this.childLeaf.add(leaf);
	}
	public void printStruct(int d) {
		for(int i=0;i<d;i++) 
			System.out.print(" ");
			System.out.println("+"+name);
			for(Leaf leaf : childLeaf) {
				leaf.printStruct(d+2);
			}
			for(Composite c : childComposite) {
				c.printStruct(d+2);
			}
	}
}
public class Main {
	  public static void main(String args[])
	  {
		  Composite root = new Composite("服装");
		  Composite c1 = new Composite("男装");
		  Composite c2 = new Composite("女装");
		  Leaf l1 = new Leaf("衬衣");
		  Leaf l2 = new Leaf("夹克");
		  Leaf l3 = new Leaf("裙子");
		  Leaf l4 = new Leaf("套装");
		  root.AddComposite(c1);
		  root.AddComposite(c2);
		  c1.AddLeaf(l1);
		  c1.AddLeaf(l2);
		  c2.AddLeaf(l3);
		  c2.AddLeaf(l4);
		  root.printStruct(0);
	}
}*/
//组合模式
abstract class Component{
	protected String name;
	public Component(String name) {
		super();
		this.name = name;
	}
	public abstract void Add(Component c);
	public abstract void Remove(Component c);
	public abstract void Display(int depth);
}
class Leaf extends Component{
	public Leaf(String name) {
		super(name);
	}
	public void Add(Component c) {
		// TODO Auto-generated method stub
		System.out.println("Cannot add a leaf");
	}
	public void Remove(Component c) {
		// TODO Auto-generated method stub
		System.out.println("Cannot remove from a leaf");
	}
	public void Display(int depth) {
		// TODO Auto-generated method stub
		for(int i=0;i<depth;i++) 
			System.out.print(" ");
			System.out.println("-"+name);
	}
}
class Composite extends Component{
	private ArrayList<Component> component = new ArrayList<Component>();
	public Composite(String name) {
		super(name);
	}
	public void Add(Component c) {
		// TODO Auto-generated method stub
		component.add(c);
	}
	public void Remove(Component c) {
		// TODO Auto-generated method stub
		component.remove(c);
	}
	public void Display(int depth) {
		// TODO Auto-generated method stub
		for(int i=0;i<depth;i++)
			System.out.print(" ");
		System.out.println("+"+name);
		for(Component c : component) {
			c.Display(depth + 2);
		}
	}
}
public class Main {
	  public static void main(String args[])
	  {
		  Composite root = new Composite("服装");
		  Composite c1 = new Composite("男装");
		  Composite c2 = new Composite("女装");
		  Leaf l1 = new Leaf("衬衣");
		  Leaf l2 = new Leaf("夹克");
		  Leaf l3 = new Leaf("裙子");
		  Leaf l4 = new Leaf("套装");
		  root.Add(c1); root.Add(c2);
		  c1.Add(l1); c1.Add(l2);
		  c2.Add(l3); c2.Add(l4);
		  root.Display(0);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值