Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SW-Design: Adapters for Class Hierarchy in Delphi (Generics vs. Downcast)

I would like to have some suggestions for the following problem: Let's say you want to write adapters for the VCL controls. All Adapters should have the same base class, but differ in wrapping special controls (for example getting a value from a TEdit is different from getting a value from TSpinEdit). So the first idea is to create a class hierarchy like

TAdapter = class
end;

TEditAdapter = class (TAdapter)
end;

TSpinEditAdapter = class (TAdapter)
end;

Now I want to introduce a field to hold a reference to the vcl control. In my special adapaters I want - of course - work with the concrete subclass. But the Base class should also contain a reference (for example if I want to use the adapter to make a control visible).

Possibility 1 (Downcast in Property Accessor):

TAdapter = class
protected
  FCtrl : TControl;
end;

TEditAdapter = class (TAdapter)
  public
    property Control : TEdit read GetControl write Setcontrol;
end;
{...}
function TEditAdapter.GetControl : TEdit;
begin
  Result := FCtrl as TEdit;
end;

So if I implement a specific method I work with the property Control, if I do something in my base class I use the protected field.

Possibility 2 (Use a generic base class):

TAdapter = class
end;

TAdapter <T : TControl> = class (TAdapter)
protected
  FCtrl : T;
end;

TEditAdapter = class (TAdapter <TEdit>)
end;

Which solution would you prefer? Or is there a third solution, which is even better?

Kind regards,

Christian

like image 436
Christian Metzler Avatar asked Jun 17 '11 09:06

Christian Metzler


1 Answers

You can't use generics to solve this problem, because you'll be in one of two situations:

  • The property or method you want to "Adapt" (the Text property for example) is defined in an ancestor class. In that case you don't need generics because you can use the one adapter for the ancestor and solve the problem for all descendants.
  • The property or method is introduced by the class you want to adapt. In that case you can't use generics because in order to access the property or method you'd need a generic type constraint of that given type. Example. Let's say you want an adapter for the Text property of TMyClass. Let's assume TMyClass is the one introducing the Text property. In order to access it, you'd need to declare the generic type as TGeneric<T:TMyClass> and that's not actually generic.

In my opinion the best bet is to write specific adapters for each class, as in your first option. You might be able to use RTTI tricks to make your first option easier to implement, but I'm not sure it'd be worth it.

like image 150
Cosmin Prund Avatar answered Oct 14 '22 03:10

Cosmin Prund