前言(一些废话,可以忽略)
- 组合模式,是我们依赖关系中的一种,在这里又变换成为一种设计模式,那么它的核心肯定也是将类与类通过组合的方式拼凑在一起了
- PS.部分类实现见文末
要解决的问题
- 我们都知道一个单位,有组织架构,一个学校有各个不同的院系,一个院系又有不同的部门,那么我们式样哪种方式来方便扩展的,符合代码设计原则的方式来组织这种层级架构关系呢
- 这就是组合模式,通过将一个不同层级的组织机构一层一层的关联,既方便了扩展,又十分清晰的捋顺了各个方面的线条
组合模式
- 以学校来举例,学校下面有院系,院系下面有部门,但是这三种层级结构都是组织,所以我们创建父类或接口
- 可以向顶级组织学校中添加院系,可以向院系中添加部门,及删改查等
/**
* @program: ade-someproblem
* @author: cade franklin
* @create: 2019-12-29 29:11
**/
public interface OrganizationComponent {
void add(OrganizationComponent department);
void print();
void remove(OrganizationComponent department);
}
- 学校,是一个组织,实现OrganizationComponent,在它中间包含院系的List,将其组合进来
- 另外院系和部门的组织相似
/**
* @program: ade-someproblem
* @author: cade franklin
* @create: 2019-12-29 29:11
**/
public class University implements OrganizationComponent {
private String name;
private List<College> collegeList = new ArrayList();
public University(String name) {
this.name = name;
}
@Override
public void add(OrganizationComponent organizationComponent) {
if(organizationComponent instanceof College){
College college = (College)organizationComponent;
collegeList.add(college);
}
}
@Override
public void print() {
System.out.println("======University.print:"+name);
for (int i = 0; i < collegeList.size(); i++) {
OrganizationComponent college = collegeList.get(i);
college.print();
}
}
@Override
public void remove(OrganizationComponent organizationComponent) {
if(organizationComponent instanceof College){
College college = (College)organizationComponent;
collegeList.remove(college);
}
}
}
- 我们的使用如下
/**
* @program: ade-someproblem
* @author: cade franklin
* @create: 2019-12-29 29:11
**/
public class ComponsiteMode {
public static void main(String[] args) {
OrganizationComponent department = new Department("团委");
OrganizationComponent department1 = new Department("院办");
OrganizationComponent department2 = new Department("教研室");
OrganizationComponent college = new College("计算机学院");
college.add(department);
college.add(department1);
college.add(department2);
OrganizationComponent college1 = new College("电子信息学院");
OrganizationComponent university = new University("XXX大学");
university.add(college);
university.add(college1);
// 需要打印学校就直接从学校开始调用
// university.print();
// 如果需要从下一层级遍历,直接使用下一层级,非常的灵活
college.print();
}
}
总结
- 组合模式是通过组合的方式,将下一层级的组织结构组合在类中,这样能够灵活的使用
附录代码
/**
* @program: ade-someproblem
* @author: cade franklin
* @create: 2019-12-29 29:11
**/
public class Department implements OrganizationComponent {
private String name;
public Department(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void add(OrganizationComponent department) {
throw new UnsupportedOperationException("Department.add");
}
@Override
public void print() {
System.out.println("Department.print:"+name);
}
@Override
public void remove(OrganizationComponent department) {
throw new UnsupportedOperationException("Department.remove");
}
}
/**
* @program: ade-someproblem
* @author: cade franklin
* @create: 2019-12-29 29:11
**/
public class College implements OrganizationComponent {
private String name;
private List<Department> departmentList = new ArrayList();
public College(String name) {
this.name = name;
}
@Override
public void add(OrganizationComponent organizationComponent) {
if(organizationComponent instanceof Department){
Department department = (Department)organizationComponent;
departmentList.add(department);
}
}
@Override
public void print() {
System.out.println("------College.print:"+name);
for (int i = 0; i < departmentList.size(); i++) {
OrganizationComponent department = departmentList.get(i);
department.print();
}
}
@Override
public void remove(OrganizationComponent organizationComponent) {
if(organizationComponent instanceof Department){
Department department = (Department)organizationComponent;
departmentList.remove(department);
}
}
}
愿你不舍爱与自由。