TS的泛型单例模板(已解决)

如题。C#的单例模板我会,这个我不会。

1赞
export default class SampleClass {
    private static _instance: SampleClass = null;

    public static get Instance() {
        return SampleClass._instance || (SampleClass._instance = new SampleClass());
    }
}
2赞

这是我写的,但是在另一个脚本中调用却调不出SetAcitve();

//这个脚本用于UI的总管理
@ccclass(“UIMgr”)
export class UIMgr extends Component {
//至少有4个一级界面,3个二级界面
//这里控制UI的显示与隐藏
//所有UI窗体都继承自这个脚本

private static instance: UIMgr = new UIMgr();

//限制产生多个对象
private constructor() {
    super();
}

/**
 * 获得实例对象
 */
public static Instance(): UIMgr {
    return this.instance;
}

/**
 * 设置UI的显示与隐藏
 */
public SetAcitve() {
    if (this.node.active = false) {
        this.node.active = true;
    }
}

}

在这个GameOver()中的 UIMgr.Instance.说明我调不到。

import{ UIMgr } from “./UIMgr”;

//这个脚本负责游戏逻辑
@ccclass(“GameMgr”)
export class GameMgr extends Component {
//游戏初始化、场景跳转
//游戏结算等
//使用中介者模式

@property
public isCross:boolean;

@property
public Score=0;//得分

/**
 * 计算得分
 */
public CalScore() {
    //是否穿过平台
    if (this.isCross = true) {
        this.Score += 1;
    }
}
   

/**
 * 游戏结束
 */
public GameOver() {
    //结算奖励

    //返回主界面

    UIMgr.Instance.

    console.log("游戏结束");
}

}

大佬帮忙看一下我为什么调不到单例。

你的Instance()是个方法,调用的时候当然要加括号,另外获取实例对象的时候不应该是return this.instance而应该是UIMgr.instance

您给我的好像不是单例模板(泛型单例),我参照官方写了一个,但是NetMgr.Instance().; //这里无法调用到NetMgr.ts中的方法Hello()。这是为什么?

代码如下:
import { _decorator, Component, Node } from “cc”;
const { ccclass, property } = _decorator;

@ccclass(“SingletonPattern”)
export default class SingletonPattern {

private static instance:any=null;
public static Instance(type:{new ():T}){
if(this.instance==null){
this.instance=new type();
}
return this.instance;
}

}

继承自单例模板:
import { _decorator, Component, Node } from “cc”;
import SingletonPattern from “./Help/SingletonPattern”;
const { ccclass, property } = _decorator;

//这个脚本用于对网络服务的管理
@ccclass(“NetMgr”)
export class NetMgr extends SingletonPattern< NetMgr> {
//可能会单独把微信开放域相关的代码放在另一个脚本
//而这个脚本则是对这个进行管理

public Log()
{
    console.log("单例模板成功使用");
}

}

调用单例子类的方法:
import { _decorator, Component, Node } from “cc”;
const { ccclass, property } = _decorator;

import{ UIMgr } from “./UIMgr”;
import{NetMgr} from “./NetMgr”;

//这个脚本负责游戏逻辑
@ccclass(“GameMgr”)
export class GameMgr extends Component {
//游戏初始化、场景跳转
//游戏结算等
//使用中介者模式

@property
public isCross:boolean;

@property
public Score=0;//得分

/**
 * 计算得分
 */
public CalScore() {
    //是否穿过平台
    if (this.isCross = true) {
        this.Score += 1;
    }
}
   

/**
 * 游戏结束
 */
public GameOver() {
    //结算奖励

    //返回主界面
    NetMgr.Instance().; //这里无法调用。
   
    UIMgr.Instance().SetAcitve();

    console.log("游戏结束");
}

}

大佬,还有个问题,能解答一下吗?就是单例模板/泛型单例的问题。

简单单例模式

官方单例模板?你哪儿找的?如果你自己真的有需求用到高级的ts特性,建议你还是完整的看看ts语法。否则后面估计会遇到更多问题

export class Singleton{
private static instance: any = null;
public static Instance(c: { new(): T }): T {
if (this.instance == null) {
this.instance = new c();
}
return this.instance;
}

public Update(dt: number) {

}

}
只能帮到你这

cocos论坛里,有个帖子叫泛型单例。

你这个就直接报错了。

你就不会改一下加个???

这样写…???好像可以



我这么写不行,我的子类不是继承自Component了。

把T替换成Component也是不行的。

这样写也不行。

:sweat_smile:

这是解决了吗。。

1赞

你要明白单例是什么,确保全局只有一个。这是写的方法又很多,切换场景后,你还要确定单例只有一个这个要加一个if判断,我只是写了一个最最最简单的给你,高端的又怕你看不懂。。。其实就跟C#转过来一样,又空你去看下ts 的语法,其实出入不大的,只要你自己可以看的明白

你觉得你应该把那些语法弄懂一下