0

I'm facing a problem that I think cannot be solved without some amount of code replication, but maybe I'm missing something.

This is my scenario:

TCommonAncestor = class (TSomeClass, ISomeInterface1, ISomeInterface2)

TAncestor1 = class (TCommonAncestor)

TAncestor2 = class (TCommonAncestor)

TMyClass1 = class (TAncestor1)

TMyClass2 = class (TAncestor2)

Given I can't modify nothing before TMyClass1/2 (the ancestors are Delphi classes, to be more specific I'm inheriting from FireDAC's Query/MemTable/StoredProc...), I need to implement a number of methods and properties common to all my inherited classes. Obviously the interface isn't a problem, I can create a new IMyInterface that declares all my new methods and properties, and modify my class declarations like this:

TMyClass1 = class (TAncestor1, IMyInterface)

TMyClass2 = class (TAncestor2, IMyInterface)

But then I must implement every method on every derived class...

Is there a way to avoid duplicate so much code?

Bozzy
  • 541
  • 4
  • 21
  • Unfortunately, Delphi does not support multiple inheritance. Interfaces or a compositional model are your only choice. Which version of Delphi? – J... Aug 06 '14 at 11:23
  • Ops, I hadn't thought of seraching for multiple inheritance... I'm using Delphi XE6. – Bozzy Aug 06 '14 at 11:54
  • I've added a new answer to the duplicate question with another alternative that may suit your particular use case. http://stackoverflow.com/a/25160619/327083 – J... Aug 06 '14 at 12:37
  • 2
    Alternatively, if all you are worried about is replicating the interface implementations in each child class you can read a bit more about delegating interface implementations. In this way you can have a common interface implementation class - see for example : http://stackoverflow.com/a/3483889/327083 – J... Aug 06 '14 at 13:06
  • Thank you J. Actually delegating interface implementation is more suitable for my needs. I have to create components that directly inherit from FireDAC ones, they must be usable as if a native FireDAC component was there, and sometimes in some generic code I use methods and properties from TDataSet on these components. Your answer to the duplicate question gives a very clean implementation technique, but I can't figure out how this can be applied to my scenario, given the constraint I've just mentioned. – Bozzy Aug 07 '14 at 08:28

0 Answers0