What are the pros and cons of using interfaces in Delphi?

前端 未结 9 1667
既然无缘
既然无缘 2021-01-31 17:21

I have used Delphi classes for a while now but never really got into using interfaces. I already have read a bit about them but want to learn more.

I would like to hear

9条回答
  •  半阙折子戏
    2021-01-31 17:55

    The only case when we had to use interfaces (besides COM/ActiveX stuff) was when we needed multiple inheritance and interfaces were the only way to get it. In several other cases when we attempted to use interfaces, we had various kinds of problems, mainly with reference counting (when the object was accessed both as a class instance and via interface).

    So my advice would be to use them only when you know that you need them, not when you think that it can make your life easier in some aspect.

    Update: As David reminded, with interfaces you get multiple inheritance of interfaces only, not of implementation. But that was fine for our needs.

提交回复
热议问题