Recent

Author Topic: Propagate to subclasses features added to a superclass  (Read 2841 times)

simsee

  • Full Member
  • ***
  • Posts: 184
Propagate to subclasses features added to a superclass
« 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!

Edson

  • Hero Member
  • *****
  • Posts: 1302
Re: Propagate to subclasses features added to a superclass
« Reply #1 on: March 14, 2017, 08:47:40 pm »
FAIK, the only and safe way is to modify the root class.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

simone

  • Hero Member
  • *****
  • Posts: 573
Re: Propagate to subclasses features added to a superclass
« Reply #2 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.
« Last Edit: March 16, 2017, 11:23:39 am by simone »
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

 

TinyPortal © 2005-2018