实现多个接口的类的清晰度(替代代理): [英] Clarity in classes implementing multiple interfaces (alternative to delegation):

查看:263
本文介绍了实现多个接口的类的清晰度(替代代理):的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们得到以下内容:

Let's say we've got the following:

IFirst = Interface(IUnknown)    
  function GetStuff: Integer;
end;

ISecond = Interface(IUnknown)
  function GetOtherStuff: Integer;
end;

TFirstSecond = class(TInterfacedObject, IFirst, ISecond)    
private 
  function GetStuff: Integer;        //implementation of IFirst
  function GetOtherStuff: Integer;   //implementation of ISecond;
end;

我从来没有喜欢这样的事实,在TInterfacedObject中似乎没有办法区分哪些方法实现哪个接口。我错过了什么吗?有人知道一个方式结构的代码来做到这一点吗?要指定GetStuff是执行IFirst和GetOtherStuff是执行ISecond吗? ('发表评论'不是我正在寻找的答案...)

I have never liked the fact that in TInterfacedObject there seems to be no way to distinguish between which methods implement which interfaces. Am I missing something? Does anyone know a way structure the code to do that? To designate that GetStuff is the implementation of IFirst and GetOtherStuff is the implementation of ISecond? ('Put a comment' is not the answer I'm looking for...)

我知道我可以使用'implements'指令来定义TFirstSecond中的属性每个接口并将实现委托给包含在TFirstSecond内的实例,从而隔离所有内容。但是我想要一个快捷方式...

I know I can use the 'implements' directive to define properties in TFirstSecond for each interface and delegate the implementations to instances contained within TFirstSecond, thereby segregating everything. But I'd like a shortcut...

推荐答案

在发布的情况下,我更喜欢评论(界面名称只是NGLN放置它),但我想解释为什么实现关键字可能是其他一些情况下最好的解决方案,只是不是在平凡的情况下,每个接口只有一个方法,在平凡的样例。

In the case you post, I prefer comments (interface name just as NGLN puts it) too, but I would like to explain why the implements keyword can be the best solution in some other cases, just not in trivial cases where there's only one method per interface as in your trivial sample.

我知道你说你知道实现;但是对于没有看到它的人来说,我想记录它是有用的,忍受我。在某些情况下,甚至有额外的工作需要更多的课程。

I know you said you know about Implements; but for people coming along who have not seen it, I'd like to document when it IS useful, bear with me. It is even worthwhile having all the extra work of having more classes, in some cases.

所以我将使用工具不作为快捷方式(因为你看到它更长!但是只有当每个接口涉及到100种方法才能实现时,并且结果设计的耦合较少,并且仅具有更好的内聚和可读性。

So I would use implements not as a shortcut (as you see it's longer!) but only when each interface involves 100 methods to be implemented, and where the resulting design has less coupling, and better cohesion and readability only.

所以这是一个诚实的愚蠢的例子,但是如果每个IFirst和ISecond都有100种方法,那么这可能是一个很大的飞跃。

So this is an admittedly silly example, but if each of IFirst and ISecond had 100 methods, then it might be a great leap forward...

type
IFirst = interface
  function GetStuff: Integer;
end;

ISecond = interface
  function GetOtherStuff: Integer;
end;

TFirstLogic = class(TInterfacedObject, IFirst)
  function GetStuff: Integer;

end;

TSecondLogic = class(TInterfacedObject, ISecond)
  function GetOtherStuff: Integer;
end;

TFirstSecond = class(TInterfacedObject, IFirst, ISecond)
private
  FFirst:TFirstLogic;
  FSecond:TSecondLogic;
protected


  property First:TFirstLogic read FFirst implements IFirst;
  property Second:TSecondLogic read FSecond implements ISecond;
public
  constructor Create; // don't forget to create and free FFirst/FSecond.
  destructor Destroy; override; // don't forget to create and free FFirst/FSecond.


end;

你可以说实现是我们可以做局部类的唯一方法,或至少创建一个复合类实现了一堆接口,并且拥有一些用于执行实现委托的子属性(被保护或甚至是私有的)。如果您将所有其他内容从包含聚合类的单元中移出,则可以使用非常干净的设计。

You could say Implements is the only way we could do "partial classes", or at least create one composite class that implements a bunch of interfaces, and have a bunch of sub-properties (which are protected or perhaps even private) used to do the "implements" delegation. If you move everything else right out of the unit that contains the aggregating class, you could have a really clean design.

这篇关于实现多个接口的类的清晰度(替代代理):的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆