用23种设计模式打造一个cocos creator的游戏框架----(十四)观察者模式

1、模式标准

模式名称:观察者模式

模式分类:行为型

模式意图:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。

结构图:

适用于:

1、当一个抽象模型有两个方面,其中一个方面依赖于另一个方面,将这两者封装在独立的对象中以使它们可以各自独立地改变和复用。

2、当对一个对象的改变需要同时改变其他对象,而不知道具体有多少对象有待改变时。

3、当一个对象必须通知其他对象,而它又不能假定其他对象是谁,即不希望这些对象是紧耦合的

2、分析与设计

观察者模式的主要使用场景是gui、网络服务器、发布订阅系统。在前面的设计模式中,我们采用了代理模式,通过"代理拦截修改"实现了数据层和视图层之间的响应式。虽然实现了响应式,但是其中的数据不是真实的数据源。真实数据源发生变化时,还需要通过xhgame.vm.gateVM.ps = 123来手动触发修改。我们希望的数据自动监听真实数据源的变化自动触发响应式。下面我们通过观察者模式来实现它,在游戏框架里我们统一使用modelComp中的数据源,先修改一下我们的意图

意图:定义对象(modelComp)间的一种一对多的依赖关系,当一个对象(modelComp)的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。

3、开始打造

主题接口

TypeScript 复制代码
export interface ISubject {
    observers: IObserver[]
    attach(observer: IObserver): void
    detach(observer: IObserver): void
    notify(): void
}

观察者接口

TypeScript 复制代码
export interface IObserver {
    update(subject: ISubject): void
}

这里新增一个modelComp 的抽象类继承之前的ecs中Comp

TypeScript 复制代码
export abstract class ModelComp<T> extends Comp implements ISubject {
    callback: Function = null
    reset(): void {

    }
    onAttach(entity: Entity) {

    }

    onDetach(entity: Entity) {

    }
    // 观察者模式
    observers: IObserver[] = []

    attach(observer: IObserver): void {
        const isExist = this.observers.includes(observer);
        if (isExist) {
            return console.log('已存在监听者');
        }
        this.observers.push(observer);
    }
    detach(observer: IObserver): void {
        const observerIndex = this.observers.indexOf(observer);
        if (observerIndex === -1) {
            return console.log('不存在监听者');
        }
        this.observers.splice(observerIndex, 1);
    }
    notify(): void {
        console.log('ModelSubject notify')
        for (const observer of this.observers) {
            observer.update(this);
        }
    }

}

接着是具体的主题,玩家的modelComp

TypeScript 复制代码
export class PlayerModelComp extends ModelComp<IPlayerInfo_JCQ> {

    callback: Function = null

    _playerInfo: IPlayerInfo_JCQ = {
        id: 0,
        openid: '',
        server_no: '',
        platform: '',
        ps: 0,
        gold: 0,
        diamond: 0,
        last_battle_id: 0
    }
    get playerInfo() {
        return this._playerInfo
    }
    set playerInfo(val) {
        this._playerInfo = val
        this.notify()
    }
   

    reset() {
        this.callback = null
        this._playerInfo = {
            id: 0,
            openid: '',
            server_no: '',
            platform: '',
            ps: 0,
            gold: 0,
            diamond: 0,
            last_battle_id: 0
        }
        // 
        this.observers = []

    }


    onAttach(entity: Entity) {
        this.callback && this.callback()
    }

    onDetach(entity: Entity) {

    }
}

设置一个玩家信息的监听者

TypeScript 复制代码
export class PlayerInfoObserver implements IObserver {
    update(subject: PlayerModelComp): void {
        const playerInfo = subject.playerInfo
        xhgame.vm.gateVM.ps = playerInfo.ps
        xhgame.vm.gateVM.gold = playerInfo.gold
        xhgame.vm.gateVM.diamond = playerInfo.diamond
    }
}

4、开始使用

TypeScript 复制代码
export class JCQPlayerEntity extends Entity {
    model: PlayerModelComp

    init() {
        this.model = this.attachComponent(PlayerModelComp)
    }

}

对playerModelComp添加多个监听者

TypeScript 复制代码
xhgame.game.playerEntity.model.attach(new PlayerInfoObserver())
xhgame.game.playerEntity.model.attach(new OtherObserver())

观察者内原本有自己的state或者info,现在用了vm来代替了

TypeScript 复制代码
export class PlayerInfoObserver implements IObserver {
    update(subject: PlayerModelComp): void {
        const playerInfo = subject.playerInfo
        xhgame.vm.gateVM.ps = playerInfo.ps
        xhgame.vm.gateVM.gold = playerInfo.gold
        xhgame.vm.gateVM.diamond = playerInfo.diamond
    }
}
相关推荐
ujainu15 小时前
Flutter + OpenHarmony 游戏开发进阶:用户输入响应——GestureDetector 实现点击发射
flutter·游戏·openharmony
ujainu15 小时前
Flutter + OpenHarmony 实现无限跑酷游戏开发实战—— 对象池化、性能优化与流畅控制
flutter·游戏·性能优化·openharmony·endless runner
呆呆敲代码的小Y17 小时前
【Unity工具篇】| 超实用工具LuBan,快速上手使用
游戏·unity·游戏引擎·unity插件·luban·免费游戏·游戏配置表
我的offer在哪里17 小时前
用 Unity 从 0 做一个「可以玩的」游戏,需要哪些步骤和流程
游戏·unity·游戏引擎
串流游戏联盟18 小时前
启程!手机也能邂逅暖暖万相奇观
游戏·远程工作
User_芊芊君子18 小时前
HCCL高性能通信库编程指南:构建多卡并行训练系统
人工智能·游戏·ai·agent·测评
J_liaty18 小时前
23种设计模式一代理模式
设计模式·代理模式
前端不太难21 小时前
HarmonyOS 游戏里,Ability 是如何被重建的
游戏·状态模式·harmonyos
灵狐数据FoxData1 天前
QQ农场今日回归,我们想“偷”回的到底是什么?
游戏·社交电子·业界资讯·娱乐·玩游戏
微祎_1 天前
Flutter for OpenHarmony:构建一个 Flutter 平衡球游戏,深入解析动画控制器、实时物理模拟与手势驱动交互
flutter·游戏·交互