I have created two classes that extend an abstract class:
class SubstitutionTeacher extends SubstitutionAbstract {
abstract _save();
}
class SubstitutionFree extends SubstitutionAbstract {
public _save() {
}
}
class SubstitutionSubject extends SubstitutionAbstract {
public _save() {
}
}
Within the method save()
, I have implemented specific behaviors for each subclass as follows:
/* Class SubstitutionFree
public _save() {
const substitutionAdd: ISubstitutionModelTeacher = {
lesson: this.substitution.lessonNumber,
confirmed: true,
subsDate: this.substitution.date,
newTeacher: this.substitution.teacher
};
return this.replaceService.addSubstitution(substitutionAdd);
}
/* Class SubstitutionSubject
public _save() {
const substitutionAdd: ISubstitutionModelTeacher = {
lesson: this.substitution.lessonNumber,
confirmed: true,
newTeacher: this.substitution.teacher
};
return this.replaceService.addSubstitution(substitutionAdd);
}
It is evident that both methods share a common structure. To avoid duplication, we can extract this common part:
{ lesson: this.substitution.lessonNumber,
confirmed: true,
newTeacher: this.substitution.teacher
}
One approach to address this duplication would be to refactor the save()
method and pass in this common structure as a parameter. However, this might compromise the abstraction principle.