组合模式
前言
提示:以下是本篇文章正文内容,下面案例可供参考
一、什么是组合模式
对于树形结构,当容器对象(如文件夹)的某一个方法被调用时,将遍历整个树形结构,寻找也包含这个方法的成员对象(可以是容器对象,也可以是叶子对象)并调用执行。这是靠递归调用的机制实现的。
由于容器对象和叶子对象在功能上的区别,在使用这些对象的代码中必须有区别地对待容器对象和叶子对象,而实际上大多数情况下我们希望一致地处理它们,因为对于这些对象的区别对待将会使得程序非常复杂。组合模式为解决此类问题而诞生,它可以让叶子对象和容器对象的使用具有一致性
二、使用步骤
1.抽象接口
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) {
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;
}
protected abstract void print(); // 方法print 子类都需要实现
}
2.学校
public class University extends OrganizationComponent{
List<OrganizationComponent> organizationComponents = new ArrayList<>();
public University(String name, String des) {
super(name, des);
}
@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 getDes() {
return super.getDes();
}
// 就是输出University 包含的学院
@Override
protected void print() {
System.out.println("====================="+getName()+"==============");
for (OrganizationComponent organizationComponent : organizationComponents) {
organizationComponent.print();
}
}
}
3.学院
public class College extends OrganizationComponent{
List<OrganizationComponent> organizationComponents = new ArrayList<>();
public College(String name, String des) {
super(name, des);
}
@Override
protected void add(OrganizationComponent organizationComponent) {
// 实际业务中 College 的 add 和 University add 不一定相同
organizationComponents.add(organizationComponent);
}
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponents.remove(organizationComponent);
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDes() {
return super.getDes();
}
@Override // 就是输出University 包含的学院
protected void print() {
System.out.println("====================="+getName()+"==============");
for (OrganizationComponent organizationComponent : organizationComponents) {
organizationComponent.print();
}
}
}
4.系
public class Department extends OrganizationComponent{
public Department(String name, String des) {
super(name, des);
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDes() {
return super.getDes();
}
@Override
protected void print() {
System.out.println(getName());
}
}
5.使用
public class Client {
public static void main(String[] args) {
// 从大到小创建对象 学校
University university = new University("清华大学", "顶级大学");
//学院
College college1 = new College("计算机学院", "计算机学院描述");
College college2 = new College("信息工程学院", "信息工程学院描述");
// 创建各个学院下面的系
college1.add(new Department("软件工程","软件工程描述"));
college1.add(new Department("网络工程","网络工程描述"));
college1.add(new Department("计算机科学与技术","计算机科学与技术描述"));
college2.add(new Department("通信工程","通信工程描述"));
college2.add(new Department("信息工程","信息工程描述"));
// 将学院加入到 学院中
university.add(college1);
university.add(college2);
// university.print();
college1.print();
college2.print();
}
}
6.输出
7.HashMap的组合模式
总结
需要遍历组织机构,或者处理的对象具有树形结构时,非常适合使用
要求比较高的抽象性,如果节点和叶子有很多差异性的话,比如很多方法属性都不一样,不适合使用组合模式
- 简化客户端操作。客户端只需要面对一致的对象而不用考虑整体部分或者节点叶子的问题。
- 具有较强的扩展性。当我们要更改组合对象时,我们只需要调整内部的层次关系,客户端不用做出任何改动。
- 方便创建出复杂的层次结构。客户端不用理会组合里面的组成细节,容易添加节点或者叶子从而创建出复杂的树形结构
- 需要遍历组织机构,或者处理的对象具有树形结构时,非常适合使用组合模式
- 要求较高的抽象性,如果节点和叶子有很多差异性的话,比如很多方法和属性都不一样,不适合使用组合模式
学习来源 B站老韩
跳转开始卷:https://space.bilibili.com/651245581?spm_id_from=333.337.0.0
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/112543.html