【设计模式】Java设计模式 – 组合模式

Java设计模式 - 组合模式

😄 不断学习才是王道
🔥 继续踏上学习之路,学之分享笔记
👊 总有一天我也能像各位大佬一样
🏆原创作品,更多关注我CSDN: 一个有梦有戏的人
👊准备将博客园、CSDN一起记录分享自己的学习心得!!!
🌝分享学习心得,欢迎指正,大家一起学习成长!

【设计模式】Java设计模式 - 组合模式

简介

组合模式(Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。

组合模式原理

首先先看一下组合的UML图
【设计模式】Java设计模式 - 组合模式

Compoent:抽象类或者接口,是组合对象声明的接口,实现所有类的默认行为,用于访问、管理子部件。
Leaf:组合中的叶子节点,最小的类
Composite:非叶子节点,用来操作组合对象,存储子部件。

组合模式实例

接下来用一个例子来学习组合模式,学校有学院,学院下有专业,这就是一层一层的关系,需要在一个页面中展现出那个学校有什么学院,学院下有什么专业。
来看一下例子的类图
【设计模式】Java设计模式 - 组合模式

①、定义抽象类-Component

定义属性,构造器,getset,操作方法需要默认实现,因为在叶子节点是不需要去实现的,如果定义成抽象类,子类就必须实现了。在定义一个抽象类-打印信息。

package com.lyd.demo.composite;  /**  * @Author: lyd  * @Description: 抽象类  * @Date: 2022-08-30  */ public abstract class OrganizationComponent {     private String name;     private String description;     public OrganizationComponent(String name, String description) {         this.name = name;         this.description = description;     }     public String getName() {         return name;     }     public void setName(String name) {         this.name = name;     }     public String getDescription() {         return description;     }     public void setDescription(String description) {         this.description = description;     }     // 添加 - 子类不一定需要实现     public void add(OrganizationComponent organizationComponent) {         throw new UnsupportedOperationException();     }     // 删除 - 子类不一定需要实现     public void remove(OrganizationComponent organizationComponent) {         throw new UnsupportedOperationException();     }     // 打印 - 子类必须去实现     public abstract void print(); } 

②、定义叶子类和非叶子类

大学类:非叶子,组合 院系类;定义一个数组来存放组合对象,通过重写操作方法对其进行操作。

package com.lyd.demo.compose; import com.lyd.demo.composite.OrganizationComponent; import java.util.ArrayList; import java.util.List; /**  * @Author: lyd  * @Description: 学校类 - 继承OrganizationComponent - 组合 院系类  * @Date: 2022-08-30  */ public class University extends OrganizationComponent {     // 组合 College 类     List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();     public University(String name, String description) {         super(name, description);     }     /**      * 重写add      */     @Override     public void add(OrganizationComponent organizationComponent) {         organizationComponents.add(organizationComponent);     }     /**      * 重写remove      */     @Override     public void remove(OrganizationComponent organizationComponent) {         organizationComponents.add(organizationComponent);     }     @Override     public String getName() {         return super.getName();     }     @Override     public String getDescription() {         return super.getDescription();     }     // 打印包含学院的信息     @Override     public void print() {         System.out.println("< " + getName() + " >");         // 将所有学院信息打印出来         for (OrganizationComponent o : organizationComponents) {             o.print();         }     } } 

学院类:非叶子,组合Department类,定义一个数组来存放组合对象,通过重写操作方法对其进行操作。

package com.lyd.demo.compose; import com.lyd.demo.composite.OrganizationComponent; import java.util.ArrayList; import java.util.List; /**  * @Author: lyd  * @Description: 学院类  * @Date: 2022-08-30  */ public class College extends OrganizationComponent {     // 组合 Department 类     List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();     public College(String name, String description) {         super(name, description);     }     /**      * 重写add      */     @Override     public void add(OrganizationComponent organizationComponent) {         organizationComponents.add(organizationComponent);     }     /**      * 重写remove      */     @Override     public void remove(OrganizationComponent organizationComponent) {         organizationComponents.add(organizationComponent);     }     @Override     public String getName() {         return super.getName();     }     @Override     public String getDescription() {         return super.getDescription();     }     // 打印包含学院的信息     @Override     public void print() {         System.out.println("< " + getName() + " >");         // 将所有专业信息打印出来         for (OrganizationComponent o : organizationComponents) {             o.print();         }     } } 

专业类:叶子节点,没有组合的集合,所以不需要进行操作,只需要进行输出打印。

package com.lyd.demo.compose; import com.lyd.demo.composite.OrganizationComponent; /**  * @Author: lyd  * @Description: 专业类  * @Date: 2022-08-30  */ public class Department extends OrganizationComponent {     // 已经没有集合了     public Department(String name, String description) {         super(name, description);     }     @Override     public String getName() {         return super.getName();     }     @Override     public String getDescription() {         return super.getDescription();     }     // 叶子节点,就不需要add和remove     @Override     public void print() {         System.out.println(getName());     } } 

③、测试

package com.lyd.demo.test; import com.lyd.demo.compose.College; import com.lyd.demo.compose.Department; import com.lyd.demo.compose.University; import com.lyd.demo.composite.OrganizationComponent; /**  * @Author: lyd  * @Description: 测试  * @Date: 2022-08-30  */ public class ComposeTest {     public static void main(String[] args) {         // 创建大学         OrganizationComponent ZheJiangUniversity = new University("浙江大学", "人才之地");         // 创建学院         OrganizationComponent ComputerCollege = new College("计算机科学与技术学院", "-->计算机科学与技术学院");         OrganizationComponent OpticalCollege = new College("光电科学与工程学院", "-->光电科学与工程学院");         // 创建专业         Department ComputerDepartment = new Department("计算机科学与技术", "--计算机科学与技术");         Department SoftWareDepartment = new Department("软件工程", "--软件工程");         Department OpticalDepartment = new Department("光电信息科学与工程", "--光电信息科学与工程");         // 添加专业         ComputerCollege.add(ComputerDepartment);         ComputerCollege.add(SoftWareDepartment);         OpticalCollege.add(OpticalDepartment);         // 添加学院         ZheJiangUniversity.add(ComputerCollege);         ZheJiangUniversity.add(OpticalCollege);         // 打印所有         ZheJiangUniversity.print();         System.out.println("*******************************");         // 打印学院         ComputerCollege.print();         System.out.println("*******************************");         // 打印院系         ComputerDepartment.print();     } } 

组合可以理解成是层层相套。
运行结果:
【设计模式】Java设计模式 - 组合模式

通俗的讲,组合模式就是将对象组合到非类中,在非子类中进行对他们的操作,有种层层相套的感觉,可以通过打断点的形式一步一步了解。

👍创作不易,可能有些语言不是很通畅,如有错误请指正,感谢观看!记得一键三连哦!👍

发表评论

评论已关闭。

相关文章