设计模式之组合模式14

来源:互联网 发布:软件架构.pdf 编辑:程序博客网 时间:2024/05/22 10:38

设计模式之组合模式14

背景

迭代器模式pass,因为迭代器模式是在jdk1.2出现Iterator时的模式。对于现在用List,Map遍历即可搞定。

一个组织架构图,上至CEO,下至普通开发人员,有这样的一个树结构,我们用组合模式来实现

类图

设计模式之组合模式

代码实现

将对象抽离为以下几种

  • 员工信息抽象类,大家都有的信息,
  • 叶子节点,普通开发,
  • 非叶子节点,组长,管理层
  • 客户端调用

抽象信息类

public abstract class Crop {    private String name = "";    private String position = "";    private Double salary = 0.0;    private Crop parent = null;    public Crop(String name, String position, Double salary) {        this.name = name;        this.position = position;        this.salary = salary;    }    public String getInfo() {        return "Crop [name=" + name + ", position=" + position + ", salary="                + salary + "]";    }    public void setParent(Crop crop) {        this.parent = crop;    }    public Crop getParent() {        return this.parent;    }}

普通开发

public class Leaf extends Crop{    public Leaf(String name, String position, Double salary) {        super(name, position, salary);    }}

管理层

public class Branch extends Crop{    private List<Crop> subOrdinateList = new ArrayList<Crop>();    public Branch(String name, String position, Double salary) {        super(name, position, salary);    }    public void addSubOrdinate(Crop crop) {        crop.setParent(this);        this.subOrdinateList.add(crop);    }    public List<Crop> getSubOrdinate() {        return this.subOrdinateList;    }}

客户端调用

public class Client {    public static void main(String[] args) {        Branch ceo = compositeTree();        System.out.println(ceo.getInfo());        System.out.println(getTreeInfo(ceo));    }    public static Branch compositeTree() {        Branch ceo = new Branch("张三", "CEO", 100000.0);        Branch leader1 = new Branch("部门经理A", "部门经理", 61000.0);        Branch leader2 = new Branch("部门经理B", "部门经理", 62000.0);        Branch leader3 = new Branch("部门经理C", "部门经理", 63000.0);        Leaf leaf1 = new Leaf("普通开发A", "普通开发", 11000.0);        Leaf leaf2 = new Leaf("普通开发B", "普通开发", 12000.0);        Leaf leaf3 = new Leaf("普通开发C", "普通开发", 13000.0);        ceo.addSubOrdinate(leader1);        ceo.addSubOrdinate(leader2);        ceo.addSubOrdinate(leader3);        leader1.addSubOrdinate(leaf1);        leader1.addSubOrdinate(leaf2);        leader2.addSubOrdinate(leaf3);        return ceo;    }    public static String getTreeInfo(Branch branch) {        List<Crop> subordinateList = branch.getSubOrdinate();        String info = "";        for(Crop crop : subordinateList) {            if(crop instanceof Leaf) {                info += crop.getInfo() + "\n";            }else {                info += crop.getInfo() + "\n" + getTreeInfo((Branch)crop);            }        }        return info;    }}

运行结果

Crop [name=张三, position=CEO, salary=100000.0]Crop [name=部门经理A, position=部门经理, salary=61000.0]Crop [name=普通开发A, position=普通开发, salary=11000.0]Crop [name=普通开发B, position=普通开发, salary=12000.0]Crop [name=部门经理B, position=部门经理, salary=62000.0]Crop [name=普通开发C, position=普通开发, salary=13000.0]Crop [name=部门经理C, position=部门经理, salary=63000.0]
1 0
原创粉丝点击