js设计模式:桥接模式

作用:

可以将复杂的类进行一些拆分,让抽象和实现进行分离解耦,可以让每一个部分都可以单独维护

方便扩展和维护

示例:

javascript 复制代码
        class Obj {
            constructor(person) {
                this.person = person
                this.name = person.name
            }
            getHobby(){
                return this.person.hobby
            }
            getBehavior(){
                return this.person.behavior()
            }
        }

        class Human {
            constructor(hobby) {
                this.name = '打工人'
                this.hobby = hobby
            }
            behavior() {
                console.log('一身打工人的怨气')
            }
        }
        class Deity {
            constructor(hobby) {
                this.name = '神仙'
                this.hobby = hobby
            }
            behavior() {
                console.log('过着神仙般的生活')
            }
        }

        const wjt = new Obj(new Human('抖音刷美女,或者打游戏'))
        const sunwukong = new Obj(new Deity('定身七仙女,然后吃桃子'))

        console.log(wjt.name+'的爱好:'+wjt.getHobby())
        console.log(sunwukong.name+'的爱好:'+sunwukong.getHobby())
相关推荐
喝拿铁写前端1 天前
前端开发者使用 AI 的能力层级——从表面使用到工程化能力的真正分水岭
前端·人工智能·程序员
wuhen_n1 天前
LeetCode -- 15. 三数之和(中等)
前端·javascript·算法·leetcode
七月shi人1 天前
AI浪潮下,前端路在何方
前端·人工智能·ai编程
非凡ghost1 天前
MusicPlayer2(本地音乐播放器)
前端·windows·学习·软件需求
脾气有点小暴1 天前
scroll-view分页加载
前端·javascript·uni-app
beckyye1 天前
ant design vue Table根据数据合并单元格
前端·antd
布列瑟农的星空1 天前
还在手动翻译国际化词条?AST解析+AI翻译实现一键替换
前端·后端·ai编程
阿闽ooo1 天前
外观模式:从家庭电源控制看“简化接口“的设计智慧
c++·设计模式·外观模式
土豆12501 天前
Rust 错误处理完全指南:从入门到精通
前端·rust·编程语言