python – How to model two abstractions that have polymorphic relation in OOP


So I have a problem how to model data in my project. I have it in python and doing it in OOP fashion. But I cannot figure out a right way to model this case:

Let’s say there is an abstract class called Fruit. Then there is a class that inherits from fruit that is named Apple and another one Grape.

Then I want to add to Fruit a parameter that is a list of another abstract class called Sauces. Then there is a class that inherits from sauce that is named Teriyaki. Teriyaki object should act differently when it is a sauce that is applied to apple and differently when it is applied to grape.

I want to call a function on each sauce in the list of sauces that would be somehow aware of post polimorphic type of fruit and apply adequate apply function.

Whatever design I come up with, it looks hacky and just ugly. Please help me with ideas or maybe there is a common pattern for it.



Source link

Leave a Comment