samedi 5 mars 2022

Should I create a new child class to handle a new feature?

I have an abstract class A with an abstract method m. The abstract class A is inherited by classes B, C, and D which have their own implementation of method m. Method m returns a JSON object.

I need to add a new feature that would mask a few fields from the JSON object returned by method m in class D. Method m will return masked JSON if the input is true. If not, it will return JSON. This feature will not be applicable to the implementations of method m by classes B and C.

I am not sure how to add this feature. I can't change the method's signature because the new input is not applicable to other child classes of A. I thought of adding a new class E whose implementation of method m will mask the JSON but there would be some duplicate code between D and E. What would be the best way to handle this?

Aucun commentaire:

Enregistrer un commentaire