组合模式

以下内容摘抄于《C#大话设计模式》

组合模式(Composite),将对象组合成树形结构以表示‘部分-整体’的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。

组合模式的结构图如下:



代码如下

package com.schema.composite;

public abstract class Component {
	protected String name;

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

	public abstract void Add(Component c);
	public abstract void Remove(Component c);
	public abstract void Display(int depth);

	protected String GetStr(int depth){
		String res = "";

		for(int i = 0; i < depth; ++i){
			res += "-";
		}

		return res;
	}
}

package com.schema.composite;

public class Leaf extends Component {
	public Leaf(String name){
		super(name);
	}

	public void Add(Component c){
		System.out.println("can not add to a left.");
	}

	public void Remove(Component c){
		System.out.println("can not remove from a left.");
	}

	public void Display(int depth){
		System.out.println(GetStr(depth) + name);
	}
}

package com.schema.composite;

import java.util.LinkedList;
import java.util.List;

public class Composite extends Component{

	public List<Component> tree = new LinkedList<Component>();

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

	public void Add(Component c){
		tree.add(c);
	}

	public void Remove(Component c){
		tree.remove(c);
	}

	public void Display(int depth){
		System.out.println(GetStr(depth) + name);

		for(Component c : tree){
			c.Display(depth + 2);
		}
	}
}

package com.schema.composite;

public class AppMain {
	public static void main(String[] args) {
		// 创建一个根节点
		Component root = new Composite("root");
		// 在根节点上添加叶子A和B
		root.Add(new Leaf("Leaf A"));
		root.Add(new Leaf("Leaf B"));

		Composite comp = new Composite("Composite X");
		// 在分支节点comp上添加叶子节点XA和XB
		comp.Add(new Leaf("Leaf XA"));
		comp.Add(new Leaf("Leaf XB"));
		// 向根节点添加分支节点comp
		root.Add(comp);

		Component comp2 = new Composite("Composite XY");

		comp2.Add(new Leaf("Leaf XYA"));
		comp2.Add(new Leaf("Leaf XYB"));

		comp.Add(comp2);

		root.Add(new Leaf("Leaf C"));

		Leaf leaf = new Leaf("Leaf D");

		root.Add(leaf);
		root.Remove(leaf);

		root.Display(1);
	}
}

运行结果如下:


下面举一个公司管理系统的例子

代码结构图如下:


代码如下:

package com.schema.composite;

public abstract class Company {
	protected String name;

	public Company(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();// 履行职责

	protected String GetStr(int depth){
		String res = "";

		for(int i = 0; i < depth; ++i){
			res += "-";
		}

		return res;
	}
}

package com.schema.composite;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class ConcreteCompany extends Company{
	private List<Company> branchCompanies = new LinkedList<Company>();

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

	public void Add(Company c){
		branchCompanies.add(c);
	}

	public void Remove(Company c){
		branchCompanies.remove(c);
	}

	public void Display(int depth){
		System.out.println(GetStr(depth) + name);

		for(Company c : branchCompanies){
			c.Display(depth + 2);
		}
	}

	public void  LineOfDuty(){
		for (Iterator<Company> iterator = branchCompanies.iterator(); iterator.hasNext();) {
			Company type = (Company) iterator.next();

			type.LineOfDuty();
		}
	}
}

package com.schema.composite;

public class HRDepartment extends Company {
	public HRDepartment(String name){
		super(name);
	}

	public void Add(Company c){

	}

	public void Remove(Company c){

	}

	public void Display(int depth){
		System.out.println(GetStr(depth) + name);
	}

	public void LineOfDuty(){
		System.out.println(name + " 员工招聘培训管理");
	}
}

package com.schema.composite;

public class FinanceDepartment extends Company {
	public FinanceDepartment(String name){
		super(name);
	}

	public void Add(Company c){

	}

	public void Remove(Company c){

	}

	public void Display(int depth){
		System.out.println(GetStr(depth) + name);
	}

	public void LineOfDuty(){
		System.out.println(name + " 公司财务收支管理");
	}
}

package com.schema.composite;

public class AppMain {
	public static void main(String[] args) {
		ConcreteCompany root = new ConcreteCompany("北京总公司");

		root.Add(new HRDepartment("总公司人力资源部"));
		root.Add(new FinanceDepartment("总公司财务部"));

		ConcreteCompany comp = new ConcreteCompany("上海华东分公司");

		comp.Add(new HRDepartment("华东分公司人力资源部"));
		comp.Add(new FinanceDepartment("华东分公司财务部"));

		root.Add(comp);

		ConcreteCompany comp1 = new ConcreteCompany("南京办事处");

		comp1.Add(new HRDepartment("南京办事处人力资源部"));
		comp1.Add(new FinanceDepartment("南京办事处财务部"));

		comp.Add(comp1);

		ConcreteCompany comp2 = new ConcreteCompany("杭洲办事处");

		comp2.Add(new HRDepartment("杭州办事处人力资源部"));
		comp2.Add(new FinanceDepartment("杭州办事处财务部"));

		comp.Add(comp2);

		System.out.println("结构图:");

		root.Display(1);

		System.out.println("职责:");
		root.LineOfDuty();
	}
}
运行结果如下:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值