Singleton.ts 447 B

1234567891011121314151617181920212223
  1. /**
  2. * 单例基类
  3. */
  4. export class Singleton {
  5. constructor() {}
  6. /**获取单例实例
  7. * @returns 单例实例
  8. */
  9. public static ins<T extends {}>(this: new () => T): T {
  10. if (!(<any>this)._ins) {
  11. ;(<any>this)._ins = new this()
  12. }
  13. return (<any>this)._ins
  14. }
  15. /**
  16. * 销毁单例实例
  17. */
  18. public static destroy<T extends {}>(this: new () => T): void {
  19. ;(<any>this)._ins = null
  20. }
  21. }