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"关系。

相关推荐
paceboy7 小时前
Claude解决不了的问题
程序人生·软件工程
向上的车轮1 天前
AI驱动开发:Gemini 3.0如何重塑软件工程范式
人工智能·软件工程·gemini 3.0
EmmaXLZHONG2 天前
Notes of Software Engineering Ninth Edition(软件工程第九版笔记)
笔记·软件工程
行业探路者2 天前
网站二维码的全解析与使用技巧分享
大数据·人工智能·学习·产品运营·软件工程
雾江流3 天前
Deer.God 0.1.3 | 一款XP模块,可解锁数十款应用会员,弹窗捕捉,附FPA教程
软件工程
charlie1145141913 天前
AVX 指令集系列深度介绍:领域、意义、以及 AVX AVX2 的基本用法与样例
开发语言·c++·人工智能·软件工程·并行计算·avx
charlie1145141913 天前
如何把 Win32 窗口“置顶”(Windows + C++)
开发语言·c++·windows·笔记·学习·软件工程
雾江流3 天前
肉包 1.4.0 | 豆包AI手机平替,开源免费,AI自动化
运维·人工智能·自动化·软件工程
2501_916589453 天前
永久免费的win系统垃圾清理软件 释放磁盘空间
电脑·软件工程·开源软件
数学小牛马4 天前
现代软件工程课程 个人博客第三次作业
软件工程