Technically I only have one constructor, as I'm using Typescript, but have functions called from the constructor depending on what interface the constructors argument is. This is a uml describing the class I'm talking about
implementation of the constructors looks ~like
constructor(data: I1){
this.a1 = data.a1
if (data.kind === "I1"){
I1constructor(data)
}else{
I2&I3constructor(data)
}
}
I1constructor(data: I1){
a1 = data.a1
a2 = data.a2
}
I2&I3constructor(data: I2 | I3){
a3 = data.a3
a4 = data.a4
a1 = data.kind === "I2" ? data.a1 : data.a1 + data.a2
}
I feel like there must be some kind of design pattern which is the best approach for a class of this type, which doesn't randomly leave some variable unassigned. Ideally the uninstantiated variable just wouldn't be callable if the type isn't right. I feel like just using an abstract class with subclasses for each interface just isn't quite what I'm looking for.
Aucun commentaire:
Enregistrer un commentaire