设计模式随笔(十一):组合模式

概念:

将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。·

作用

(1)统一了组合对象和叶子对象
(2)简化了客户端调用
(3)易扩展,松耦合,组合对象和叶子对象能结合在一起工作

使用场景

(1) 在具有整体和部分的层次结构中,希望通过一种方式忽略整体与部分的差异,客户端可以一致地对待它们。
(2) 在一个使用面向对象语言开发的系统中需要处理一个树形结构。
(3) 在一个系统中能够分离出叶子对象和容器对象,而且它们的类型不固定,需要增加一些新的类型。

UML图

 ● Component(抽象构件):它可以是接口或抽象类,为叶子构件和容器构件对象声明接口,在该角色中可以包含所有子类共有行为的声明和实现。在抽象构件中定义了访问及管理它的子构件的方法,如增加子构件、删除子构件、获取子构件等。

 ● Leaf(叶子构件):它在组合结构中表示叶子节点对象,叶子节点没有子节点,它实现了在抽象构件中定义的行为。对于那些访问及管理子构件的方法,可以通过异常等方式进行处理。

 ● Composite(容器构件):它在组合结构中表示容器节点对象,容器节点包含子节点,其子节点可以是叶子节点,也可以是容器节点,它提供一个集合用于存储子节点,实现了在抽象构件中定义的行为,包括那些访问及管理子构件的方法,在其业务方法中可以递归调用其子节点的业务方法。

 java代码摘自:https://blog.csdn.net/jiangtianjiao/article/details/87977312

Component

public abstract class Component {
    //增加成员
    public void add(Component c) {
        throw new UnsupportedOperationException("对象不支持此功能");
    }

    //删除成员
    public void remove(Component c) {
        throw new UnsupportedOperationException("对象不支持此功能");
    }

    //获取成员
    public Component getChild(int i) {
        throw new UnsupportedOperationException("对象不支持此功能");
    }

    public abstract void operation(String preStr);  //业务方法

}

Composite

@Data
@NoArgsConstructor
public class Composite extends Component{

    private String name;

    private List<Component> child = null;

    public Composite(String name) {
        this.name = name;
    }

    @Override
    public void operation(String preStr) {
        System.out.println("输出自己:" + name + "-" + preStr);
        // 如果有子节点输出子节点
        if (child != null) {
            preStr += " ";
            for (Component component : child) {
                component.operation(preStr);
            }
        }
    }

    @Override
    public void add(Component c) {
        if (child == null) {
            child = new ArrayList<>();
        }
        child.add(c);
    }

    @Override
    public void remove(Component c) {
        if (child != null) {
            child.remove(c);
        }
    }

    @Override
    public Component getChild(int i) {
        if (i >= 0 && child != null && i < child.size()) {
            return child.get(i);
        }
        return null;
    }
}

Leaf

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Leaf extends Component{

    private String name;

    @Override
    public void operation(String preStr) {
        // 具体业务
        System.out.println(preStr + "-" + name);
    }
}

client调用

public class CombinationClient {
    public static void main(String[] args) {
        // 定义多个Composite组合对象
        Component root = new Composite("服装");
        Component c1 = new Composite("男装");
        Component c2 = new Composite("女装");
        Component c3 = new Composite("母婴");

        // 定义多个Leaf叶子对象
        Component leaf1 = new Leaf("西服");
        Component leaf2 = new Leaf("夹克");
        Component leaf3 = new Leaf("衬衫");
        Component leaf4 = new Leaf("裙子");
        Component leaf5 = new Leaf("套装");
        Component leaf6 = new Leaf("鞋袜");
        Component leaf7 = new Leaf("孕妇装");
        Component leaf8 = new Leaf("婴儿装");

        // 组合成为树形的对象结构
        root.add(c1);
        root.add(c2);
        root.add(leaf6);
        c1.add(leaf1);
        c1.add(leaf2);
        c1.add(leaf3);
        c2.add(leaf4);
        c2.add(leaf5);
        c2.add(c3);
        c3.add(leaf7);
        c3.add(leaf8);

        // 调用根对象的输出功能输出整棵树
        root.operation("");
    }
}
原文地址:https://www.cnblogs.com/chylcblog/p/14281182.html