组合设计模式

一、组合设计模式

1、介绍

  • 组合模式(Composite Pattern),又叫部分整体模式,它创建了对象组的树形结构。将对象组合成树状结构以表示“整体-部分”的层次关系
  • 组合模式依据树形结构来组合对象,用来表示部分以及整体层次
  • 组合模式属于结构型模式
  • 组合模式使得用户对单个对象和组合对象的访问具有一致性。即:组合能让客户以一致的方式处理个别对象以及组合对象

2、原理UML类图

  • Component:这是组合中对象声明接口,定义或实现公共有的接口默认行为,用于访问和管理Component子部件,Component可以是抽象类或接口
  • Composite:非叶子节点,用于存储子部件,在Component接口中实现子部件的相关操作,比如增加、删除、操作
  • Leaf:在组合中表示叶子节点,叶子节点没有子节点

3、组合模式优点和使用

  • 简化客户端操作。客户端只需面对一致的对象而不用考虑整体部分或者节点的问题
  • 具有较强扩展性。当我们要更改组合对象时,我们只需要调整内部的层次关系。客户端不用做出任何改动
  • 方便创建出复杂的层次结构。客户端不用理会组合里面的组成细节,容易添加节点或者叶子,从而创建出复杂的树形结构
  • 需要遍历组织机构,或者处理的对象具有树形结构时,非常适合使用组合模式
  • 要求较高的抽象性,如果节点和叶子有很多差异性的话。比如很多方法和树形不一样,那么不适合使用组合模式

二、普通设计编程

1、介绍

公司、部门、组构成三个层次。

公司包含部门,部门包含组。

如果没有学过组合设计模式,我们一开始的解决方法会如下方所示。Company类聚合Department类,Department类聚合Group类。

此种方法的优势在于简单、容易理解。

但是对于客户端来说,操作复杂,无论我们是增加还是删除一个公司下的部门(或部门下的组),涉及的代码较多。同时这种设计也不利于管理。

2、UML

3、代码

  1. import java.util.List;
  2. /**
  3. * @description: 公司
  4. * @author: dashu
  5. * @create: 10:43
  6. */
  7. public class Company {
  8. /**
  9. * 名称
  10. */
  11. private String name;
  12. /**
  13. * 部门
  14. */
  15. private List<Department> departments;
  16. public String getName() {
  17. return name;
  18. }
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22. public List<Department> getDepartments() {
  23. return departments;
  24. }
  25. public void setDepartments(List<Department> departments) {
  26. this.departments = departments;
  27. }
  28. public void print(){
  29. System.out.println("-----"+name+"-----");
  30. for (Department department : departments){
  31. System.out.println("---"+department.getName()+"---");
  32. for (Group group : department.getGroups()){
  33. System.out.println(group.getName());
  34. }
  35. }
  36. }
  37. }
  1. import java.util.List;
  2. /**
  3. * @description: 部门
  4. * @author: dashu
  5. * @create: 10:55
  6. */
  7. public class Department {
  8. /**
  9. * 名称
  10. */
  11. private String name;
  12. /**
  13. * 组
  14. */
  15. private List<Group> groups;
  16. public String getName() {
  17. return name;
  18. }
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22. public List<Group> getGroups() {
  23. return groups;
  24. }
  25. public void setGroups(List<Group> groups) {
  26. this.groups = groups;
  27. }
  28. public void print() {
  29. System.out.println("---" + name + "---");
  30. for (Group group : groups) {
  31. System.out.println(group.getName());
  32. }
  33. }
  34. }
  1. /**
  2. * @description: 组
  3. * @author: dashu
  4. * @create: 10:58
  5. */
  6. public class Group {
  7. /**
  8. * 名称
  9. */
  10. private String name;
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public void print() {
  18. System.out.println(name);
  19. }
  20. }
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. /**
  4. * @description: 客户端
  5. * @author: dashu
  6. * @create: 10:40
  7. */
  8. public class Client {
  9. public static void main(String[] args) {
  10. Company company = new Company();
  11. company.setName("软件开发公司");
  12. List<Department> departments = new ArrayList<Department>();
  13. company.setDepartments(departments);
  14. Department department1 = new Department();
  15. department1.setName("研发部");
  16. List<Group> groups = new ArrayList<Group>();
  17. department1.setGroups(groups);
  18. Group groupA = new Group();
  19. groupA.setName("A组");
  20. groups.add(groupA);
  21. Group groupB = new Group();
  22. groupB.setName("B组");
  23. groups.add(groupB);
  24. departments.add(department1);
  25. Department department2 = new Department();
  26. department2.setName("财务部");
  27. List<Group> groups2 = new ArrayList<Group>();
  28. department2.setGroups(groups2);
  29. Group groupC = new Group();
  30. groupC.setName("C组");
  31. groups2.add(groupC);
  32. Group groupD = new Group();
  33. groupD.setName("D组");
  34. groups2.add(groupD);
  35. departments.add(department2);
  36. company.print();
  37. }
  38. }

二、组合设计模式编程

1、介绍

组合模式使得用户对单个对象和组合对象的访问具有一致性。即:组合能让客户以一致的方式处理个别对象以及组合对象

2、UML

3、代码

  1. /**
  2. * @description: 组织组件
  3. * @author: dashu
  4. * @create: 11:28
  5. */
  6. public abstract class OrganizationComponent {
  7. private String name;
  8. public OrganizationComponent(String name){
  9. this.name = name;
  10. }
  11. /**
  12. * 添加组织
  13. * @param organizationComponent
  14. */
  15. protected void add(OrganizationComponent organizationComponent){
  16. throw new UnsupportedOperationException();
  17. }
  18. /**
  19. * 删除组织
  20. * @param organizationComponent
  21. */
  22. protected void remove(OrganizationComponent organizationComponent){
  23. throw new UnsupportedOperationException();
  24. }
  25. public String getName() {
  26. return name;
  27. }
  28. public void setName(String name) {
  29. this.name = name;
  30. }
  31. /**
  32. * 抽象打印方法
  33. */
  34. protected abstract void print();
  35. }
  1. /**
  2. * @description: 公司
  3. * @author: dashu
  4. * @create: 10:43
  5. */
  6. public class Company extends OrganizationComponent{
  7. /**
  8. * 名称
  9. */
  10. private String name;
  11. public Company(String name) {
  12. super(name);
  13. }
  14. private List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();
  15. @Override
  16. protected void add(OrganizationComponent organizationComponent) {
  17. organizationComponents.add(organizationComponent);
  18. }
  19. @Override
  20. protected void remove(OrganizationComponent organizationComponent) {
  21. organizationComponents.remove(organizationComponent);
  22. }
  23. @Override
  24. public void print(){
  25. System.out.println("-----"+getName()+"-----");
  26. for (OrganizationComponent organizationComponent : organizationComponents) {
  27. organizationComponent.print();
  28. }
  29. }
  30. }
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. /**
  4. * @description: 部门
  5. * @author: dashu
  6. * @create: 10:55
  7. */
  8. public class Department extends OrganizationComponent{
  9. /**
  10. * 名称
  11. */
  12. private String name;
  13. public Department(String name) {
  14. super(name);
  15. }
  16. private List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();
  17. @Override
  18. protected void add(OrganizationComponent organizationComponent) {
  19. organizationComponents.add(organizationComponent);
  20. }
  21. @Override
  22. protected void remove(OrganizationComponent organizationComponent) {
  23. organizationComponents.remove(organizationComponent);
  24. }
  25. @Override
  26. public void print() {
  27. System.out.println("---"+getName()+"---");
  28. for (OrganizationComponent organizationComponent : organizationComponents) {
  29. organizationComponent.print();
  30. }
  31. }
  32. }
  1. /**
  2. * @description: 组
  3. * @author: dashu
  4. * @create: 10:58
  5. */
  6. public class Group extends OrganizationComponent{
  7. /**
  8. * 名称
  9. */
  10. private String name;
  11. public Group(String name) {
  12. super(name);
  13. }
  14. @Override
  15. public void print() {
  16. System.out.println(getName());
  17. }
  18. }
  1. /**
  2. * @description: 客户端
  3. * @author: dashu
  4. * @create: 11:41
  5. */
  6. public class Client {
  7. public static void main(String[] args) {
  8. OrganizationComponent component = new Company("软件开发公司");
  9. OrganizationComponent department1 = new Department("研发部");
  10. OrganizationComponent department2 = new Department("财务部");
  11. OrganizationComponent a = new Group("A组");
  12. OrganizationComponent b = new Group("B组");
  13. OrganizationComponent c = new Group("C组");
  14. OrganizationComponent d = new Group("D组");
  15. department1.add(a);
  16. department1.add(b);
  17. department2.add(c);
  18. department2.add(d);
  19. component.add(department1);
  20. component.add(department2);
  21. component.print();
  22. System.out.println("------------------------");
  23. department1.print();
  24. System.out.println("------------------------");
  25. department2.print();
  26. }
  27. }