Interface vs Base class

2018-12-31 01:31发布

When should I use an interface and when should I use a base class?

Should it always be an interface if I don't want to actually define a base implementation of the methods?

If I have a Dog and Cat class. Why would I want to implement IPet instead of PetBase? I can understand having interfaces for ISheds or IBarks (IMakesNoise?), because those can be placed on a pet by pet basis, but I don't understand which to use for a generic Pet.

30条回答
不流泪的眼
2楼-- · 2018-12-31 01:57

This is pretty .NET specific, but the Framework Design Guidelines book argues that in general classes give more flexibility in an evolving framework. Once an interface is shipped, you don't get the chance to change it without breaking code that used that interface. With a class however, you can modify it and not break code that links to it. As long you make the right modifications, which includes adding new functionality, you will be able to extend and evolve your code.

Krzysztof Cwalina says on page 81:

Over the course of the three versions of the .NET Framework, I have talked about this guideline with quite a few developers on our team. Many of them, including those who initially disagreed with the guidelines, have said that they regret having shipped some API as an interface. I have not heard of even one case in which somebody regretted that they shipped a class.

That being said there certainly is a place for interfaces. As a general guideline always provide an abstract base class implementation of an interface if for nothing else as an example of a way to implement the interface. In the best case that base class will save a lot of work.

查看更多
素衣白纱
3楼-- · 2018-12-31 01:57

Previous comments about using abstract classes for common implementation is definitely on the mark. One benefit I haven't seen mentioned yet is that the use of interfaces makes it much easier to implement mock objects for the purpose of unit testing. Defining IPet and PetBase as Jason Cohen described enables you to mock different data conditions easily, without the overhead of a physical database (until you decide it's time to test the real thing).

查看更多
只若初见
4楼-- · 2018-12-31 01:58

Interfaces

  • Defines the contract between 2 modules. Cannot have any implementation.
  • Most languages allow you to implement multiple interfaces
  • Modifying an interface is a breaking change. All implementations need to be recompiled/modified.
  • All members are public. Implementations have to implement all members.
  • Interfaces help in Decoupling. You can use mock frameworks to mock out anything behind an interface
  • Interfaces normally indicate a kind of behavior
  • Interface implementations are decoupled / isolated from each other

Base classes

  • Allows you to add some default implementation that you get for free by derivation
  • Except C++, you can only derive from one class. Even if could from multiple classes, it is usually a bad idea.
  • Changing the base class is relatively easy. Derivations do not need to do anything special
  • Base classes can declare protected and public functions that can be accessed by derivations
  • Abstract Base classes can't be mocked easily like interfaces
  • Base classes normally indicate type hierarchy (IS A)
  • Class derivations may come to depend on some base behavior (have intricate knowledge of parent implementation). Things can be messy if you make a change to the base implementation for one guy and break the others.
查看更多
琉璃瓶的回忆
5楼-- · 2018-12-31 01:59

Also keep in mind not to get swept away in OO (see blog) and always model objects based on behavior required, if you were designing an app where the only behavior you required was a generic name and species for an animal then you would only need one class Animal with a property for the name, instead of millions of classes for every possible animal in the world.

查看更多
素衣白纱
6楼-- · 2018-12-31 02:01

Interfaces and base classes represent two different forms of relationships.

Inheritance (base classes) represent an "is-a" relationship. E.g. a dog or a cat "is-a" pet. This relationship always represents the (single) purpose of the class (in conjunction with the "single responsibility principle").

Interfaces, on the other hand, represent additional features of a class. I'd call it an "is" relationship, like in "Foo is disposable", hence the IDisposable interface in C#.

查看更多
梦该遗忘
7楼-- · 2018-12-31 02:03

Interfaces should be small. Really small. If you're really breaking down your objects, then your interfaces will probably only contain a few very specific methods and properties.

Abstract classes are shortcuts. Are there things that all derivatives of PetBase share that you can code once and be done with? If yes, then it's time for an abstract class.

Abstract classes are also limiting. While they give you a great shortcut to producing child objects, any given object can only implement one abstract class. Many times, I find this a limitation of Abstract classes, and this is why I use lots of interfaces.

Abstract classes may contain several interfaces. Your PetBase abstract class may implement IPet (pets have owners) and IDigestion (pets eat, or at least they should). However, PetBase will probably not implement IMammal, since not all pets are mammals and not all mammals are pets. You may add a MammalPetBase that extends PetBase and add IMammal. FishBase could have PetBase and add IFish. IFish would have ISwim and IUnderwaterBreather as interfaces.

Yes, my example is extensively over-complicated for the simple example, but that's part of the great thing about how interfaces and abstract classes work together.

查看更多
登录 后发表回答