用23种设计模式打造一个cocos creator的游戏框架----(二十一)组合模式

1、模式标准

模式名称:组合模式

模式分类:结构型

模式意图:将对象组合成树型结构以表示"部分-整体"的层次结构。Composite 使得用户对单个对象和组合对象的使用具有一致性。

结构图:

适用于:

1、想表示对象的部分-整体层次结构。

2、希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。

2、分析与设计

组合模式可以与命令模式结合使用,实现单命令,命令组,命令组内套命令组的功能。这样多一些常用的命令组,只需一个命令表达式就搞定了。

意图:将对象(命令)组合成树型结构以表示"部分-整体"的层次结构。

3、开始打造

命令类接口

TypeScript 复制代码
// 命令类接口
export interface ICommand {
    execute(): void;
}

命令组

TypeScript 复制代码
// 命令组
export class GroupCommand implements ICommand {
    private name: string;
    private commands: ICommand[] = [];
    constructor(name: string) {
        this.name = name;
    }
    add(command: ICommand): void {
        this.commands.push(command);
    }
    remove(command: ICommand): void {
        const index = this.commands.indexOf(command);
        if (index !== -1) {
            this.commands.splice(index, 1);
        }
    }
    getChild(index: number): ICommand {
        return this.commands[index]
    }
    execute(): void {
        console.log(`Executing command group: ${this.name}`);
        // 执行所有子命令
        for (const command of this.commands) {
            command.execute();
        }
    }
}

单命令

TypeScript 复制代码
// 具体技能命令类 - 小技能
export class SmallSkillCommand implements ICommand {
    execute(): void {
        console.log("释放小技能");
    }
}

// 具体技能命令类 - 中技能
export class MediumSkillCommand implements ICommand {
    execute(): void {
        console.log("释放中技能");
    }
}

// 具体技能命令类 - 大技能
export class LargeSkillCommand implements ICommand {
    execute(): void {
        console.log("释放大技能");
    }
}

4、开始使用

修改上次用过的解析器模式的上下文,新增commandId == 'attackgroup'

TypeScript 复制代码
// 单位 操作命令 另一个单位
export class UnitCommandUnitContext {
    command: ICommand = null
    fromUnitItem: UnitItem<any> = null
    toUnitItem: UnitItem<any> = null
    getUnitItem(unitItemId: string) {
        return xhgame.game.battleEntity.model.unitItemMap.get(unitItemId)
    }
    getCommand(commandId: string) {
        if (commandId == 'attackgroup') {
            const command1 = new SmallSkillCommand();
            const command2 = new MediumSkillCommand();
            const command3 = new LargeSkillCommand();

            // 先生成一个子命令组
            const commandGroup1 = new GroupCommand("commandGroup1");
            commandGroup1.add(command1);
            commandGroup1.add(command2);

            // 再生成一个含子命令组及多个单命令的命令组
            const commandGroup2 = new GroupCommand("commandGroup2");
            commandGroup2.add(commandGroup1);
            commandGroup2.add(command1);
            commandGroup2.add(command2);
            commandGroup2.add(command3);
            return commandGroup2
        }
        return new AttackCommand(null, null)
    }
    setCommand(command: ICommand) {
        this.command = command
    }
    setUnitItem(unitItem: UnitItem<any>) {
        if (this.fromUnitItem == null) {
            this.fromUnitItem = unitItem
        }
        if (this.toUnitItem == null) {
            this.toUnitItem = unitItem
        }
    }
    executeCommand() {
        if (this.command instanceof AttackCommand) {
            this.command.setUnitItem(this.fromUnitItem)
            this.command.setTargetUnitItem(this.toUnitItem)
            this.command.execute()
        } else {
            this.command.execute()
        }
    }
}

修改表达式

TypeScript 复制代码
......
// 修改表达式,现在一个表达式,实际干了n多个命令
const commandText = "[[UnitItem.20]]{{attackgroup}}[[UnitItem.21]]";

......

效果

相关推荐
刀法如飞3 小时前
AI时代,程序员都应该是算法思想工程师
人工智能·设计模式·程序员
在西安放羊的牛油果9 小时前
我把 2000 行下单代码,重构成了一套交易前端架构
前端·设计模式·架构
寅时码1 天前
React 正在演变为一场不可逆的赛博瘟疫:AI 投毒、编译器迷信与装死的官方
前端·react.js·设计模式
willow4 天前
Axios由浅入深
设计模式·axios
七月丶6 天前
别再手动凑 PR 了:这个 AI Skill 会按仓库习惯自动建分支、拆提交、提 PR
人工智能·设计模式·程序员
刀法如飞6 天前
从程序员到架构师:6大编程范式全解析与实践对比
设计模式·系统架构·编程范式
九狼6 天前
Flutter + Riverpod +MVI 架构下的现代状态管理
设计模式
静水流深_沧海一粟7 天前
04 | 别再写几十个参数的构造函数了——建造者模式
设计模式
StarkCoder7 天前
从UIKit到SwiftUI的迁移感悟:数据驱动的革命
设计模式