用23种设计模式打造一个cocos creator的游戏框架----(三)外观模式模式

1、模式标准

模式名称:外观模式

模式分类:结构型

模式意图:为一组复杂的子系统提供了一个统一的简单接口。这个统一接口位于所有子系统之上,使用户可以更方便地使用整个系统。

结构图:

适用于:

  1. 当你想为复杂子系统提供一个简单接口时。外观模式可以定义一个统一的接口,将复杂的子系统调用封装起来,使得客户端只需要调用简单的接口即可完成复杂操作。
  2. 当客户端和抽象的实现类过于紧密耦合,或者子系统之间的依赖关系复杂难以理解时。通过引入外观类,可以解决这些问题,降低客户端与子系统的耦合,提高系统的可维护性。

2、分析与设计

其实就是给我们的游戏框架设计一个主要的入口,比如我设计的游戏框架名叫xhgame(小何游戏),我就以这个xhgame作为这个外观。大部分功能都可以在xhgame这个入口进行完成。

意图:为一组游戏内的子系统提供了一个统一的框架入口xhgame。这个xhgame接口位于所有子系统之上,使用户可以更方便地使用整个系统。

3、开始打造

TypeScript 复制代码
export class xhgame {
    // 设计模式10(外观模式)
    /** 当前游戏 */
    static get game() {
        return gameInstance.game
    };
    /** 当前游戏用到的enums */
    static get enums() {
        return gameInstance.game.getEnums()
    };
    /** 当前游戏用到的uiid */
    static get uiid() {
        return gameInstance.game.getEnums().UUID
    };
    /** 当前游戏用到的接口 */
    static get api() {
        return gameInstance.game.getEnums().API
    };
    /** 当前游戏用到的prefab */
    static get prefab() {
        return gameInstance.game.getEnums().PREFAB
    };
    /** 当前游戏用到的音效 */
    static get bgm() {
        return gameInstance.game.getEnums().BGM
    };
    /** 当前游戏用到的事件 */
    static get eventType() {
        return gameInstance.game.getEnums().eventType
    };
    /** 当前游戏用到的网络通讯 */
    static get net() {
        return gameInstance.game.getNet()
    };
    /** 当前游戏用到的视图绑定 */
    static get vm() {
        return gameInstance.game.getVM().getVMs()
    }
    /** 单位构建管理(含单位特有属性) */
    static get itemFactory() {
        return gameInstance.game.getItemFactory()
    }
    static get camera() {
        return gameInstance.camera
    }
    static get config() {
        return gameInstance.game.config
    }
    /** 本地存储 */
    static get storage() {
        return gameInstance.storage
    }
    /** 事件 */
    static get eventBus() {
        return gameInstance.eventBus
    }
    /** 游戏时间管理 */
    static get timer() {
        return gameInstance.timer
    }
    /** nodes管理 */
    static nodes: NodesManager = null;
    /** ui界面管理 */
    static gui: GuiManager = null;
    /** 游戏音乐管理 */
    static audio: AudioManager = null;
}

4、开始使用

TypeScript 复制代码
export class DemoGame {

  start(){
        xhgame.net.post(url)
        xhgame.timer.start()
        xhgame.storage.set('token','abc')
        xhgame.game.playerEntity.joinRoom()
        await xhgame.gui.openUIAsync(xhgame.uiid.Battle_Index)
  }


}

现在可以快速方便的使用各个子系统了,客户端只需要调用简单的接口,就可以直达子系统内的方法。

相关推荐
xiezhr31 分钟前
米哈游36岁程序员被曝复工当晚猝死出租屋内
游戏·程序员·游戏开发
_哆啦A梦18 小时前
Vibe Coding 全栈专业名词清单|设计模式·基础篇(创建型+结构型核心名词)
前端·设计模式·vibecoding
爱搞虚幻的阿恺4 天前
Niagara粒子系统-超炫酷的闪电特效(加餐 纸牌螺旋上升效果)
游戏·游戏引擎
智算菩萨4 天前
儿童游乐空间的双维建构:室内淘气堡与室外亲子乐园的发展学理、功能分野与协同育人机制研究
游戏·游戏策划
marteker4 天前
房地产市场平台Zillow与《魔兽世界》合作展示游戏内房屋
游戏
阿闽ooo4 天前
中介者模式打造多人聊天室系统
c++·设计模式·中介者模式
小米4964 天前
js设计模式 --- 工厂模式
设计模式
wanhengidc4 天前
云手机 打造云端算力
运维·服务器·网络·游戏·智能手机
逆境不可逃4 天前
【从零入门23种设计模式08】结构型之组合模式(含电商业务场景)
线性代数·算法·设计模式·职场和发展·矩阵·组合模式
驴儿响叮当20104 天前
设计模式之状态模式
设计模式·状态模式