代码如下:
public abstract class OrganizationComponent { private String name; // 名字 private String des; // 说明 protected void add(OrganizationComponent organizationComponent) { //默认实现 throw new UnsupportedOperationException(); } protected void remove(OrganizationComponent organizationComponent) { //默认实现 throw new UnsupportedOperationException(); } //构造器 public OrganizationComponent(String name, String des) { super(); this.name = name; this.des = des; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDes() { return des; } public void setDes(String des) { this.des = des; } //方法print, 做成抽象的, 子类都需要实现 protected abstract void print(); }
//University 就是 Composite , 可以管理College public class University extends OrganizationComponent { List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>(); // 构造器 public University(String name, String des) { super(name, des); // TODO Auto-generated constructor stub } // 重写add @Override protected void add(OrganizationComponent organizationComponent) { // TODO Auto-generated method stub organizationComponents.add(organizationComponent); } // 重写remove @Override protected void remove(OrganizationComponent organizationComponent) { // TODO Auto-generated method stub organizationComponents.remove(organizationComponent); } @Override public String getName() { // TODO Auto-generated method stub return super.getName(); } @Override public String getDes() { // TODO Auto-generated method stub return super.getDes(); } // print方法,就是输出University 包含的学院 @Override protected void print() { // TODO Auto-generated method stub System.out.println("--------------" + getName() + "--------------"); //遍历 organizationComponents for (OrganizationComponent organizationComponent : organizationComponents) { organizationComponent.print(); } } }
public class College extends OrganizationComponent { //List 中 存放的Department List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>(); // 构造器 public College(String name, String des) { super(name, des); // TODO Auto-generated constructor stub } // 重写add @Override protected void add(OrganizationComponent organizationComponent) { // TODO Auto-generated method stub // 将来实际业务中,Colleage 的 add 和 University add 不一定完全一样 organizationComponents.add(organizationComponent); } // 重写remove @Override protected void remove(OrganizationComponent organizationComponent) { // TODO Auto-generated method stub organizationComponents.remove(organizationComponent); } @Override public String getName() { // TODO Auto-generated method stub return super.getName(); } @Override public String getDes() { // TODO Auto-generated method stub return super.getDes(); } // print方法,就是输出University 包含的学院 @Override protected void print() { // TODO Auto-generated method stub System.out.println("--------------" + getName() + "--------------"); //遍历 organizationComponents for (OrganizationComponent organizationComponent : organizationComponents) { organizationComponent.print(); } } }
public class Department extends OrganizationComponent { //没有集合 public Department(String name, String des) { super(name, des); // TODO Auto-generated constructor stub } //add , remove 就不用写了,因为他是叶子节点 @Override public String getName() { // TODO Auto-generated method stub return super.getName(); } @Override public String getDes() { // TODO Auto-generated method stub return super.getDes(); } @Override protected void print() { // TODO Auto-generated method stub System.out.println(getName()); } }
public class Client { public static void main(String[] args) { // TODO Auto-generated method stub //从大到小创建对象 学校 OrganizationComponent university = new University("清华大学", " 中国顶级大学 "); //创建 学院 OrganizationComponent computerCollege = new College("计算机学院", " 计算机学院 "); OrganizationComponent infoEngineercollege = new College("信息工程学院", " 信息工程学院 "); //创建各个学院下面的系(专业) computerCollege.add(new Department("软件工程", " 软件工程不错 ")); computerCollege.add(new Department("网络工程", " 网络工程不错 ")); computerCollege.add(new Department("计算机科学与技术", " 计算机科学与技术是老牌的专业 ")); // infoEngineercollege.add(new Department("通信工程", " 通信工程不好学 ")); infoEngineercollege.add(new Department("信息工程", " 信息工程好学 ")); //将学院加入到 学校 university.add(computerCollege); university.add(infoEngineercollege);
university.print();
infoEngineercollege.print(); } }
运行结果如下:
--摘自韩顺平大神笔记
原文:https://www.cnblogs.com/liuyi13535496566/p/14839302.html