组合模式(Composite)Java


定义

(GoF《设计模式》):将对象组合成树形结构以表示“部分整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。


组合模式(Composite)Java_第1张图片

package ding.study.designpatterns.composite;
/**
 * 公司类抽象啊
 * @author daniel
 *
 */
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 i);
	 //展示部门职能
	 public abstract void lineOfDuty();
	}



package ding.study.designpatterns.composite;

import java.util.ArrayList;

import java.util.List;
/**
 * 公司实现类
 * @author daniel
 *
 */
public class CompanyImpl extends Company {
	 //公司或者部门抽象
	 private List <Company> children=new ArrayList<Company>();
	 
	 public CompanyImpl(String name){
	  super(name);
	 }
	 @Override
	 public void add(Company c) {
 	  children.add(c);

	 }

	 @Override
	 public void display(int i) {
 	  StringBuffer sb=new StringBuffer();
	  for(int j=0;j<i;j++){
	   sb.append("-");
	  }
	  System.out.println(sb.toString()+this.name);

	  for(Company company :children){
	   company.display(i+2);
	  }
	 }

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

	 }

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




package ding.study.designpatterns.composite;

/**
 * 财务 部门类
 * @author daniel
 *
 */
public class FinanceDepartment extends Company {

 public FinanceDepartment (String name){
  super(name);
 }
 @Override
 public void add(Company c) {
  // TODO Auto-generated method stub

 }

 @Override
 public void display(int i) {
  StringBuffer sb=new StringBuffer();
  for(int j=0;j<i;j++){
   sb.append("-");
  }
  System.out.println(sb.toString()+this.name);
 }

 @Override
 public void lineOfDuty() {
  // TODO Auto-generated method stub
  System.out.println(this.name+"    公司财务收支管理");
 }

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

 }

}

 


package ding.study.designpatterns.composite;

/**
 * HR部门类
 * @author daniel
 *
 */
public class HRDepartment extends Company {
 public HRDepartment (String name){
  super(name);
 }

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

 }

 @Override
 public void display(int i) {
  StringBuffer sb=new StringBuffer();
  for(int j=0;j<i;j++){
   sb.append("-");
  }
  System.out.println(sb.toString()+this.name);
 }

 @Override
 public void lineOfDuty() {
  // TODO Auto-generated method stub
  System.out.println(this.name+"   员工管理培训");
 }

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

 }

}


执行

package ding.study.designpatterns.composite;

public class ZTestMain {

	/**
	 * @author daniel
	 * @time 2016-5-27 下午12:41:27
	 * @param args
	 */
	public static void main(String[] args) {
 
		  // TODO Auto-generated method stub
			 CompanyImpl zong=new CompanyImpl("总公司");
		  zong.add(new HRDepartment("总公司HR"));
		  zong.add(new FinanceDepartment("总公司财务"));
		  
		  CompanyImpl bj=new CompanyImpl("bj公司");
		  bj.add(new HRDepartment("bj公司HR"));
		  bj.add(new FinanceDepartment("bj公司财务"));
		  
		  
		  CompanyImpl sh=new CompanyImpl("sh公司");
		  sh.add(new HRDepartment("sh公司HR"));
		  sh.add(new FinanceDepartment("sh公司财务"));
		  
		  zong.add(bj);
		  zong.add(sh);
		  System.out.println("-----------公司结构-------------");
		  zong.display(1);
		  System.out.println("-----------部门职能-------------");
		  zong.lineOfDuty();
		 }
	 

}


结果


组合模式(Composite)Java_第2张图片



源码

https://github.com/dingsai88/StudyTest/tree/master/src/ding/study/designpatterns/composite


你可能感兴趣的:(组合模式(Composite)Java)