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(" ");
}
}