i have a question about a refactor from condition to polymorph object. I can't understand how I can generalize a condition into a polymorphic object by making it always return the correct value of the data type based on a type.
I have tried with an abstract class only I don't know how to proceed. Sorry if the question is wrong or trivial, but I don't know how to do it.
my polymorph-model.ts
import { ViewerDataType, XMLImagesValues } from "./models";
export abstract class ViewerDataSource {
constructor(
public _type: ViewerDataType
){}
abstract getSource(): string | XMLImagesValues[];
}
export class ManifestSource extends ViewerDataSource {
getSource(): string {
return this._type.value.manifestURL
}
}
export class XMLSource extends ViewerDataSource {
getSource(): XMLImagesValues[] {
return this._type.value.xmlImages
}
}
a snippet of my component.ts for the refactor. Here i need to change the two condition in the @Input and in the ngAfterViewInit
//...
public _viewerDataType: string; // tslint:disable-line: variable-name
public _manifestPath: string; // tslint:disable-line: variable-name
public _xmlImagesPath: XMLImagesValues[]; // tslint:disable-line: variable-name
@Input() set data(v: ViewerDataType) {
// TODO: need to be refactored with polymorph
if (v.value.manifestURL !== this._manifestPath) {
this._manifestPath = v.value.manifestURL;
this.manifestChange.next(this._manifestPath);
}
if (v.value.xmlImages !== this._xmlImagesPath) {
this._xmlImagesPath = v.value.xmlImages;
this.xmlImageChange.next(this._xmlImagesPath);
}
this._viewerDataType = v.type;
}
manifestChange = new BehaviorSubject<string>('');
xmlImageChange = new BehaviorSubject<XMLImagesValues[]>([]);
ngAfterViewInit() {
// TODO: need to be refactored with polymorph
if (this._viewerDataType === 'manifest'){
this.tileSources = this.manifestTileSources;
}else{
this.tileSources = this.xmlImageTileSources;
}
}
//...
Thanks a lot to those who will answer.