设计模式——组成模式

在这里插入图片描述

package composite;

public abstract class Component {
	private String name;
	public Component(String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void add(Component c) {};
	public void remove(Component c) {};
	//方法show,做成抽象的,每个子类都要有
	protected abstract void show(int depth);
}
package composite;

import java.util.ArrayList;
import java.util.List;

public class Composite extends Component {
	//一个子对象用来存储其下属的叶子节点和枝节点
	private List<Component> children=new ArrayList<Component>();
	
	public Composite(String name) {
		super(name);
	}
	
	public Composite(String name, List<Component> children) {
		super(name);
		this.children = children;
	}
	//重写add方法
	@Override
	public void add(Component c) {
		children.add(c);
	}
	//重写remove方法
	@Override
	public void remove(Component c) {
		children.remove(c);
	}
	@Override
	protected void show(int depth) {
		System.out.println(new String("-"+depth)+getName());
		//遍历children
		for (Component component : children) {
			component.show(depth+1);
		}
	}

}

package composite;

public class Leaf extends Component {

	public Leaf(String name) {
		super(name);
	}
/*//叶子节点中没有增加和删除方法
	@Override
	public void add(Component c) {
		System.out.println("不能添加叶子!");
	}

	@Override
	public void remove(Component c) {
		System.out.println("不能删除叶子");
	}
*/
	@Override
	protected void show(int depth) {
		System.out.println(new String("-"+depth)+getName());
	}

}

package composite;

public class Client {

	public static void main(String[] args) {
		//创建根对象,并从根上生成两片树叶Leaf a,Leaf b
		Composite root=new Composite("root");
		root.add(new Leaf("Leaf a"));
		root.add(new Leaf("Leaf b"));

		//根上生成分支childX,并从分支上生成两片树叶Leaf xa,Leaf xb
		Composite childX=new Composite("childX");
		root.add(childX);
		childX.add(new Leaf("Leaf xa"));
		childX.add(new Leaf("Leaf xb"));
		

		//从分支上再长出分支childXY,并从分支上生成两片树叶Leaf xya,Leaf xyb
		Composite childXY=new Composite("childXY");
		childX.add(childXY);
		childX.add(new Leaf("Leaf xya"));
		childX.add(new Leaf("Leaf xyb"));
		
		//根部又长出来一片树叶,但是没长牢,被风吹走了
		Leaf leaf=new Leaf("leaf c");
		root.add(leaf);
		root.remove(leaf);
		
		root.show(1);
	}

}

组成模式实现商品类别树:

package composite_clothes;

public abstract class Cloth {

	private String name;
	
	public Cloth(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public void add(Cloth c){
		
	}
	public void remove(Cloth c){
		
	}
	protected abstract void show(String preStr);
}

package composite_clothes;

import java.util.ArrayList;
import java.util.List;

public class Composite extends Cloth {

	List<Cloth> cloth=new ArrayList<Cloth>();
	public Composite(String name) {
		super(name);
	}
	@Override
	public void add(Cloth c){
		cloth.add(c);
	}
	public void remove(Cloth c){
		cloth.remove(c);
	}
	
	@Override
	protected void show(String preStr) {
		System.out.println(new String("+"+getName()));
		for (Cloth cloth2 : cloth) {
			cloth2.show(preStr);
		}
	}

}

package composite_clothes;

public class Leaf extends Cloth {

	public Leaf(String name) {
		super(name);
	}

	@Override
	protected void show(String preStr) {
		System.out.println(preStr+"  -"+getName());
	}

}

package composite_clothes;

public class Client {

	public static void main(String[] args) {
		Cloth all=new Composite("服装");
		Cloth c1=new Composite("男装");
		Cloth c2=new Composite("女装");
		
		Cloth leaf1=new Leaf("衬衣");
		Cloth leaf2=new Leaf("夹克");
		Cloth leaf3=new Leaf("裙子");
		Cloth leaf4=new Leaf("套装");
		
		all.add(c1);
		all.add(c2);
		c1.add(leaf1);
		c1.add(leaf2);
		c2.add(leaf3);
		c2.add(leaf4);
		
		all.show(" ");
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值