OOP 개발자가 TypeScript로 애플리케이션을 만들 때

나는 최근에 거의 100개의 엔티티에 대해 이 코드를 보았습니다.

export interface IXXX {
  attr1: string
  attr2: string
  attr3: string
  attr4: string
}

export class XXX implements IXXX {
  private _attr1: string = ''
  private _attr2: string = ''
  private _attr3: string = ''
  private _attr4: string = ''

  get attr1 (): string {
    return this._attr1
  }

  set attr1 (newattr1: string) {
    this._attr1 = newattr1
  }

  get attr2 (): string {
    return this._attr2
  }

  set attr2 (newattr2: string) {
    this._attr2 = newattr2
  }

  get attr3 (): string {
    return this._attr3
  }

  set attr3 (newattr3: string) {
    this._attr3 = newattr3
  }

  get attr4 (): string {
    return this._attr4
  }

  set attr4 (newattr4: string) {
    this._attr4 = newattr4
  }

  toString (): string {
    return this._attr2
  }

  static createWithData (attr1: string, attr2: string, attr3: string, attr4: string): XXX {
    const result = new XXX()
    result.attr1 = attr1
    result.attr2 = attr2
    result.attr3 = attr3
    result.attr4 = attr4
    return result
  }

  static createFromObject (someObject: IXXX): XXX {
    const newObject = new XXX()

    newObject.attr1 = someObject.attr1
    newObject.attr2 = someObject.attr2
    newObject.attr3 = someObject.attr3
    newObject.attr4 = someObject.attr4

    return newObject
  }
}


어떻게 생각해, 이게 갈 길이야?

그리고 console.log 이것으로부터 아무런 이점을 얻지 못하는 것 같습니다-toString을 참조하십시오.

좋은 웹페이지 즐겨찾기