Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2012-05

Extend multiple classes with same code

11 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Zaher Dirkey

Hi,

I have 2 objects inherited from the base one

T_A = class(TObject);
T_B1 = class(T_A);
T_B2 = class(T_A);
T_B3 = class(T_A);

Now if I want to extend T_B1 it is easy to inherit it to T_C1 = class (T_B1) and add many functions to it, but.

I want the same extend T_B2 not T_B3,

1 - The first idea is adding this functions to T_A, but it is not my class to modify it, or it is protected from my modify.
2 - Copy and paste this functions to T_B2, but in that case I need every time to fix functions in B1 I must do it in B2, that make it so hard, and not good programming quality.

Is there any new idea to do that, what about Helper, generics, i read about it i felt it is not fit to my problem (or i am wrong).

Thanks in advance

Zaher Dirkey

Reply by email

Michael Van Canneyt
to Zaher Dirkey
Zaher Dirkey wrote 19 lines
Hi, I have 2 objects inherited from the base one T_A = class(TObject); T_B1 = class(T_A); T_B2 = class(T_A); T_B3 = class(T_A); Now if I want to extend T_B1 it is easy to inherit it to T_C1 = class (T_B1) and add many functions to it, but. I want the same extend T_B2  not T_B3, 1 - The first idea is adding this functions to T_A, but it is not my class to modify it, or it is protected from my modify. 2 - Copy and paste this functions to T_B2, but in that case I need every time to fix functions in B1 I must do it in B2, that make it so hard, and not good programming quality. Is there any new idea to do that, what about Helper, generics, i read about it i felt it is not fit to my problem (or i am wrong).

Maybe you can try interfaces and interface delegation.

Michael.

Reply by email

Zaher Dirkey
to Michael Van Canneyt

On Fri, May 18, 2012 at 9:18 PM, Michael Van Canneyt <

Show quoted text 6 lines
wrote: Maybe you can try interfaces and interface delegation.

I don't think so, class T_A is not my code or it is very strict to modify it, also i am talk about multiple implementation not interfacing objects (or you can provide me and example).

Best Regards
Zaher Dirkey

Reply by email

Sven Barth
to Zaher Dirkey: I have 2 objects inherited from the base one T_A = class(TObject); T_B1 = class(T_A);…
Zaher Dirkey wrote 25 lines
Hi, I have 2 objects inherited from the base one T_A = class(TObject); T_B1 = class(T_A); T_B2 = class(T_A); T_B3 = class(T_A); Now if I want to extend T_B1 it is easy to inherit it to T_C1 = class (T_B1) and add many functions to it, but. I want the same extend T_B2 not T_B3, 1 - The first idea is adding this functions to T_A, but it is not my class to modify it, or it is protected from my modify. 2 - Copy and paste this functions to T_B2, but in that case I need every time to fix functions in B1 I must do it in B2, that make it so hard, and not good programming quality. Is there any new idea to do that, what about Helper, generics, i read about it i felt it is not fit to my problem (or i am wrong). Thanks in advance

Helper types might help you here.

E.g.

type
   T_AHelper = class helper for T_A
     procedure YourNewProc;
   end;
procedure T_AHelper.YourNewProc;
begin
   ...
end;
var
   t: T_B1;
begin
   t.YourNewProc;
end;

You can access public, published and (strict) protected members using a class helper.

The helper needs to be in scope ( = it's unit needs to be in the uses) if you want to use one of it's methods on a instance of T_A (or T_B).

Regards,
Sven

Reply by email

Bernd
to Zaher Dirkey: I have 2 objects inherited from the base one T_A = class(TObject); T_B1 = class(T_A);…

2012/5/18 Zaher Dirkey :

Show quoted text 9 lines
Hi, I have 2 objects inherited from the base one T_A = class(TObject); T_B1 = class(T_A); T_B2 = class(T_A); T_B3 = class(T_A);

How about this:

T_A = class(TObject);
T_AX = class(T_A);    // <-- this class implements your extensions
T_B1 = class(T_AX);  // TB_1 and
T_B2 = class(T_AX);  // TB_2 inherit it
T_B3 = class(T_A);

Reply by email

Zaher Dirkey
to Bernd
Bernd wrote 14 lines
_B3 = class(T_A); How about this: T_A = class(TObject); T_AX = class(T_A); // <-- this class implements your extensions T_B1 = class(T_AX); // TB_1 and T_B2 = class(T_AX); // TB_2 inherit it T_B3 = class(T_A);

I can't, in fact it is my mistak when i explained.

B1 B2 is in the base unit like as A, i cant modify it, very strict.

My projects is db layer, i have TmyCustomField,
TmyFields=class(TmyCustomField) and TmyParams=class(TmyCustomField), that in the base unit.

now i like to inherit both TmyFields and TmyParams to add functions work on Firebird, TmyFirebirdFields and TmyFirebirdParams, both have same functions to work on buffer SQLDA.

Best Regards
Zaher Dirkey

Reply by email

Jorge Aldo G. de F. Junior
to Zaher Dirkey

You simply cant do it. Not even multiple inheritance would help you in this case.

You will have to extend each class by hand.

Multiple inheritance would allow you to join back branches in the class hierarchy, but in this case you want to add the same functionality to multiple branches, but keep the branches separate entities. This is impossible with current freepascal OOP and i dont think this is even a good thing to have.

It could be done multi-class helper, kind of class helper that works on multiple classes. But i dont even know of a language that has such possibility (i dont know much languages anyway).

2012/5/18 Zaher Dirkey :

Show quoted text 36 lines
On Fri, May 18, 2012 at 11:27 PM, Bernd wrote: > > > _B3 = class(T_A); > How about this: > > T_A = class(TObject); > > T_AX = class(T_A);    // <-- this class implements your extensions > > T_B1 = class(T_AX);  // TB_1 and > T_B2 = class(T_AX);  // TB_2 inherit it > T_B3 = class(T_A); > > I can't, in fact it is my mistak when i explained. B1 B2 is in the base unit like as A, i cant modify it, very strict. My projects is db layer, i have TmyCustomField, TmyFields=class(TmyCustomField) and TmyParams=class(TmyCustomField), that in the base unit. now i like to inherit both TmyFields and TmyParams to add functions work on Firebird, TmyFirebirdFields and TmyFirebirdParams, both have same functions to work on buffer SQLDA. Best Regards Zaher Dirkey _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Zaher Dirkey
to Jorge Aldo G. de F. Junior

On Sat, May 19, 2012 at 2:10 AM, Jorge Aldo G. de F. Junior <

Show quoted text 14 lines
wrote: You simply cant do it. Not even multiple inheritance would help you in this case. You will have to extend each class by hand. Multiple inheritance would allow you to join back branches in the class hierarchy, but in this case you want to add the same functionality to multiple branches, but keep the branches separate entities. This is impossible with current freepascal OOP and i dont think this is even a good thing to have.

I have no Idea about "multiple inheritance".

I am thinking now if i can assign a helper to more than one class by make base helper, then inherit it for every class B1 and B2. I will try and post a feedback.

It could be done multi-class helper, kind of class helper that works on multiple classes. But i dont even know of a language that has such possibility (i dont know much languages anyway).

I hope i can find one, not for me for my kids in the future ;)

Best Regards
Zaher Dirkey

Reply by email

Sven Barth
to Zaher Dirkey
Zaher Dirkey wrote 20 lines
On Sat, May 19, 2012 at 2:10 AM, Jorge Aldo G. de F. Junior < > wrote: You simply cant do it. Not even multiple inheritance would help you in this case. You will have to extend each class by hand. Multiple inheritance would allow you to join back branches in the class hierarchy, but in this case you want to add the same functionality to multiple branches, but keep the branches separate entities. This is impossible with current freepascal OOP and i dont think this is even a good thing to have. I have no Idea about "multiple inheritance". I am thinking now if i can assign a helper to more than one class by make base helper, then inherit it for every class B1 and B2. I will try and post a feedback.

Yes, you can do that basically:

type
   T_AHelper = class helper for T_A
   end;
   T_B1Helper = class helper(T_AHelper) for T_B1
   end;
   T_B2Helper = class helper(T_AHelper) for T_B2
   end;

Please not that the extended classes (in this case T_B1 and T_B2) need to inherited from T_A, otherwise the compiler will complain.

Regards,
Sven

Reply by email

Jorge Aldo G. de F. Junior
to Sven Barth

So basically they are in the same branch (Well you could argue that every object inherits from TObject, but i cant imagine someone creating a helper class for TObject).

Show quoted text 10 lines
Please not that the extended classes (in this case T_B1 and T_B2) need to inherited from T_A, otherwise the compiler will complain. Regards, Sven _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Zaher Dirkey
to Jorge Aldo G. de F. Junior

On Sat, May 19, 2012 at 7:54 PM, Jorge Aldo G. de F. Junior <

Show quoted text 5 lines
wrote: but i cant imagine someone creating a helper class for TObject).

It is in the example above but in real it base class of mine, TmncCustomField in fact

But it fail for me, because there is overridden functions.

BTW thanks for you all

Best Regards
Zaher Dirkey

Reply by email