How should I properly satisfy the Liskov Substitution Principle in this sample?
class User(ABC): # App core logic
def add_photo(self, photo: bytes, ) -> None:
...
class FacebookUser(User): # API interation logic
def add_photo(self, photo: custom_type, ) -> None:
... # Set of Facebook specific actions.
super().add_photo(photo=photo.bytes, )
I know 4 solutions:
- Name mangling. Flaws:
Forcing me to rename all the existing code withParent.add_photoand probably all the rest methods. - Composition. Flaws:
2.1. Can't reflect the logical relation between the classes.
2.2 Forcing me to implement (proxy) methods for everyParentmethod. ifchecking for theselfobject type inside the method. Flaws:
3.1. Additional complexity and clumsy branches.- Implement
convert_photomethod fromcustom_typetobytestype inside theFacebookUserchild class. Flaws:
4.1 I still need to place a newconvert_photomethod inside some kinda "event" method (set of methods) and the best name for it isadd_photo.
Aucun commentaire:
Enregistrer un commentaire