大话设计模式十九:组合模式(分公司 = 一部门)

组合模式使用户对单个对象和组合对象的使用具有一致性。

应用场景:需求中体香部分和整体层次的结构或希望用户忽略组合对象与单个对象的不同,可以统一地使用组合结构中的所有对象。

优点:基本对象可以被组合成更复杂的组合对象,而这个组合对象又可以被组合,可以不断地递归下去。

// 抽象公司类
public abstract class Company {

	private String name;

	public Company(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	// 增加
	public abstract void add(Company c);

	// 移除
	public abstract void remove(Company c);

	// 显示
	public abstract void display(int depth);

	// 履行职责
	public abstract void lineOfDuty();
}


// 具体公司类
public class ConcreteCompany extends Company {

	private List<Company> children = new ArrayList<Company>();

	public ConcreteCompany(String name) {
		super(name);
	}

	@Override
	public void add(Company c) {
		children.add(c);
	}

	@Override
	public void display(int depth) {
		System.out.println("-" + this.getName());

		for (Company c : children) {
			for (int i = 0; i < depth; i++) {
				System.out.print("--");
			}
			c.display(depth + 2);
		}
	}

	@Override
	public void lineOfDuty() {
		for (Company c : children) {
			c.lineOfDuty();
		}
	}

	@Override
	public void remove(Company c) {
		children.remove(c);
	}

}


public class FinanceDepartment extends Company {

	public FinanceDepartment(String name) {
		super(name);
	}

	@Override
	public void add(Company c) {

	}

	@Override
	public void display(int depth) {
		System.out.println("-" + this.getName());
	}

	@Override
	public void lineOfDuty() {
		System.out.println(this.getName() + "All the things about Finance!");
	}

	@Override
	public void remove(Company c) {
	}

}

public class HRDepartment extends Company {

	public HRDepartment(String name) {
		super(name);
	}

	@Override
	public void add(Company c) {
	}

	@Override
	public void display(int depth) {
		System.out.println("-" + this.getName());
	}

	@Override
	public void lineOfDuty() {
		System.out.println(this.getName() + " recruitment and training!");
	}

	@Override
	public void remove(Company c) {

	}

}

public class CompositeMain {

	public static void main(String[] args) {
		ConcreteCompany root = new ConcreteCompany("The Head Office Of Our Company In Beijing");
		root.add(new HRDepartment("Head Office HR"));
		root.add(new FinanceDepartment("Head Office Finance"));

		ConcreteCompany comp = new ConcreteCompany("The leaf company in shanghai");
		comp.add(new HRDepartment("shanghai office HR"));
		comp.add(new FinanceDepartment("shanghai office finance"));
		root.add(comp);

		ConcreteCompany comp1 = new ConcreteCompany("the office of shanghai company in nanjing");
		comp1.add(new HRDepartment("nanjing office HR"));
		comp1.add(new FinanceDepartment("nanjing office finance"));
		comp.add(comp1);

		ConcreteCompany comp2 = new ConcreteCompany("the office of shanghai company in hangzhou");
		comp2.add(new HRDepartment("hangzhou office HR"));
		comp2.add(new FinanceDepartment("hangzhou office finance"));
		comp.add(comp2);

		System.out.println("the structure of our company:");
		root.display(1);

		System.out.println("the duty of every department:");
		root.lineOfDuty();
	}

}


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