zoukankan      html  css  js  c++  java
  • 组合模式Component

    简介

      组合模式是一种结构型模式,将对象组合成树形结构以表示“部分-整体”的层次结构。
    组合模式使得用户对单个对象和组合对象的使用具有唯一性。解决对象包含对象的问题,通过组合的方式(在对象内部引用对象)来进行布局,这种组合是区别于继承的,而另一层含义是指树形结构子节点的抽象(将叶子节点与数枝节点抽象为子节点),区别于普通的分别定义叶子节点与数枝节点的方式。

    角色

      Component : 组合中的对象声明接口,在适当的情况下,实现所有类共有接口的默认行为。声明一个接口用于访问和管理 Component 的子部件。
      Leaf : 表示叶节点对象。叶子节点没有子节点。
      Composite : 定义枝节点行为,用来存储子部件,在 Component 接口中实现与子部件相关的操作。例如 Add 和 Remove。

    类图

     

    示例

    接口

    public abstract class Component {
        protected String name;
    
        public Component(String name) {
            this.name = name;
        }
    
        public abstract void add(Component component);
    
        public abstract void remove(Component component);
    
        public abstract void display();
    }

    叶节点

    public class Leaf extends Component {
        public Leaf(String name) {
            super(name);
        }
    
        public void add(Component component) {
            System.out.println("叶子节点不能添加");
        }
    
        public void remove(Component component) {
            System.out.println("叶子节点不能删除");
        }
    
        public void display() {
            System.out.println(name);
        }
    }

    枝节点

    public class Composite extends Component {
        public Composite(String name) {
            super(name);
        }
    
        private List<Component> children = new ArrayList<Component>();
    
        public void add(Component component) {
            children.add(component);
        }
    
        public void remove(Component component) {
            children.remove(component);
        }
    
        public void display() {
            System.out.println(name);
            for (Component child : children) {
                child.display();
            }
        }
    
    }

    客户端

    public class Client {
        public static void main(String[] args) {
            Component root = new Composite("root");
            root.add(new Leaf("Leaf A"));
            root.add(new Leaf("Leaf B"));
    
            Component compX = new Composite("Composite X");
            compX.add(new Leaf("Leaf XA"));
            compX.add(new Leaf("Leaf XB"));
            root.add(compX);
    
            Component compXY = new Composite("Composite XY");
            compXY.add(new Leaf("Leaf XYA"));
            compXY.add(new Leaf("Leaf XYB"));
            compX.add(compXY);
    
            root.display();
        }
    }

    测试

    root
    Leaf A
    Leaf B
    Composite X
    Leaf XA
    Leaf XB
    Composite XY
    Leaf XYA
    Leaf XYB
    View Code

    应用场景

    • 表示对象的部分-整体层次结构。
    • 客户端忽略组合对象与单个对象的差异,客户端将统一地使用组合结构中的所有对象。

    分级数据结构的一个普遍性的例子是你每次使用电脑时所遇到的:文件系统

    Head First 设计模式(中文版)的示例:

    码云地址:hxxps://gitee.com/manusas/CompositeDP

  • 相关阅读:
    2019 ECfianl
    Codeforces Round #610 (Div. 2)
    IOS设计模式之三:MVC模式
    MVC3快速搭建Web应用(二)
    IOS设计模式之四:观察者模式
    MVC3快速搭建Web应用(一)
    Three20 NetWork
    IOS设计模式之一:单例模式
    IOS设计模式之二:Delegate模式
    写博客的意义
  • 原文地址:https://www.cnblogs.com/manusas/p/8482052.html
Copyright © 2011-2022 走看看