I have a base class that is used as a blueprint for other classes. Data of these classes are stored in SQL database and loaded when needed. The problem is how I can know what database object corresponds to what class.
So I have an animal class and 2 more classes (cat and dog) that extends an animal class. Dog and Cat classes will have all properties that base class have but they can hold different methods/functions. The database only stores data of these classes. Now the problem is when this data needs to be loaded how can the system tell if should create dog or cat class?
Here is some example code
const database_data = { name: "test", strength: 10 };
class Animal {
public name: string;
protected strength: number;
constructor(name: string, strength: number) {
this.name = name;
this.strength = strength;
}
get getStrength(): number {
return this.strength;
}
}
class Dog extends Animal {
constructor(name: string, strength: number) {
super(name, strength);
}
wuf(): void {
console.log(`Dog ${this.name} says wuf. Strength: ${this.strength}`);
}
}
class Cat extends Animal {
constructor(name: string, strength: number) {
super(name, strength);
}
miau(): void {
console.log(`Cat ${this.name} says miau. Cat is not strong ;)`);
}
}
//Loading animals from database....
// const loadedAnimal = new Dog/Cat? how do I know(database_data.name, database_data.strength);
// console.log(loadedAnimal);