//1.定义接口,包括所有的属性和方法,传入的是自身的实现类
//2.定义实现类,重写相关方法
//3.直到叶子节点为止
package com.serlyf.composite;
public abstract class OrganizationComponent {
private String name;
private String desc;
public OrganizationComponent(String name, String desc) {
this.name = name;
this.desc = desc;
}
protected void add(OrganizationComponent organizationComponent){
//默认实现
throw new UnsupportedOperationException();
}
protected void remove(OrganizationComponent organizationComponent){
//默认实现
throw new UnsupportedOperationException();
}
protected abstract void print();
public String getName() {
return name;
}
public String getDesc() {
return desc;
}
public void setName(String name) {
this.name = name;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
public class University extends OrganizationComponent{
//存放的是学院College
List<OrganizationComponent> organizationComponents=new ArrayList<OrganizationComponent>();
public University(String name, String desc) {
super(name, desc);
}
@Override
protected void add(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponents.remove(organizationComponent);
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDesc() {
return super.getDesc();
}
protected void print() {
System.out.println("=============="+getName()+"=================");
for(OrganizationComponent organizationComponent:organizationComponents){
organizationComponent.print();
}
}
}
public class College extends OrganizationComponent{
//存放的是专业department
List<OrganizationComponent> organizationComponents=new ArrayList<OrganizationComponent>();
public College(String name, String desc) {
super(name, desc);
}
@Override
protected void add(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponents.remove(organizationComponent);
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDesc() {
return super.getDesc();
}
protected void print() {
System.out.println("=============="+getName()+"=================");
for(OrganizationComponent organizationComponent:organizationComponents){
organizationComponent.print();
}
}
}
public class Department extends OrganizationComponent{
public Department(String name, String desc) {
super(name, desc);
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDesc() {
return super.getDesc();
}
protected void print() {
System.out.println("=============="+getName()+"=================");
}
}
public class CompositeTest {
public static void main(String[] args) {
//创建大学
OrganizationComponent university = new University("四川大学", "好的大学");
//创建大学下的学院
OrganizationComponent college1 = new College("机械学院", "不错");
OrganizationComponent college2 = new College("电气学院", "也不错");
//创建专业
college1.add(new Department("机制专业","不错哦"));
college1.add(new Department("自动化","也不错哦"));
college2.add(new Department("电气专业","不错哦"));
college2.add(new Department("电气而言","也不错哦"));
university.add(college1);
university.add(college2);
university.print();
}
}
标签:10,设计模式,String,组合,name,organizationComponent,OrganizationComponent,public,desc From: https://www.cnblogs.com/NIAN2011/p/16754726.html