JS设计模式初识(十一)-中介者模式

本文深入探讨了中介者模式,一种用于降低对象间耦合度的设计模式。通过将对象间的复杂交互封装在一个中介者对象中,简化了对象之间的通信,使系统更加灵活。文章通过一个小游戏的实现案例,展示了中介者模式的具体应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定义

中介者模式的作用就是解除对象与对象之间的紧耦合关系。增加一个中介者对象后,所有的 相关对象都通过中介者对象来通信,而不是互相引用,所以当一个对象发生改变时,只需要通知 中介者对象即可。中介者使各对象之间耦合松散,而且可以独立地改变它们之间的交互。中介者模式使网状的多对多关系变成了相对简单的一对多关系

11.1 基本知识

当不使用中介者模式的时候,每个人每个物体之间都会产生一些错综复杂的联系 , 如果要修改中间某一个对象都得小心翼翼, 因为会牵一发而动全身。如下图11-1:

如下图11-2使用中介者模式后:在图11-1中如果对象A发生了改变,则需要同时通知跟A发生引用关系的 B、D、E、F 这 4 个对象;而在图14-2中,使用中介者模式改进之后,A 发生改变时则只需要通知这个中介者 对象即可。

11.2 实现个小游戏

玩家可加入不同颜色的队伍

    // 中介者
    const playerDirector = (function() {
        const players = {}; // all player
        const operations = {}; // 能执行的操作
    
        operations.addPlayer = (player) => {
            const { teamColor, name } = player;
            const colorTeams = players[teamColor] || [];
            colorTeams.push(player);
        }
        operations.removePlayer = (player) => {
            const { teamColor } = player;
            players[teamColor] = colorTeams.filter((item) => item === player);
    
        }
        operations.playerDead = (player) => {
            const { teamColor } = player;
            let colorTeams = players[teamColor];
            const hasAlive = colorTeams.find((item) => item.state === 'alive');
            const allDead = !hasAlive;
    
            if (allDead) {
                colorTeams.forEach((item) => { item.lose() });
                for (let color in players) {
                    if (color !== teamColor) {
                        players[color].forEach((item) => { item.win() });
                    }
                }
            }
        }
        operations.changeTeam = (player, newTeamColor) => {
            const { teamColor } = palyer;
            operations.removePlayer(player);
            palyer.teamColor = newTeamColor;
            operations.addPlayer(palyer)
        }
        return {
            reciveMessage: (message, ...args) => operations[message].apply(this, args),
        }
    })();
    
    // 玩家类
    function Player(name, teamColor) {
        this.name = name;
        this.teamColor = teamColor;
        this.state = 'alive';
    }
    Player.prototype.win = function() {
        console.log(this.name+' won',);
    }
    Player.prototype.lose = function() {
        console.log(this.name+' lose');
    }
    Player.prototype.die = function() {
        this.state = 'dead';
        playerDirector.reciveMessage('playerDead', this);
    }
    Player.prototype.changeTeam = function(newTeamColor) {
        playerDirector.reciveMessage('changeTeam', this, newTeamColor);
    }
    
    // 玩家工厂
    function PlayerFactory(palyer, color) {
        const newPlayer = new Player(player, color);
        playerDirector.reciveMessage('addPlayer', newPlayer);
    }
复制代码
11.3 总结

中介者模式使各个对象之间得以解耦,以中介者和对象之间的一对多关系取代了对象 之间的网状多对多关系。各个对象只需关注自身功能的实现,对象之间的交互关系交给了中介者 对象来实现和维护。 不过,中介者模式也存在一些缺点。其中,最大的缺点是系统中会新增一个中介者对象,因 为对象之间交互的复杂性,转移成了中介者对象的复杂性,使得中介者对象经常是巨大的。中介 者对象自身往往就是一个难以维护的对象。

转载于:https://juejin.im/post/5d050ee4f265da1b802043ae

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值