I would like to get some suggestions on the following problem: Let's say you want to write adapters for VCL controls. All adapters must have the same base class, but differ in special packet control (for example, getting a value from TEdit is different from getting a value from TSpinEdit). So, the first idea is to create a class hierarchy, for example
TAdapter = class end; TEditAdapter = class (TAdapter) end; TSpinEditAdapter = class (TAdapter) end;
Now I want to enter a field to reference the vcl control. In my special adapters, of course, I want to work with a specific subclass. But the base class should also contain a link (for example, if I want to use an adapter to make the control visible).
Option 1 (Downcast in Access 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 a property control, if I do something in my base class, I use a protected field.
Option 2 (use a common 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?
Respectfully,
Christian
source share