Lazarus

Free Pascal => General => Topic started by: simsee on February 22, 2017, 12:12:32 pm

Title: Propagate to subclasses features added to a superclass
Post by: simsee on February 22, 2017, 12:12:32 pm
Dear all, I have problem concerning oop design. In an external library (with object pascal code available) I have a class hierarchy with a root class and some derived subclasses. I'd like to add some features (fields, properties and methods) to root class and to propagate them to existing derived subclasses. In order to achieve this goal, I have to modify the original code of library, or can I use some clever solutions (class helpers?, multiple inheritance via interfaces?)? Thanks!
Title: Re: Propagate to subclasses features added to a superclass
Post by: Edson on March 14, 2017, 08:47:40 pm
FAIK, the only and safe way is to modify the root class.
Title: Re: Propagate to subclasses features added to a superclass
Post by: simone on March 15, 2017, 10:34:10 pm
The pure OOP approach requires inheritance/subclassing to extend a class. So in this case the straight solution should be add fields/properties/methods to the root class. The price to pay for that would be the modification of code in the external library/component. If this is unacceptable or impossible, you can use a class helper for the root class, in order to add features that are then inherithed by subclasses. However class helpers have some limitations, among others in this case you can't add fields to the 'helped class', nor properties relying on backing field, but only methods and properties that don't refer to backing fields. This could be a solution for the problem. However notice that, in general, the use of class helpers by design is considered bad practice.
TinyPortal © 2005-2018