当前位置:   article > 正文

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

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

1、模式标准

模式名称:观察者模式

模式分类:行为型

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

结构图:


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

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

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

2、分析与设计 

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

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

3、开始打造

主题接口

  1. export interface ISubject {
  2. observers: IObserver[]
  3. attach(observer: IObserver): void
  4. detach(observer: IObserver): void
  5. notify(): void
  6. }

观察者接口

  1. export interface IObserver {
  2. update(subject: ISubject): void
  3. }

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

  1. export abstract class ModelComp<T> extends Comp implements ISubject {
  2. callback: Function = null
  3. reset(): void {
  4. }
  5. onAttach(entity: Entity) {
  6. }
  7. onDetach(entity: Entity) {
  8. }
  9. // 观察者模式
  10. observers: IObserver[] = []
  11. attach(observer: IObserver): void {
  12. const isExist = this.observers.includes(observer);
  13. if (isExist) {
  14. return console.log('已存在监听者');
  15. }
  16. this.observers.push(observer);
  17. }
  18. detach(observer: IObserver): void {
  19. const observerIndex = this.observers.indexOf(observer);
  20. if (observerIndex === -1) {
  21. return console.log('不存在监听者');
  22. }
  23. this.observers.splice(observerIndex, 1);
  24. }
  25. notify(): void {
  26. console.log('ModelSubject notify')
  27. for (const observer of this.observers) {
  28. observer.update(this);
  29. }
  30. }
  31. }

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

  1. export class PlayerModelComp extends ModelComp<IPlayerInfo_JCQ> {
  2. callback: Function = null
  3. _playerInfo: IPlayerInfo_JCQ = {
  4. id: 0,
  5. openid: '',
  6. server_no: '',
  7. platform: '',
  8. ps: 0,
  9. gold: 0,
  10. diamond: 0,
  11. last_battle_id: 0
  12. }
  13. get playerInfo() {
  14. return this._playerInfo
  15. }
  16. set playerInfo(val) {
  17. this._playerInfo = val
  18. this.notify()
  19. }
  20. reset() {
  21. this.callback = null
  22. this._playerInfo = {
  23. id: 0,
  24. openid: '',
  25. server_no: '',
  26. platform: '',
  27. ps: 0,
  28. gold: 0,
  29. diamond: 0,
  30. last_battle_id: 0
  31. }
  32. //
  33. this.observers = []
  34. }
  35. onAttach(entity: Entity) {
  36. this.callback && this.callback()
  37. }
  38. onDetach(entity: Entity) {
  39. }
  40. }

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

  1. export class PlayerInfoObserver implements IObserver {
  2. update(subject: PlayerModelComp): void {
  3. const playerInfo = subject.playerInfo
  4. xhgame.vm.gateVM.ps = playerInfo.ps
  5. xhgame.vm.gateVM.gold = playerInfo.gold
  6. xhgame.vm.gateVM.diamond = playerInfo.diamond
  7. }
  8. }

4、开始使用

  1. export class JCQPlayerEntity extends Entity {
  2. model: PlayerModelComp
  3. init() {
  4. this.model = this.attachComponent(PlayerModelComp)
  5. }
  6. }

对playerModelComp添加多个监听者 

  1. xhgame.game.playerEntity.model.attach(new PlayerInfoObserver())
  2. xhgame.game.playerEntity.model.attach(new OtherObserver())

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

  1. export class PlayerInfoObserver implements IObserver {
  2. update(subject: PlayerModelComp): void {
  3. const playerInfo = subject.playerInfo
  4. xhgame.vm.gateVM.ps = playerInfo.ps
  5. xhgame.vm.gateVM.gold = playerInfo.gold
  6. xhgame.vm.gateVM.diamond = playerInfo.diamond
  7. }
  8. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/152446
推荐阅读
相关标签
  

闽ICP备14008679号