电销网站建设,网站内页模板,台州最新消息今天最新动态,免费发布信息的网站平台有哪些文章目录 前言一、组合模式基本介绍二、UML类图三、完整代码抽象类#xff0c;所有类都继承此类学校类以父类型引用组合一个学院类学院类以父类型引用组合一个专业类专业类#xff0c;叶子节点#xff0c;不能再组合其他类测试类 四、组合模式在JDK集合的源码分析五、组合模… 文章目录 前言一、组合模式基本介绍二、UML类图三、完整代码抽象类所有类都继承此类学校类以父类型引用组合一个学院类学院类以父类型引用组合一个专业类专业类叶子节点不能再组合其他类测试类 四、组合模式在JDK集合的源码分析五、组合模式的注意事项和细节 前言 一、组合模式基本介绍 二、UML类图 三、完整代码
抽象类所有类都继承此类
package tanchishell.SJMS.composite;//抽象类也可以是接口或者一个实体类
public abstract class OrganizationComponent {private String name; // 名字private String des; // 说明//不能做成抽象方法因为有的子类不需要实现 add和remove 方法protected void add(OrganizationComponent organizationComponent) {//默认实现throw new UnsupportedOperationException();}protected void remove(OrganizationComponent organizationComponent) {//默认实现throw new UnsupportedOperationException();}//方法 print, 做成抽象的, 子类都需要实现protected abstract void print();//构造器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;}}
学校类以父类型引用组合一个学院类
package tanchishell.SJMS.composite;import java.util.ArrayList;
import java.util.List;// 学校类 University 就是 Composite , 可以管理 College
public class University extends OrganizationComponent {ListOrganizationComponent organizationComponents new ArrayListOrganizationComponent();// 构造器public University(String name, String des) {super(name, des);//}// 重写 addOverrideprotected void add(OrganizationComponent organizationComponent) {organizationComponents.add(organizationComponent);}// 重写 removeOverrideprotected void remove(OrganizationComponent organizationComponent) {organizationComponents.remove(organizationComponent);}// print 方法就是输出 University 包含的学院Overrideprotected void print() {System.out.println(-------------- getName() --------------);//遍历 organizationComponentsfor (OrganizationComponent organizationComponent : organizationComponents) {organizationComponent.print();}}Overridepublic String getName() {return super.getName();}Overridepublic String getDes() {return super.getDes();}}
学院类以父类型引用组合一个专业类
package tanchishell.SJMS.composite;import java.util.ArrayList;
import java.util.List;//学院类
public class College extends OrganizationComponent{ListOrganizationComponent organizationComponents new ArrayListOrganizationComponent();// 构造器public College(String name, String des) {super(name, des);//}// 重写 addOverrideprotected void add(OrganizationComponent organizationComponent) {organizationComponents.add(organizationComponent);}// 重写 removeOverrideprotected void remove(OrganizationComponent organizationComponent) {organizationComponents.remove(organizationComponent);}// print 方法就是输出 College 包含的专业Overrideprotected void print() {System.out.println(-------------- getName() --------------);//遍历 organizationComponentsfor (OrganizationComponent organizationComponent : organizationComponents) {organizationComponent.print();}}Overridepublic String getName() {return super.getName();}Overridepublic String getDes() {return super.getDes();}
}
专业类叶子节点不能再组合其他类
package tanchishell.SJMS.composite;public class Department extends OrganizationComponent {//没有集合public Department(String name, String des) {super(name, des);}
//add , remove 就不用写了因为他是叶子节点Overridepublic String getName() {return super.getName();}Overridepublic String getDes() {return super.getDes();}Overrideprotected void print() {//输出自己的name 就可以了System.out.println(getName());}
}
测试类
package tanchishell.SJMS.composite;public class Client {public static void main(String[] args) {//从大到小创建对象 学校OrganizationComponent university new University(清华大学, 中国顶级大学 );//创建 学院OrganizationComponent computerCollege new College(计算机学院, 计算机学院 );OrganizationComponent infoEngineercollege new College(信息工程学院, 信息工程学院 );//创建各个学院下面的系(专业)computerCollege.add(new Department(软件工程, 软件工程不错 ));computerCollege.add(new Department(网络工程, 网络工程不错 ));computerCollege.add(new Department(计算机科学与技术, 计算机科学与技术是老牌的专业 ));System.out.println(--------------------------------------------------------------);infoEngineercollege.add(new Department(通信工程, 通信工程不好学 ));infoEngineercollege.add(new Department(信息工程, 信息工程好学 ));//将学院加入到 学校university.add(computerCollege);university.add(infoEngineercollege);university.print();//输出 信息工程学院//infoEngineercollege.print();//输出 计算机学院
// computerCollege.print();}
}输出
--------------------------------------------------------------
--------------清华大学--------------
--------------计算机学院--------------
软件工程
网络工程
计算机科学与技术
--------------信息工程学院--------------
通信工程
信息工程
四、组合模式在JDK集合的源码分析 HashMap的组合模式对比我们上面的组合又多做了一层接口 Map 所有的Map子类都实现了Map接口 然后就是一个抽象类 AbstractMap所有的子类都去继承了这个 抽象类 和我们上面一样抽象类这里也抛出了异常防止叶子节点自动继承该方法叶子节点不能再进行 put 动作。 来到 HashMap 继承了 抽象map 和map 接口而且 put 方法有方法体有具体实现 五、组合模式的注意事项和细节