• 结构型设计模式——组合模式


    摘要

    组合模式(composite pattern): 允许你将对象组合成树形结构来表现"整体/部分"层次结构. 组合能让客户以一致的方式处理个别对象以及对象组合。

    一、组合模式的意图

    将对象组合成树形结构来表示“整体/部分”层次关系,允许用户以相同的方式处理单独对象和组合对象。

    二、组合模式的类图

    组件(Component)类是组合类(Composite)和叶子类(Leaf)的父类,可以把组合类看成是树的中间节点。组合对象拥有一个或者多个组件对象,因此组合对象的操作可以委托给组件对象去处理,而组件对象可以是另一个组合对象或者叶子对象。

    三、组合模式的实现

    1. public abstract class Component {
    2. protected String name;
    3. public Component(String name) {
    4. this.name = name;
    5. }
    6. public void print() {
    7. print(0);
    8. }
    9. abstract void print(int level);
    10. abstract public void add(Component component);
    11. abstract public void remove(Component component);
    12. }
    1. public class Composite extends Component {
    2. private List child;
    3. public Composite(String name) {
    4. super(name);
    5. child = new ArrayList<>();
    6. }
    7. @Override
    8. void print(int level) {
    9. for (int i = 0; i < level; i++) {
    10. System.out.print("--");
    11. }
    12. System.out.println("Composite:" + name);
    13. for (Component component : child) {
    14. component.print(level + 1);
    15. }
    16. }
    17. @Override
    18. public void add(Component component) {
    19. child.add(component);
    20. }
    21. @Override
    22. public void remove(Component component) {
    23. child.remove(component);
    24. }
    25. }
    1. public class Leaf extends Component {
    2. public Leaf(String name) {
    3. super(name);
    4. }
    5. @Override
    6. void print(int level) {
    7. for (int i = 0; i < level; i++) {
    8. System.out.print("--");
    9. }
    10. System.out.println("left:" + name);
    11. }
    12. @Override
    13. public void add(Component component) {
    14. throw new UnsupportedOperationException(); // 牺牲透明性换取单一职责原则,这样就不用考虑是叶子节点还是组合节点
    15. }
    16. @Override
    17. public void remove(Component component) {
    18. throw new UnsupportedOperationException();
    19. }
    20. }
    1. public class Client {
    2. public static void main(String[] args) {
    3. Composite root = new Composite("root");
    4. Component node1 = new Leaf("1");
    5. Component node2 = new Composite("2");
    6. Component node3 = new Leaf("3");
    7. root.add(node1);
    8. root.add(node2);
    9. root.add(node3);
    10. Component node21 = new Leaf("21");
    11. Component node22 = new Composite("22");
    12. node2.add(node21);
    13. node2.add(node22);
    14. Component node221 = new Leaf("221");
    15. node22.add(node221);
    16. root.print();
    17. }
    18. }

    四、组合模式的总结

    组合模式源码使用

    • javax.swing.JComponent#add(Component)
    • java.awt.Container#add(Component)
    • java.util.Map#putAll(Map)
    • java.util.List#addAll(Collection)
    • java.util.Set#addAll(Collection)

    博文参考

  • 相关阅读:
    CPU的亲缘性affinity
    无状态自动配置 DHCPv6无状态配置 DHCPv6有状态配置
    一起Talk Android吧(第四百一十二回:Math类常用方法介绍)
    MySQL开窗函数
    (imdb数据集)电影评论分类实战:二分类问题
    绘制X-Bar-S和X-Bar-R图,监测过程,计算CPK过程能力指数
    刷题记录(NC16664 [NOIP2004]合唱队形,NC235954 滑雪,NC235948 最大子串和,NC235624 牛可乐和最长公共子序列)
    ES6汇总
    android 通过adb shell 命令获取最大打开文件数和当前打开文件数及串口使用率
    含文档+PPT+源码等]精品spring boot+MySQL学生在线考试系统vue[包运行成功]计算机Java毕业设计SSM项目源码
  • 原文地址:https://blog.csdn.net/weixin_41605937/article/details/133337738