我什么时候应该设计C#类库时选择了一个多接口继承? [英] When should I choose inheritance over an interface when designing C# class libraries?

查看:177
本文介绍了我什么时候应该设计C#类库时选择了一个多接口继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我公司拥有一批处理器类,会做两件完全不同的事情,但是从普通code(一种局面控制反转)被称为

I have a number Processor classes that will do two very different things, but are called from common code (an "inversion of control" situation).

我不知道我应该cognicent什么设计考虑(或认识到,为你USsers)决定时,如果他们都应该从 BaseProcessor ​​继承,或实施 IProcessor ​​作为接口。

I'm wondering what design considerations I should be cognicent (or cognizant, for you USsers) of when deciding if they should all inherit from BaseProcessor, or implement IProcessor as an interface.

推荐答案

一般情况下,规则是这样的:

Generally, the rule goes something like this:


  • 继承描述了是-A 的关系。

  • 实现接口描述的可以做的关系。

  • Inheritance describes an is-a relationship.
  • Implementing an interface describes a can-do relationship.

为了把这个在某种程度上更具体,让我们来看一个例子。在<一个href=\"http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx\"><$c$c>System.Drawing.Bitmap类的是安图片(正因为如此,它从图片类继承)也有,但是它的可以做处理,所以它实现了 的IDisposable 接口。它还可以做序列化,所以它从<一个实现href=\"http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.aspx\"><$c$c>ISerializable接口。

To put this in somewhat more concrete terms, let's look at an example. The System.Drawing.Bitmap class is-an image (and as such, it inherits from the Image class), but it also can-do disposing, so it implements the IDisposable interface. It also can-do serialization, so it implements from the ISerializable interface.

但是更实际,接口经常被用来模拟在C#多重继承。如果你的处理器类需要从像继承 System.ComponentModel.Component ,那么你别无选择,只能实施一个 IProcessor ​​接口。

But more practically, interfaces are often used to simulate multiple inheritance in C#. If your Processor class needs to inherit from something like System.ComponentModel.Component, then you have little choice but to implement an IProcessor interface.

事实上,两个接口和抽象基类提供一份合同,指定什么特定的类可以做。这是一个普遍的神话,接口都需要声明本合同,但这是不正确的。在我看来,最大的优点是抽象基类,可以为子类提供默认功能。但如果这是有道理没有默认的功能,没有什么让你从标记方法本身为摘要,要求派生类实现它自己,就像如果他们实现一个接口。

The fact is that both interfaces and abstract base class provide a contract specifying what a particular class can do. It's a common myth that interfaces are necessary to declare this contract, but that's not correct. The biggest advantage to my mind is that abstract base classes allow you provide default functionality for the subclasses. But if there is no default functionality that makes sense, there's nothing keeping you from marking the method itself as abstract, requiring that derived classes implement it themselves, just like if they were to implement an interface.

有关回答这样的问题,我经常求助于 .NET框架设计指南中,有该说一下类和接口之间进行选择:

For answers to questions like this, I often turn to the .NET Framework Design Guidelines, which have this to say about choosing between classes and interfaces:

在一般情况下,类是preferred构建露出抽象。

In general, classes are the preferred construct for exposing abstractions.

接口的主要缺点是,他们比类灵活得多,当涉及到允许的API的进化。一旦你船的接口,一组成员被永远定格。该接口的任何增加将打破实现接口现有类型。

The main drawback of interfaces is that they are much less flexible than classes when it comes to allowing for the evolution of APIs. Once you ship an interface, the set of its members is fixed forever. Any additions to the interface would break existing types implementing the interface.

一个类提供了更大的灵活性。您可以添加成员,你已经运类。只要该方法不抽象(即,只要提供的方法的缺省的实现),现有的任何派生类继续起作用不​​变。

A class offers much more flexibility. You can add members to classes that you have already shipped. As long as the method is not abstract (i.e., as long as you provide a default implementation of the method), any existing derived classes continue to function unchanged.

[。 。 。 ]

之一赞成接口最常见的论据是,它们允许分开执行合同。然而,争论错误地假定你不能单独使用类从执行合同。居住在从他们的具体实现一个单独的程序抽象类来实现这种分离的好方法。

One of the most common arguments in favor of interfaces is that they allow separating contract from the implementation. However, the argument incorrectly assumes that you cannot separate contracts from implementation using classes. Abstract classes residing in a separate assembly from their concrete implementations are a great way to achieve such separation.

他们一般建议如下:


  • 不要青睐定义了接口的类。

  • 不要使用抽象类,而不是接口解耦实现合同。抽象类,如果正确定义,允许在同一个程度合同与执行之间的解耦。

  • 不要如果您需要提供一个值类型的多态层次定义一个接口。

  • 考虑定义接口,以达到类似的效果,以多重继承的。

  • Do favor defining classes over interfaces.
  • Do use abstract classes instead of interfaces to decouple the contract from implementations. Abstract classes, if defined correctly, allow for the same degree of decoupling between contract and implementation.
  • Do define an interface if you need to provide a polymorphic hierarchy of value types.
  • Consider defining interfaces to achieve a similar effect to that of multiple inheritance.

克里斯·安德森的前presses利用这最后承租人特定协议,理由是:

Chris Anderson expresses particular agreement with this last tenant, arguing that:

抽象类型做的版本要好得多,并允许将来的扩展,但他们也烧掉你的唯一基本类型。接口是在适当的时候,你真的确定两个物体之间的合同不随时间不变。抽象基类是定义为家庭的类型的通用基更好。

Abstract types do version much better, and allow for future extensibility, but they also burn your one and only base type. Interfaces are appropriate when you are really defining a contract between two objects that is invariant over time. Abstract base types are better for defining a common base for a family of types.

这篇关于我什么时候应该设计C#类库时选择了一个多接口继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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