组合模式介绍
- 使用组合模式可以让用户可以使用统一的方式处理整个树形结构的个别对象和组合对象,从而简化客户端的操作。并且扩展性好
- 当需要处理的对象是树形结构时可以考虑使用组合模式。
- 节点和叶子节点存在很大差异的情况下不建议使用组合模式。
代码举例
不使用组合模式举例
一个公司有很多部门,每个部门有很多分组
/**
* 分组
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Group {
//分组名称
private String name;
//输出分组
public void print() {
System.out.println(name);
}
}
/**
* 部门
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
//部门名
private String name;
//部门下所有的分组
private List<Group> groups;
//输出部门 分组
public void print() {
System.out.println("---" + name + "---");
for (Group group : groups) {
System.out.println(group.getName());
}
}
}
/**
* 公司
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Company {
//公司名
private String name;
//公司下的部门
private List<Department> departments;
//输出公司 部门 分组
public void print(){
System.out.println("-----"+name+"-----");
for (Department department : departments){
System.out.println("---"+department.getName()+"---");
for (Group group : department.getGroups()){
System.out.println(group.getName());
}
}
}
}
//客户端测试用例
public static void main(String[] args) {
//分组
Group groupA = new Group("A组");
Group groupB = new Group("B组");
Group groupC = new Group("C组");
Group groupD = new Group("D组");
//部门
Department department1 = new Department("开发部",
new ArrayList<>(Arrays.asList(groupA, groupB)));
Department department2 = new Department("财务部",
new ArrayList<>(Arrays.asList(groupC, groupD)));
//公司
Company company = new Company("XXX科技公司",
new ArrayList<>(Arrays.asList(department1, department2)));
//输出
company.print();
}
使用组合模式优化后
/**
* 组织组件
*/
public abstract class OrganizationComponent {
private String name;
//添加组织
public void add(OrganizationComponent organizationComponent){
throw new UnsupportedOperationException();
}
//删除组织
public void remove(OrganizationComponent organizationComponent){
throw new UnsupportedOperationException();
}
//抽象打印方法
public abstract void print();
}
/**
* 分组
*/
public class Group extends OrganizationComponent{
/**
* 分组名称
*/
private String name;
public Group(String name){
this.name = name;
}
public String getName() {
return name;
}
//输出名称
@Override
public void print() {
System.out.println(this.getName());
}
}
/**
* 部门
*/
public class Department extends OrganizationComponent{
//部门名称
private String name;
public Department(String name) {
this.name = name;
}
public String getName() {
return name;
}
private List<OrganizationComponent> organizationComponents = new ArrayList<>();
@Override
public void add(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
@Override
public void remove(OrganizationComponent organizationComponent) {
organizationComponents.remove(organizationComponent);
}
@Override
public void print() {
System.out.println("---"+getName()+"---");
for (OrganizationComponent organizationComponent : organizationComponents) {
organizationComponent.print();
}
}
}
/**
* 公司
*/
public class Company extends OrganizationComponent{
/**
* 公司名称
*/
private String name;
public Company(String name){
this.name = name;
}
public String getName() {
return name;
}
private List<OrganizationComponent> organizationComponents = new ArrayList<>();
@Override
public void add(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
@Override
public void remove(OrganizationComponent organizationComponent) {
organizationComponents.remove(organizationComponent);
}
@Override
public void print(){
System.out.println("-----"+getName()+"-----");
for (OrganizationComponent organizationComponent : organizationComponents) {
organizationComponent.print();
}
}
}
//客户端测试用例
public static void main(String[] args) {
OrganizationComponent company = new Company("XXX开发公司");
OrganizationComponent department1 = new Department("研发部");
OrganizationComponent department2 = new Department("财务部");
OrganizationComponent groupA = new Group("A组");
OrganizationComponent groupB = new Group("B组");
OrganizationComponent groupC = new Group("C组");
OrganizationComponent groupD = new Group("D组");
department1.add(groupA);
department1.add(groupB);
department2.add(groupC);
department2.add(groupD);
company.add(department1);
company.add(department2);
company.print();
}
标签:OrganizationComponent,Java,name,Group,代码,void,new,设计模式,public
From: https://www.cnblogs.com/zwmBlog/p/18084082