九折技术 九折技术
首页
  • Go
  • MIT-6824
  • 算法与数据结构
  • 面向对象
  • 代码整洁
  • 重构
  • 设计模式
  • 学习
  • 技术
  • 人文
关于
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub

HoldDie

长期有耐心,一切才刚刚开始!
首页
  • Go
  • MIT-6824
  • 算法与数据结构
  • 面向对象
  • 代码整洁
  • 重构
  • 设计模式
  • 学习
  • 技术
  • 人文
关于
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub
  • 代码整洁

  • 重构

  • 设计模式

    • 创建型-单例模式
    • 创建型-工厂模式
    • 创建型-建造者模式
    • 创建型-原型模式
    • 结构型-代理模式
    • 结构型-桥接模式
    • 结构型-装饰器模式
    • 结构型-适配器模式
    • 结构型-门面模式
    • 结构型-组合模式
      • 结构型-享元模式
      • 行为型-观察者模式
      • 行为型-模板模式
      • 行为型-策略模式
      • 行为型-责任链模式
      • 行为型-状态模式
      • 行为型-迭代器模式
      • 行为型-访问者模式
      • 行为型-备忘录模式
      • 行为型-命令模式
      • 行为型-解释器模式
      • 行为型-中介模式
    • 架构
    • 设计模式
    holddie
    2020-08-31

    结构型-组合模式

    # 什么是组合模式

    • Composite Design Pattern
    • 主要处理树形结构数据,可以理解为一组对象集合。
    • 将一组对象组织成树形结构,以表示“部分”、“整体”的层次结构。
    • 组合让客户端可以统一单一对象和组合对象的处理逻辑。

    # 组合模式的应用场景

    • 部门和员工关系
    • Linux 系统中的文件系统

    # 组合模式条件

    • 组合模式,将一组对象组织成树形结构,将单个对象和组合对象都看作树中的节点,以统一处理逻辑,并且它利用树形结构的特点,递归处理每个子树,一次简化代码实现。
    • 使用组合模式的前提是,你的业务场景必须能够表示成树形结构。

    # 代码实现

    public interface IEmployee {
    
      public void add(IEmployee employee);
    
      public void reemove(IEmployee employee);
    
      public IEmployee getChild(int i);
    
      public String getName();
    
      public double getSalary();
    
      public void print();
    
    }
    
    {
    
      private String name;
      private double salary;
      private List<IEmployee> employees = new ArrayList<>();
    
      public Manager(String name, double salary) {
        this.name = name;
        this.salary = salary;
      }
    
      @Override
      public void add(IEmployee employee) {
        employees.add(employee);
      }
    
      @Override
      public void reemove(IEmployee employee) {
        employees.remove(employee);
      }
    
      @Override
      public IEmployee getChild(int i) {
        return employees.get(i);
      }
    
      @Override
      public String getName() {
        return this.name;
      }
    
      @Override
      public double getSalary() {
        return this.salary;
      }
    
      @Override
      public void print() {
        System.out.println("--------------------");
        System.out.println("name: " + name);
        System.out.println("Salary: " + salary);
        System.out.println("--------------------");
        for (IEmployee ep :
             employees) {
          ep.print();
        }
      }
    }
    
    public class Developer implements IEmployee {
      private String name;
      private double salary;
    
      public Developer(String name, double salary) {
        this.name = name;
        this.salary = salary;
      }
    
      @Override
      public void add(IEmployee employee) {
        String msg = "not supported by leaf node";
        throw new UnsupportedOperationException(msg);
      }
    
      @Override
      public void reemove(IEmployee employee) {
        String msg = "not supported by leaf node";
        throw new UnsupportedOperationException(msg);
      }
    
      @Override
      public IEmployee getChild(int i) {
        String msg = "not supported by leaf node";
        throw new UnsupportedOperationException(msg);
      }
    
      @Override
      public String getName() {
        return this.name;
      }
    
      @Override
      public double getSalary() {
        return this.salary;
      }
    
      @Override
      public void print() {
        System.out.println("--------------------");
        System.out.println("name: " + name);
        System.out.println("Salary: " + salary);
        System.out.println("--------------------");
      }
    }
    
    public static void main(String[] args) {
      System.out.println("Hello World!");
    
      Developer developer1 = new Developer("001", 011);
      Developer developer2 = new Developer("002", 012);
      Developer developer3 = new Developer("003", 013);
    
      Manager manager = new Manager("000", 1000);
      manager.add(developer1);
      manager.add(developer2);
      manager.add(developer3);
    
      manager.print();
    
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    编辑
    #组合模式
    上次更新: 2020/09/08, 15:09:00
    结构型-门面模式
    结构型-享元模式

    ← 结构型-门面模式 结构型-享元模式→

    最近更新
    01
    行为型-访问者模式
    11-24
    02
    行为型-备忘录模式
    11-24
    03
    行为型-命令模式
    11-24
    更多文章>
    Theme by Vdoing | Copyright © 2019-2020 HoldDie | MIT License
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式