设计模式(十一)之组合模式

文章目录

  • 什么是组合模式
  • 例子
  • 总结

什么是组合模式

组合模式是一种结构型设计模式,它旨在将容器和内容作为同一种东西看待即使容器与内容具有一致性。在容器中既可以放入内容,也可以放入容器,形成递归结构。

例子

构建公司的组织架构树

Node节点

package StructuralPattern.CompositeMode;

import java.util.List;

/**
 * Node节点
 */
public interface Node {
    /**
     * 增加一个子节点
     * @param node
     * @return
     */
    Node add(Node node);

    /**
     * 获取所有子节点
     * @return
     */
    List<Node> listChildren();

    /**
     * 打印结构
     * @return
     */
    String print();
}

Organization
具有内容name以及容器list

package StructuralPattern.CompositeMode;

import java.util.ArrayList;
import java.util.List;

/**
 * 组织
 */

public class Organization implements Node {
    private String name;

    private List<Node> list;
	
	/**
	* 层次
	*/
    private int floor;

    public Organization(String name,int floor) {
        list = new ArrayList<>();
        this.name = name;
        this.floor = floor;
    }

    @Override
    public Node add(Node node) {
        list.add(node);
        return this;
    }

    @Override
    public List<Node> listChildren() {
        return list;
    }

    @Override
    public String print() {
        StringBuilder res = new StringBuilder();
        res.append("    ".repeat(Math.max(0, floor)));
        res.append("organization: ").append(name).append('\n');
        list.forEach(l->{
            res.append(l.print());
        });
        return res.toString();
    }
}

Department

package StructuralPattern.CompositeMode;

import java.util.ArrayList;
import java.util.List;

/**
 * 部门
 */

public class Department implements Node{
    private String name;

    private List<Node> list;

    /**
     * 层次
     */
    private int floor;

    public Department(String name,int floor){
        list = new ArrayList<>();
        this.name = name;
        this.floor = floor;
    }

    @Override
    public Node add(Node node) {
        list.add(node);
        return this;
    }

    @Override
    public List<Node> listChildren() {
        return list;
    }

    @Override
    public String print() {
        StringBuilder res = new StringBuilder();
        res.append("    ".repeat(Math.max(0, floor)));
        res.append("department: ").append(name).append('\n');
        list.forEach(l->{
            res.append(l.print());
        });
        return res.toString();
    }
}

Employee

package StructuralPattern.CompositeMode;

import java.util.ArrayList;
import java.util.List;

/**
 * 员工
 */

public class Employee implements Node{
    private String name;

    private List<Node> list;

    /**
     * 层次
     */
    private int floor;

    public Employee(String name,int floor){
        list = new ArrayList<>();
        this.name = name;
        this.floor = floor;
    }

    @Override
    public Node add(Node node) {
        throw new UnsupportedOperationException();
    }

    @Override
    public List<Node> listChildren() {
        return list;
    }

    @Override
    public String print() {
        StringBuilder res = new StringBuilder();
        res.append("    ".repeat(Math.max(0, floor)));
        res.append("Employee: ").append(name).append('\n');
        return res.toString();
    }
}

Main

package StructuralPattern.CompositeMode;

/**
 * Main
 */

public class Main {
    public static void main(String[] args) {
        Node company = new Organization("xxx有限公司",0);

        Department depart1 = new Department("信息部",1);
        Department depart2 = new Department("研发部",1);
        Department depart3 = new Department("软件开发部",2);

        depart3.add(new Employee("five",3))
                .add(new Employee("six",3));
        depart2.add(new Employee("four",2)).add(depart3);
        depart1.add(new Employee("one",1))
                .add(new Employee("two",1))
                .add(new Employee("three",1));
        company.add(depart1).add(depart2);
        System.out.println(company.print());
    }
}

结果
设计模式(十一)之组合模式_第1张图片

总结

Composite模式使得内容对象和容器对象具有一致性,从而形成统一的树形结构,并用一致的方式去处理它们。

你可能感兴趣的:(设计模式,组合模式,设计模式)