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

相关推荐
上海知从科技11 小时前
SENT传输协议:汽车传感器数字化通信的最优解决方案
科技·安全·汽车·软件工程·汽车电子
CETOL公差分析1 天前
CETOL 6σ 在汽车工程领域的深度应用:从底盘到三电系统的公差优化实践
汽车·软件工程
琢磨先生David1 天前
未来十年软件工程专业就业前景
软件工程
Terrence Shen2 天前
关于传统软件工程后端技术和当代AI智能体agent构建的harness engineering的一点思考
人工智能·软件工程
Soari2 天前
告别玩具级 Demo!深度拆解 agents-towards-production,用硬核工程把 AI Agent 推向工业级生产线
人工智能·软件工程·llmops·架构优化·genai·aiagent·生产级部署
大迪deblog2 天前
系统架构设计-关键路径真题详解
软件工程
Soari2 天前
AI 编码智能体的“安全防弹衣”:深度拆解 agent-skills,构建百分百受信任的专业技能注册表
人工智能·网络安全·github·软件工程·aiagent·claudecode·agent-skills
Dola_Zou3 天前
医疗装备的出海安全底座
安全·自动化·软件工程·软件加密
启航挨踢3 天前
软考备考资料分享
软件工程
一路往蓝-Anbo4 天前
第四章:手撕协议栈 —— 缓冲区与结构体数据的 Mock 技巧
网络·stm32·单片机·嵌入式硬件·软件工程·tdd