samedi 28 avril 2018

Structural typing and Polymorphism - Adding super types

Below is an example code(polymorphic) taken from angular framework using TypeScript syntax,

export abstract class AbstractControlDirective {
   ...
}


AbstractControlDirective subtypes

export abstract class NgControl extends AbstractControlDirective {
     ...
}
export abstract class ControlContainer extends AbstractControlDirective {
   ...
}
export declare abstract class ControlContainer extends AbstractControlDirective {
  ...
}
export declare abstract class NgControl extends AbstractControlDirective {
    ...
}


NgControl subtypes

export class NgModel extends NgControl implements OnChanges, OnDestroy {
   ...
}

export class FormControlDirective extends NgControl implements OnChanges {
    ....
}

export class FormControlName extends NgControl implements OnChanges, OnDestroy {
   ...
}

export declare class FormControlDirective extends NgControl implements OnChanges {
    ...
}

export declare class FormControlName extends NgControl implements OnChanges, OnDestroy {
   ...
}

export declare class NgModel extends NgControl implements OnChanges, OnDestroy {
   ...
}

In general, there are many situations, that came across new requirements, to add obvious looking super type in intermediate layers of class hierarchy, that injects breakage in subtypes, unless using some design pattern. Using design pattern can make the class hierarchy less error prone but breaks the hierarchy structure.


To avoid this problem, Can we maintain this hierarchy without using extends keyword? TypeScript being structural typed...

Aucun commentaire:

Enregistrer un commentaire