现有如下需求,某公司的机构:
+公司
+分公司
+本地分公司
-呼和浩特
+包头
-昆区办事处
-青山办事处
+外地分公司
+总部
-人事处
+办公室
+财务处
实现思路:
抽象组合类,叶子类,根类
叶子类与根类继承与抽象组合类,根类中创建一个抽象组合类类型的列表用于存放字节点(节点可以是叶子如本地分公司里放呼和浩特,也可以是根如分公司里放本地分公司,是根的话实现套娃)
创建抽象组合类的对象,按照需求结构存放(初始节点是根类的公司,字节点为根类的分公司和总部......)
代码
package zzz;
//组合模式实现公司树型结构
import java.util.ArrayList;
import java.util.List;
class company{
public static void main (String []args)
{
item one, two1, two2, three1, three2, three3, three4,three5,hhht,bt,kq,qs,dh; //声明空间类型
//第一层
one = new root("公司");
//第二层
two1 = new root("分公司");
one.Add(two1);//将分公司添加到公司字节点
two2 = new root("总部");
one.Add(two2);//将总部添加到公司字节点
//第三层
three1 = new root("本地分公司");
two1.Add(three1);
three2 = new root("外地分公司");
two1.Add(three2);
//*****分公司
three3 = new leaf("人事处");
two2.Add(three3);//人事处是叶子节点
three4 = new root("办公处");
two2.Add(three4);
three5 = new root("财务处");
two2.Add(three5);
//第四层
hhht = new leaf("呼和浩特");//叶子节点
three1.Add(hhht);
bt = new root("包头");
three1.Add(bt);
//第五层
kq = new leaf("昆曲办事处");
bt.Add(kq);
qs = new leaf("青山办事处");
bt.Add(qs);
dh = new leaf("东河办事处");
bt.Add(dh);
//one.lookchild();
//System.out.println("8*********");
//two1.lookchild();
//System.out.println("8*********");
//two2.lookchild();
//System.out.println("8*********");
// three1.lookchild();
// three2.lookchild();
// three3.lookchild();
//three4.lookchild();
// three5.lookchild();
//two1.click();
System.out.println( one.getname()+"");
one.click();
/*
for(int i=0;i<one.fileList.size();i++)
{
System.out.println(name);
}*/
}
}
//抽象构件类,被叶子检点和根检点继承。
//click方法叶子节点为显示名字,根节点为显示名字遍历子节点的click方法(如果字节点是根则又可以套娃)
abstract class item
{
protected abstract void lookchild();//遍历子节点
public abstract void Add(item file);
public abstract void Remove(item file);
public abstract void click();
public abstract String getname();//访问器
}
//叶子*******************************************************
class leaf extends item
{
public int pd=0;//默认为0遍历完为1
private String name;
public String getname()//访问器
{
return this.name;
}
public leaf(String name)
{
this.name = name;
}
//叶子检点没有添加删除
public void Add(item file)
{
System.out.println("不能给叶子添加字结点");
}
public void Remove(item file)
{
System.out.println("不能给叶子删除字结点");
}
//叶子click方法显示自己的名字
public void click()
{
// System.out.println(name);
}
@Override
protected void lookchild() {
// TODO Auto-generated method stub
}
}
//根**********************************************************
class root extends item
{
private List<item> fileList = new ArrayList<item>();
public int pd=0;//默认为0遍历完为1
private String name;
public String getname()//访问器
{
return this.name;
}
public root(String name)
{
this.name = name;
}
public void Add(item file)
{
fileList.add(file);
}
public void Remove(item file)
{
fileList.remove(file);
}
public void lookchild()//遍历子节点
{
for(int i=0;i<fileList.size();i++)
{
System.out.println(fileList.get(i).getname());
}
}
public void click()
{
for(int i=0;i<fileList.size();i++)
{
System.out.println(fileList.get(i).getname());
((item) fileList.get(i)).click();//便利字结点的click,字节点可以是根也可以是叶子
}
}
}
运行结果