How to use the interface with VCL classes?

In my previous question, I was offered to use the interface: How to implement identical methods with 2 or more classes?

But nothing is done as I do not know how to use the interfaces.

I think you can call this code pseudo :)

type IDoSomething = interface ['{EFE0308B-A85D-4DF3-889C-40FBC8FE84D0}'] procedure DoSomething1; procedure WMSize(var Message: TWMSize); message WM_SIZE; // <- Unknown directive: 'message' end; TMyCheckBox = class(TCheckBox, IDoSomething) // <- Undeclared identifier: 'DoSomething1' end; TMyRadioButton = class(TRadioButton, IDoSomething) // <- Undeclared identifier: 'DoSomething1' end; implementation procedure IDoSomething.DoSomething1; // Identifier redeclared: 'IDoSomething' begin // do the same thing for TMyCheckBox and TRadioButton end; procedure IDoSomething.WMSize(var Message: TWMSize); begin // handle this message the same as with TMyCheckBox and TRadioButton end; 

How to use the interface to centralize the code? and how can I share new properties using the interface?

+2
source share
3 answers
  • The message keyword is not valid in an interface method declaration. Remove message WM_SIZE; , and the code will work fine until the compiler encounters the second problem - see below

  • Interface methods never have their own implementations. If the IDoSomething interface declares a DoSomething1 method, the compiler will not allow you to write an implementation of the procedure IDoSomething.DoSomething1; type procedure IDoSomething.DoSomething1; as in your code, instead, a class that implements the interface should provide an implementation body.

+3
source

This is my point:

1) You cannot include an interface definition

 procedure WMSize(var Message: TWMSize); message WM_SIZE; 

because it is actually a dynamic method:

Remember that interface definition methods cannot be declared as virtual , dynamic , abstract, or overriding .

2) Including interface definition

 procedure WMSize(var Message: TWMSize); 

- This is nonsense, because this does not mean that the method signature should be implemented further.

Interface definition must be bare:

 type IDoSomething = interface ['{EFE0308B-A85D-4DF3-889C-40FBC8FE84D0}'] procedure DoSomething1; end; 

The following class definitions remain unchanged (the same for their implementation):

 TMyCheckBox = class(TCheckBox, IDoSomething) procedure DoSomething1; procedure WMSize(var Message: TWMSize); message WM_SIZE; end; TMyRadioButton = class(TRadioButton, IDoSomething) procedure DoSomething1; procedure WMSize(var Message: TWMSize); message WM_SIZE; end; implementation procedure TMyCheckBox.DoSomething1; begin // end; procedure TMyCheckBox.WMSize(var Message: TWMSize); begin // end; { TMyRadioButton } procedure TMyRadioButton.DoSomething1; begin // end; procedure TMyRadioButton.WMSize(var Message: TWMSize); begin // end; 
+2
source

As the name says: Interfaces are a declaration of an interface, not an implementation. And this is what tees are for. They give you the ability to call procedures in different classes, even if you don't know anything about the class.
And since totaly different classes can implement the same interface, the implementation can be completely different. So your code looks like this:

 type IDoSomething = interface ['{EFE0308B-A85D-4DF3-889C-40FBC8FE84D0}'] procedure DoSomething1; procedure WMSize(var Message: TWMSize); end; TMyCheckBox = class(TCheckBox, IDoSomething) public procedure DoSomething1; procedure WMSize(var Message: TWMSize); message WM_SIZE; end; TMyRadioButton = class(TRadioButton, IDoSomething) procedure DoSomething1; procedure WMSize(var Message: TWMSize); message WM_SIZE; end; implementation procedure TMyCheckBox.DoSomething1; begin // do the same thing for TMyCheckBox and TRadioButton end; procedure TMyCheckBox.WMSize(var Message: TWMSize); begin // handle this message the same as with TMyCheckBox and TRadioButton end; procedure TMyRadioButton.DoSomething1; begin // do the same thing for TMyCheckBox and TRadioButton end; procedure TMyRadioButton.WMSize(var Message: TWMSize); begin // handle this message the same as with TMyCheckBox and TRadioButton end; 
+1
source

Source: https://habr.com/ru/post/1390313/


All Articles