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

1、模式标准

模式名称:单例模式

模式分类:创建型

模式意图:保证一个类仅有一个实例,并提供一个访问它的全局访问点。

结构图:

适用于:

  1. 当类只能有一个实例而且这个实例易于通过一个访问点访问时。
  2. 当这个唯一的实例应该是通过子类化可扩展的,并且用户应该无需更改代码就能使用一个扩展的实例时。

2、分析与设计

有一些类会自带单例模式,有些采用统一的单例去管理。因为我们是设计一个游戏框架所以这里采用了统一的方式。方便认识整个系统中那些系统使用了单例。

意图:保证一个全局唯一的单例,并提供一个访问它的全局访问点。

3、开始打造

TypeScript 复制代码
export class SingletonInstance {
    // 设计模式5(单例模式)
    private static _instance: SingletonInstance = new this()
    static get instance(): SingletonInstance {
        return this._instance
    }
    static getInstance() {
        return this._instance
    }
    /** 当前游戏 */
    game: DemoGame
    // game: JCQGame
    // game: FeijianGame
    /** 本地存储 */
    storage: StorageManager = new StorageManager();
    /** 事件管理 */
    eventBus: EventManager = new EventManager()
    /** 游戏时间管理 */
    timer: TimerManager = new TimerManager();
}
export var gameInstance = SingletonInstance.instance;

4、开始使用

TypeScript 复制代码
import { gameInstance } from './SingletonInstance';

const { ccclass, property } = _decorator;
/**
 * 总入口
 */
@ccclass('Main')
export class Main extends Component {

    /** 界面层节点 */
    @property({ type: Node, tooltip: "界面层Node" })
    public gui: Node = null;

    /** 游戏层节点 */
    @property({ type: Node, tooltip: "游戏层Node" })
    public game: Node = null;

    /** 游戏编码 */
    @property({ tooltip: "游戏编码" })
    public gameCode: string = '';


    start() {
        if (DEBUG) profiler.showStats();
    }

    onLoad() {
        window['xhgame'] = xhgame // 方便console中查看全局
        // game.frameRate = 30;
        // 设计模式3(生成器模式)
        const gameDesign = new GameDesign();
        switch (this.gameCode) {
            case 'demo': // demo
                gameDesign.setGameBuilder(new DemoGameBuilder(this));
                gameInstance.game = gameDesign.buildGame<DemoGame>()
                break;
            case 'jianchuqiao': // 剑出鞘
                gameDesign.setGameBuilder(new JianchuqiaoGameBuilder(this));
                gameInstance.game = gameDesign.buildGame<JCQGame>()
                break;
            case 'feijian': // 飞剑
                gameDesign.setGameBuilder(new FeijianGameBuilder(this));
                gameInstance.game = gameDesign.buildGame<FeijianGame>()
                break;
        }
        gameInstance.game.start()
        gameInstance.timer.start()
    }

在全局访问点gameInstance中设置了设置了当前唯一的游戏game:DemoGame,以及系统种需用到的存储单例,时间单例,及事件单例等等

相关推荐
二哈赛车手23 分钟前
新人笔记---多策略搭建策略执行链实现RAG检索后过滤
java·笔记·spring·设计模式·ai·策略模式
Swift社区1 小时前
Store + System:鸿蒙游戏黄金分层
游戏·华为·harmonyos
楼田莉子1 小时前
仿Muduo的高并发服务器:Channel模块与Poller模块
linux·服务器·c++·学习·设计模式
geovindu16 小时前
go: Strategy Pattern
开发语言·设计模式·golang·策略模式
嵌入式学习_force1 天前
02_state
设计模式·蓝牙
星辰徐哥1 天前
Unity基础:游戏对象的激活与隐藏:SetActive方法详解
游戏·unity·lucene
CS创新实验室1 天前
CS实验室行业报告:游戏行业就业分析报告
大数据·游戏
王杨游戏养站系统1 天前
王杨游戏蜘蛛养站系统:提交百度站长工具平台教程!
游戏·百度·游戏下载站养站系统·游戏养站系统
码界筑梦坊1 天前
112-基于Flask的游戏行业销售数据可视化分析系统
开发语言·python·游戏·信息可视化·flask·毕业设计·echarts
maaath1 天前
【maaath】Flutter for OpenHarmony 游戏中心应用实战开发
flutter·游戏·华为·harmonyos