Typescript ::抽象静态

时间:2017-05-26 07:56:02

标签: generics typescript inheritance

在我的一个个人项目中,我试图设计一些对象和列表类型。对象和列表应该是可序列化的(即具有toJSON()fromJSON()方法)。示例对象和列表将具有以下基本代码:

type IPerson = {
  id: number;
  name: string;
  // additional properties
}

class Person {
  id: number;
  name: string;
  // additional properties
  constructor(id: number, name: string, ...) { ... }
  toJSON(): IPerson { return { ... } }
  static fromJSON(json: IPerson): Person { return new Person(...) }
  // additional methods
}

class PersonList {
  list: Person[];
  constructor(list: Person[]) { ... }
  findById(id: number) { return this.list.find(it => it.id === id) }
  findByName(name: string) { return this.list.find(it => it.name === name) }
  add(person: Person) { this.list.push(person) }
  remove(person: Person) { this.list = this.list.filter(it => it !== person) }
  toJSON(): IPerson[] { return this.list.map(it => it.toJSON()) }
  static fromJSON(json: IPerson[]): PersonList { return new PersonList(json.map(it => Person.fromJSON(it))) }
  // additional methods
}

我正在使用的所有对象和列表至少包含此处列出的方法。

现在我正在尝试将其转换为通用解决方案,以便:

type JSON = {
  id: number;
  name: string;
}

abstract class BaseObject<T extends JSON> {
  abstract get id();
  abstract get name();
  constructor(id: number, name: string) { ... }
  abstract toJSON(): T
  abstract static fromJSON(json: T): BaseObject<T>
}

class BaseList<T, U> {
  list: BaseObject<T>[];
  constructor(list: BaseObject<T>[]) { ... }
  findById(id: number) { return this.list.find(it => it.id === id) }
  findByName(name: string) { return this.list.find(it => it.name === name) }
  add(obj: BaseObject<T>) { this.list.push(obj) }
  remove(obj: BaseObject<T>) { this.list = this.list.filter(it => it !== obj) }
  toJSON(): U[] { return this.list.map(it => it.toJSON()) }
  static fromJSON(json: U[]): BaseList<T, U> { return new BaseList<T, U>(json.map(it => BaseObject<T>.fromJSON(it))) }
}

如果这个结构有效(它没有),它将使我的生活如此简单:

type IPerson = JSON & {
  // additional fields
}

class Person extends BaseObject<IPerson> {
  get id() { ... }
  get name() { ... }
  // additional getters for other fields
  toJSON(): IPerson { return { ... } }
  static fromJSON(json: IPerson): Person { return new Person(...) }
  // additional methods
}

class PersonList extends BaseList<Person, IPerson> {
  // additional methods
}

// other object and list types definitions follow

然而,我的解决方案在这些方面失败了:

  1. BaseObject不能使用抽象静态fromJSON()方法。
  2. BaseList不能使用抽象静态fromJSON()方法。
  3. BaseList.fromJSON()无法实例化新列表,也无法调用BaseObject.fromJSON()来实例化新对象。
  4. 我如何规避这些问题?我有没有更好的设计模式?

1 个答案:

答案 0 :(得分:0)

由于我无法定义静态抽象函数,因此这是我使用的实现:

type BaseJson = {
    id: string;
}
abstract class BaseObj<U> {
    abstract get id(): string;
    abstract toJSON(): U;
}
abstract class BaseList<T extends BaseObj<U>, U> {
    list: BaseObj<U>[] = [];
    add(x: BaseObj<U>) { this.list.push(x) }
    remove(x: BaseObj<U>) { this.list = this.list.filter(it => it !== x) }
    toJSON(): U[] { return this.list.map(it => it.toJSON()) }
}

type IPerson = BaseJson & {
    name: string;
}
class Person extends BaseObj<IPerson> {
    data: IPerson;
    constructor(data: IPerson) { super(); this.data = data }
    get id() { return this.data.id }
    get name() { return this.data.name }
    toJSON(): IPerson { return { id: this.id, name: this.name } }
    static fromJSON(json: IPerson): Person { return new Person(json) }
}
class PersonList extends BaseList<Person, IPerson> {
    constructor(list: Person[]) { super(); this.list = list }
    static fromJSON(json: IPerson[]): PersonList { return new PersonList(json.map(it => Person.fromJSON(it))) }
}

底线:我必须在所有对象和列表类中定义静态fromJSON()方法。这些方法并不复杂,所以这是一个舒适的双赢。 =)