OMT画图的五种结构表达方式

  1. 实例化:A类依赖于B类。

    javascript 复制代码
    class B {
      doSth () {
    
      }
    }
    
    class A {
      constructor () {}
    
      run () {
        const b = new B()
        b.doSth()
      }
    }
    
    new A().run()
  2. 委托:A对象依赖于B对象。

    javascript 复制代码
    class B {
      doSth () {
    
      }
    }
    const b = new B()
    
    class A {
      constructor () {}
    
      run () {
        b.doSth()
      }
    }
    new A().run()
  3. 聚合:A对象和B对象是"has-a"关系。

    javascript 复制代码
    class Student {  
      constructor(name) {  
        this.name = name  
      }
    }
    
    class Classroom {  
      constructor() {  
        this.students = []  
      }
    
      addStudent(student) {  
        this.students.push(student)  
      }  
    }
    
    let student1 = new Student('Alice')  
    let student2 = new Student('Bob')  
    let classroom = new Classroom()  
    classroom.addStudent(student1)  
    classroom.addStudent(student2)
  4. 组合:A对象和B对象是"is-a"关系。

    javascript 复制代码
    class Creature {  
      constructor(name) {  
        this.name = name  
      }  
    }  
    
    class Eye {  
      constructor(color) {  
        this.color = color  
      }  
    }  
    
    class Human extends Creature {  
      constructor() {  
        super('Human')  
        this.leftEye = new Eye('blue')  
        this.rightEye = new Eye('brown')  
      }  
    }
  5. 继承:A类和B类是"is-a"关系。

相关推荐
watersink10 小时前
第20章 沙场春点兵
软件工程
watersink12 小时前
第23章 2014真题作文
软件工程
watersink16 小时前
第27章 2021真题作文
软件工程
bug攻城狮17 小时前
软件系统从零到一的过程:关键环节与产出文档解析
软件工程
watersink17 小时前
第26章 2020真题作文
软件工程
watersink20 小时前
第24章 2015真题作文
软件工程
watersink20 小时前
第28章 2022真题作文
软件工程
workflower21 小时前
State(状态)模式
语言模型·集成测试·软件工程·软件构建·需求分析·软件需求
watersink21 小时前
第25章 2019真题作文
软件工程
郝学胜-神的一滴21 小时前
CMake赋能持续集成|自动化测试落地的进阶指南 ✨
c++·ci/cd·软件工程·软件构建