Inheritance of master data: Can this be done?

Example: I have an Entity called Car , which is abstract. Then there are two children of Cabriolet and Pickup .

Now I have an Entity called Driver , which has a relationship called currentCar 1..1 for Entity Car . Therefore, I can assign either Cabriolet or Pickup for any currentCar property of the driver. Then I will need to examine the object to find out at runtime if I have a convertible or pickup when I get the current driver from the driver. Is this a valid design in Core Data?

+1
source share
1 answer

I do not understand why this will not work at a technical level, but it violates the OOP polymorphism.

Why do you need to know if there is a car type? Could you define the methods of the abstract superclass ( Car ) and redefine them accordingly in the subclasses ( Cabriolet and Pickup )? Could you reorganize the hierarchy of cars so that the attributes of the subclasses become more general and transfer them to the attributes of the Car , thereby eliminating the need for subclasses?

I had problems with NSFetchResultsController when retrieving objects retrieved from a common superclass. (The returned objects can only be sorted / grouped by the attribute of the object. The class type is not an attribute, so it cannot be used to sort / group entities. My solution / hack was related to the type attribute for the superclass - ugly, but it worked.)

+2
source

Source: https://habr.com/ru/post/1385408/


All Articles