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())
相关推荐
不像程序员的程序媛7 分钟前
Nginx日志切分
服务器·前端·nginx
Daniel李华16 分钟前
echarts使用案例
android·javascript·echarts
北原_春希16 分钟前
如何在Vue3项目中引入并使用Echarts图表
前端·javascript·echarts
JY-HPS16 分钟前
echarts天气折线图
javascript·vue.js·echarts
尽意啊18 分钟前
echarts树图动态添加子节点
前端·javascript·echarts
吃面必吃蒜18 分钟前
echarts 极坐标柱状图 如何定义柱子颜色
前端·javascript·echarts
O_oStayPositive19 分钟前
Vue3使用ECharts
前端·javascript·echarts
竹秋…19 分钟前
echarts自定义tooltip中的内容
前端·javascript·echarts
宝贝露.19 分钟前
Axure引入Echarts图无法正常显示问题
前端·javascript·echarts
shmily麻瓜小菜鸡20 分钟前
前端文字转语音
前端