0

可能的重复:
接口与抽象类(一般 OO)

我可以看到他们在协调开发团队或可能由其他人进一步开发的代码方面的优势。

但如果没有,是否有理由使用它们?如果我省略它们会发生什么?

摘要——我将能够实例化它。没问题。如果这没有意义——我不会。

接口——无论如何,我在派生它的所有类中都声明了该功能。

注意:我不是在问它们是什么。我在问他们是否对协调以外的任何事情都有帮助。

4

4 回答 4

2

Both are what I call contracts and can be used in the following fashion by an individual developer:

Abstract

  • Allows for polymophism of differing derived implementations.
  • Allows one to create base functionality which can be dictated or not that the derived class be required to implement.
  • Allows for a default operation to be runtime consumed if the derived does not implement or required to implement.
  • Provides a consistency across derived objects which a base class pointer can utilize without having to have the actual derived; hence allows generic operations on a derived object from a base class reference similar to an Interface in runtime operation.

Interface

  • Allows a generic pattern of usage as a defacto contract of operation(s). This usage is can be targetted to the process in hand and allows for the surgically precise operations for that contract.
  • Used to help with factory patterns (its the object returned), mocking of data during unit tests and the ability to replace an existing class (say from a factory returning the interface) with a different object and it doesn't cause any consumer of the factory any pain of refactoring due to the adherence of the interface contract.
  • Provides a pattern of usage which can be easily understood away from the static of the rest of the class's implementation.

Long story short are they required to get a job done? No.

But if you are into designing systems which will have a lifespan of more than one cycle, the upfront work by said architect will pay off in the long run whether on a team or by an individual.


++Update

I do practice what I preach and when handing off a project to other developers it was nice to say

  1. Look at the interface IProcess which all the primary business classes adhere to. That process defines a system of goals which can help you understand the purpose and the execution of the business logic in a defined way.
  2. While maintaining and adding new functionality to the project the interfaces actually helped me remember the flow and easily add new business logic into the project.
于 2012-04-04T18:30:21.890 回答
1

抽象 - 你可以实例化它的一个孩子,但更重要的是,它可以有自己的抽象方法和字段。

接口 - 就抽象而言更“粗略”的一种,但.NET您可以拥有多重继承。因此,通过定义接口,您可以引导接口的使用者订阅不同的合约(接口),从而呈现指定类型的不同“形状”。

于 2012-04-04T18:07:57.233 回答
1

我认为如果你不与他人协调,它会做两件事

  1. 有助于防止您对自己的代码做奇怪的事情。想象一下你写了一个类,并在多个项目中使用它。您可以在一个项目中对其进行改进,以使其在另一个项目中的表亲无法识别。拥有一个抽象类或接口会让您在更改函数签名时三思而后行。
  2. 它为您提供了前进的灵活性——这里有很多经典的例子。使用你想要完成的事情的通用形式,如果你决定以后需要一种不同的形式(流式阅读器是一个很好的例子,对吧?)你可以在以后更容易地实现它。
于 2012-04-04T18:12:04.267 回答
0

即使您没有与任何人协调,也有很多理由使用这两种结构。主要用途是两者实际上都有助于表达开发者的意图,这可能会帮助您稍后弄清楚为什么选择您实际选择的设计。它们还可以允许进一步的可扩展性

抽象类允许您定义一个通用实现,该实现将在许多派生类之间共享,同时将一些行为委托给子类。它允许 DRY(不要重复自己)原则,以避免在任何地方重复相同的代码。

接口表示你的类实现了一个特定的契约。这在框架内有一个非常有用的用途,其中:

  • 使用需要实现某些接口的库功能。示例是 IDisposable、IEquatable、IEnumerable...

  • 在泛型中使用约束。

  • 允许模拟接口(如果您进行单元测试)而不必实例化真实对象。

  • COM 对象的使用

于 2012-04-04T18:37:22.430 回答