I'm trying to create a factory that creates instance using a string id.
I will let the code explain itself...
class A
{
}
class B
{
}
class MyFactory{
private static _instance: MyFactory;
private _map: { [id: string]: object; } = {};
private constructor() {
// Map the id and classes
this._map["A"] = A;
this._map["B"] = B;
}
public createInstance(id: string) : object {
let newInstance = null;
if (this._map[id] != null) {
newInstance = Object.create(this._map[id]);
newInstance.constructor.apply(newInstance);
}
return newInstance;
}
public static get instance() {
return this._instance || (this._instance = new this());
}
}
I'm a bit struggling with what should I save in the map and how should I use it to construct a new instance from that class...