大话设计模式-组合模式

需求
设计一个总公司和分公司的OA系统。
实现

public abstract class Company
{
	private String name;
	public Company(String name)
	{
		this.name=name;
	}
	public Company()
	{
		
	}
	public String getName() 
	{
		return name;
	}
	public void setName(String name) 
	{
		this.name=name;
	}
	protected abstract void add(Company company);
	protected abstract void remove(Company company);
	protected abstract void display(int depth);
}
import java.util.*;

class ConcreteCompany extends Company
{
	private List<Company> list;
	public ConcreteCompany()
	{
		list=new ArrayList<Company>();
	}
	public ConcreteCompany(String name)
	{
		super(name);
		list=new ArrayList<Company>(); 
	}
	@Override
	protected void add(Company company)
	{
		// TODO Auto-generated method stub
		list.add(company);
	}
	@Override
	protected void remove(Company company)
	{
		// TODO Auto-generated method stub
		list.remove(company);
	}
	@Override
	protected void display(int depth)
	{
		// TODO Auto-generated method stub
		StringBuilder sb=new StringBuilder("");
		for(int i=0;i<depth;i++)
		{
			sb.append("-");
		}
		System.out.println(new String(sb)+this.getName());
		for(Company c:list)
		{
			c.display(depth+2);
		}
	}
}
public class FinanceDepartment extends Company
{
	public FinanceDepartment()
	{
		
	}
	public FinanceDepartment(String name)
	{
		super(name);
	}
	@Override
	protected void add(Company company)
	{
		// TODO Auto-generated method stub	
	}
	@Override
	protected void remove(Company company)
	{
		// TODO Auto-generated method stub	
	}
	@Override
	protected void display(int depth) 
	{
		// TODO Auto-generated method stub
		StringBuilder sb=new StringBuilder("");
		for(int i=0;i<depth;i++)
		{
			sb.append("-");
		}
		System.out.println(new String(sb)+this.getName());
	}
}
public class HRDepartment extends Company
{
	public HRDepartment()
	{
		
	}
	public HRDepartment(String name)
	{
		super(name);
	}
	@Override
	protected void add(Company company) 
	{
		// TODO Auto-generated method stub	
	}
	@Override
	protected void remove(Company company) 
	{
		// TODO Auto-generated method stub	
	}
	@Override
	protected void display(int depth)
	{
		// TODO Auto-generated method stub	
		StringBuilder sb=new StringBuilder("");
		for(int i=0;i<depth;i++)
		{
			sb.append("-");
		}
		System.out.println(new String(sb)+this.getName());
	}
}
public class Main  
{  
    public static void main(String[] args)  
    {  
    	Company root=new ConcreteCompany();
		root.setName("北京总公司");
		root.add(new HRDepartment("总公司人力资源部"));
		root.add(new FinanceDepartment("总公司财务部"));
		Company shandongCom=new ConcreteCompany("山东分公司");
		shandongCom.add(new HRDepartment("山东分公司人力资源部"));
		shandongCom.add(new FinanceDepartment("山东分公司账务部"));
		Company zaozhuangCom=new ConcreteCompany("枣庄办事处");
		zaozhuangCom.add(new FinanceDepartment("枣庄办事处财务部"));
		zaozhuangCom.add(new HRDepartment("枣庄办事处人力资源部"));
		Company jinanCom=new ConcreteCompany("济南办事处");
		jinanCom.add(new FinanceDepartment("济南办事处财务部"));
		jinanCom.add(new HRDepartment("济南办事处人力资源部")); 
		shandongCom.add(jinanCom);
		shandongCom.add(zaozhuangCom);
		Company huadongCom=new ConcreteCompany("华东分公司");
		huadongCom.add(new HRDepartment("华东分公司人力资源部"));
		huadongCom.add(new FinanceDepartment("华东分公司账务部"));
		Company hangzhouCom=new ConcreteCompany("杭州办事处");
		hangzhouCom.add(new FinanceDepartment("杭州办事处财务部"));
		hangzhouCom.add(new HRDepartment("杭州办事处人力资源部"));
		Company nanjingCom=new ConcreteCompany("南京办事处");
		nanjingCom.add(new FinanceDepartment("南京办事处财务部"));
		nanjingCom.add(new HRDepartment("南京办事处人力资源部"));
		huadongCom.add(hangzhouCom);
		huadongCom.add(nanjingCom); 
		root.add(shandongCom);
		root.add(huadongCom);
		root.display(0);
    }  
}
组合模式,将对象组合成树形结构以表示部分-整体的层次结构,使得用户对单个对象和组合对象的使用具有一致性。用于对象的部分-整体层次结构,如树形菜单、文件夹菜单、部门组织架构图;也用于需要对用户隐藏组合对象与单个对象的不同时的情况,使得用户统一地使用组合结构中的所有对象。

大话设计模式-组合模式_第1张图片


你可能感兴趣的:(大话设计模式-组合模式)