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

相关推荐
梁辰兴8 小时前
软件工程:软件维护的副作用
数据库·软件工程·梁辰兴·控制方法·软件维护的副作用·副作用类型·副作用原因
盖伦发发14 小时前
业务开发常用设计模式:状态、策略、工厂、适配器、模板方法
后端·软件工程
梁辰兴15 小时前
软件工程:逆向工程
软件工程·逆向工程·应用场景·梁辰兴·抽象层次·工程过程·方法工具
記億揺晃着的那天15 小时前
Amazon Ads API 实战:如何高精度关联广告活动(Campaign)与 ASIN 及 ERP 产品主数据
软件工程·amazon·架构设计·系统设计·亚马逊·sp-api·amazon ads api
盖伦发发16 小时前
Redis 核心教学: 数据结构, 缓存设计, 分布式锁
java·redis·后端·软件工程
hans汉斯17 小时前
数据挖掘|基于BP神经网络的少数民族村寨文化型旅游体验产品潜在游客挖掘
深度学习·神经网络·算法·yolo·软件工程·bp·汉斯出版社
摸鱼仙人~2 天前
Vue 应用完整启动链路
前端·vue.js·软件工程
郝学胜-神的一滴2 天前
C++20模板元编程 02:吃透模板基础核心四大核心知识点
开发语言·数据结构·c++·程序人生·软件工程·visual studio
架构谨制@涛哥2 天前
知识工程-4.超越RAG:OKF会如何取代矢量数据库吗?
人工智能·软件工程·软件构建·知识图谱
nagualky1232 天前
AI Agent上线后怎么升级?先建立一套可回滚的变更控制
人工智能·机器学习·语言模型·软件工程