大话设计模式 组合模式

总公司和分公司

package org.ustc.composite;

public abstract class Company {
	protected String companyName;

	public Company(String companyName) {
		super();
		this.companyName = companyName;
	}
	
	public abstract void add(Company c);
	public abstract void remove(Company c);
	public abstract void display(int depth);
	public abstract void duty();
}
package org.ustc.composite;

import java.util.ArrayList;
import java.util.List;

public class ConcreteCompany extends Company{
	private List<Company> companys;
	public ConcreteCompany(String companyName) {
		super(companyName);
		// TODO Auto-generated constructor stub
		companys = new ArrayList<Company>();
	}

	@Override
	public void add(Company c) {
		// TODO Auto-generated method stub
		companys.add(c);
	}

	@Override
	public void remove(Company c) {
		// TODO Auto-generated method stub
		companys.remove(c);
	}

	

	@Override
	public void duty() {
		// TODO Auto-generated method stub
		for(Company c:companys)
			c.duty();
	}

	@Override
	public void display(int depth) {
		// TODO Auto-generated method stub
		for(int i=0;i<depth;i++){
			System.out.print("-");
		}
		System.out.println(this.companyName);
		for(Company c:companys){
			c.display(depth+2);
		}
	}

}


package org.ustc.composite;

public class HResourceDepartment extends Company{

	public HResourceDepartment(String companyName) {
		super(companyName);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void add(Company c) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void remove(Company c) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void display(int depth) {
		// TODO Auto-generated method stub
		for(int i=0;i<depth;i++)
		System.out.print("-");
		System.out.println(this.companyName);
	}

	@Override
	public void duty() {
		// TODO Auto-generated method stub
		System.out.println("培训员工");
	}

}

package org.ustc.composite;

public class FinanceDepartment extends Company {

	public FinanceDepartment(String companyName) {
		super(companyName);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void add(Company c) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void remove(Company c) {
		// TODO Auto-generated method stub

	}

	@Override
	public void display(int depth) {
		// TODO Auto-generated method stub
		for(int i=0;i<depth;i++)
			System.out.print("-");
		System.out.println(companyName);
	}

	@Override
	public void duty() {
		// TODO Auto-generated method stub
		System.out.println("处理财务");
	}

}

package org.ustc.composite;

public class Main {
public static void main(String[] args) {
	Company root = new ConcreteCompany("北京总公司");
	root.add(new FinanceDepartment("总公司财务部"));
	root.add(new HResourceDepartment("总公司人力资源部"));
	
	Company eastern = new ConcreteCompany("华东公司");
	Company Nanjing = new ConcreteCompany("南京办事处");
	eastern.add(Nanjing);
	Nanjing.add(new FinanceDepartment("南京办事处人力资源"));
	root.add(eastern);
	root.display(1);
	
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值