Abstract class or interface. Which way is correct?

孤者浪人 提交于 2021-02-17 22:17:38

问题


There are two way for choosing between abstract class or interface. Microsoft solution and Oracle solution:


Microsoft, design guideline:

Do use abstract (MustInherit in Visual Basic) classes instead of interfaces to decouple the contract from implementations.

http://msdn.microsoft.com/en-us/library/ms229013.aspx


Oracle, The Java Tutorials:

If an abstract class contains only abstract method declarations, it should be declared as an interface instead.

http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html


My question is which way is correct? Microsoft or Oracle solution? Note that I think choose between abstract class or interface should not depends on programming language (Java or C#).


回答1:


If I recall my blog reading correctly, the Microsoft advice to use abstract classes stems from the ability to reuse implementation with an abstract class, something you can't do with an interface.

Note also that the Microsoft page you linked to is specifically guidance for writing code libraries for sharing/reuse across multiple projects. The likelihood in this situation is that you'll be writing all the implementations of the interface yourself, probably within the same assembly. Good practices for working on a single product or system will vary somewhat.

One common approach that I've seen across a number of codebases in a number of languages is this:

  • Define an interface to specify the contract
  • Create an abstract class implementing the contract to provide any common implementation useful to all descendants
  • Implementations of the contract then have the option to start from the base class for convenience, or just to implement the interface if they want full control

A fourth step common in the .NET world is to provide convenience extension functions built on the interface.




回答2:


They are 2 statements for different contexts.

The Microsoft guideline you quote from is for "Designing Class Libraries". And it states the reason for favoring abstract classes there: you can add functionality without breaking anything.

For separation and decoupling over layers and other boundaries, Microsoft also advices interfaces.




回答3:


An interface carries no implementation - it's a contract. It allows for complete decoupling.

I will go from an interface to an abstract (base) class if I want to provide some common implementation while forcing on the inheriting class concrete class to provide some implementation specific to that class. That's provides a little less decoupling.

Also be aware that many languages like C# (and .net languages like VB.net etc...) as well as Java do not allow multiple inheritance so interfaces become a way of allowing a class to have many behaviors.



来源:https://stackoverflow.com/questions/9243589/abstract-class-or-interface-which-way-is-correct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!