设计模式----组合模式(composite)

该设计模式是以公司的组织结构为背景的

Crop.java代码如下:

package com.designPattern.composite;

public abstract class Corp {

	private String name ="";
	
	private String position = "";
	
	private int salary = 0;
	
	public Corp(String name,String position,int salary){
		this.name = name;
		this.position = position;
		this.salary = salary;
	}
	
	public String getInfo(){
		String info = "姓名:"+this.name+"\t职位:"+this.position+"\t薪水:"+this.salary;
		return info;
	}
}



Leaf.java代码如下:

package com.designPattern.composite;

public class Leaf extends Corp{

	public Leaf(String name, String position, int salary) {
		super(name, position, salary);
	}

}



Branch.java代码如下:
 package com.designPattern.composite;

import java.util.ArrayList;

public class Branch extends Corp{
	ArrayList<Corp> subordinateList = new ArrayList<Corp>();

	public Branch(String name, String position, int salary) {
		super(name, position, salary);
	}
	
	public void addSubordinate(Corp corp){
		this.subordinateList.add(corp);
	}
	
	public ArrayList<Corp> getSubordinate(){
		return this.subordinateList;
	}

}



Client.java代码如下:
 package com.designPattern.composite;

import java.util.ArrayList;

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 root = new Branch("老大","总经理",100000);
		
		Branch developDep = new Branch("老刘","研发部经理",10000);
		Branch salesDep = new Branch("老马","销售部经理",20000);
		Branch financeDep = new Branch("老赵","财务部经理",30000);
		
		Branch firstDevGroup = new Branch("杨组长","研发一组组长",8000);
		Branch secondDevGroup = new Branch("吴组长","研发二组组长",8000);
		
		Leaf a = new Leaf("a","开发人员",2000);
		Leaf b = new Leaf("b","开发人员",2000);
		Leaf c = new Leaf("c","开发人员",2000);
		Leaf d = new Leaf("d","开发人员",2000);
		Leaf e = new Leaf("e","开发人员",2000);
		Leaf f = new Leaf("f","开发人员",2000);
		Leaf g = new Leaf("g","开发人员",2000);
		Leaf h = new Leaf("h","开发人员",2000);
		Leaf i = new Leaf("i","开发人员",2000);
		Leaf k = new Leaf("k","销售人员",2000);
		Leaf l = new Leaf("l","销售人员",2000);
		Leaf j = new Leaf("j","财务人员",2000);
		Leaf m = new Leaf("m","财务人员",2000);
		Leaf n = new Leaf("n","CEO秘书",2000);
		Leaf p = new Leaf("陆副经理","研发部副经理",2000);
		
		firstDevGroup.addSubordinate(a);
		firstDevGroup.addSubordinate(b);
		firstDevGroup.addSubordinate(c);
		firstDevGroup.addSubordinate(d);
		
		secondDevGroup.addSubordinate(e);
		secondDevGroup.addSubordinate(f);
		secondDevGroup.addSubordinate(g);
		secondDevGroup.addSubordinate(h);
		secondDevGroup.addSubordinate(i);
		
		salesDep.addSubordinate(k);
		salesDep.addSubordinate(l);
		
		financeDep.addSubordinate(j);
		financeDep.addSubordinate(m);
	
		developDep.addSubordinate(firstDevGroup);
		developDep.addSubordinate(secondDevGroup);
		developDep.addSubordinate(p);
		
		root.addSubordinate(n);
		root.addSubordinate(salesDep);
		root.addSubordinate(financeDep);
		root.addSubordinate(developDep);
		
		return root;
		
	}
	
	public static String getTreeInfo(Branch root){
		ArrayList<Corp> subordinateList = root.getSubordinate();
		String info = "";
		
		for(Corp c:subordinateList){
			if(c instanceof Leaf){
				info = info +c.getInfo()+"\n";
			}else{
				info = info +c.getInfo()+"\n"+getTreeInfo((Branch)c);
			}
		}
		
		return info;
	}

}



运行结果如下:
 姓名:老大	职位:总经理	薪水:100000
姓名:n	职位:CEO秘书	薪水:2000
姓名:老马	职位:销售部经理	薪水:20000
姓名:k	职位:销售人员	薪水:2000
姓名:l	职位:销售人员	薪水:2000
姓名:老赵	职位:财务部经理	薪水:30000
姓名:j	职位:财务人员	薪水:2000
姓名:m	职位:财务人员	薪水:2000
姓名:老刘	职位:研发部经理	薪水:10000
姓名:杨组长	职位:研发一组组长	薪水:8000
姓名:a	职位:开发人员	薪水:2000
姓名:b	职位:开发人员	薪水:2000
姓名:c	职位:开发人员	薪水:2000
姓名:d	职位:开发人员	薪水:2000
姓名:吴组长	职位:研发二组组长	薪水:8000
姓名:e	职位:开发人员	薪水:2000
姓名:f	职位:开发人员	薪水:2000
姓名:g	职位:开发人员	薪水:2000
姓名:h	职位:开发人员	薪水:2000
姓名:i	职位:开发人员	薪水:2000
姓名:陆副经理	职位:研发部副经理	薪水:2000

你可能感兴趣的:(设计模式)