C# - Recommendations for Abstract Classes vs. Interfaces
2014-10-29 14:47
323 查看
The choice of whether to design your functionality as an interface or an abstract class can sometimes be a difficult one. An
abstract class is a class that cannot be instantiated, but must be inherited from. An abstract class may be fully implemented, but is more usually partially implemented or not implemented at all, thereby encapsulating common functionality for inherited
classes.
An interface, by contrast, is a totally abstract set of members that can be thought of as defining a contract for conduct. The implementation of an interface is left completely to the developer.
Both interfaces and abstract classes are useful for component interaction. If a method requires an interface as an argument, then any object that implements that interface can be used in the argument. Abstract
classes also allow for this kind of polymorphism, but with a few caveats:
Classes may inherit from only one base class, so if you want to use abstract classes to provide polymorphism to a group of classes, they must all inherit from that class.
Abstract classes may also provide members that have already been implemented. Therefore, you can ensure a certain amount of identical functionality with an abstract class, but cannot with an interface.
Here are some recommendations to help you to decide whether to use an interface or an abstract class to provide polymorphism for your components.
If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting
classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.
If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces
are best suited for providing common functionality to unrelated classes.
If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.
If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces
contain no implementation for any members.
The choice of whether to design your functionality as an interface or an abstract class can sometimes be a difficult one. An
abstract class is a class that cannot be instantiated, but must be inherited from. An abstract class may be fully implemented, but is more usually partially implemented or not implemented at all, thereby encapsulating common functionality for inherited
classes.
An interface, by contrast, is a totally abstract set of members that can be thought of as defining a contract for conduct. The implementation of an interface is left completely to the developer.
Both interfaces and abstract classes are useful for component interaction. If a method requires an interface as an argument, then any object that implements that interface can be used in the argument. Abstract
classes also allow for this kind of polymorphism, but with a few caveats:
Classes may inherit from only one base class, so if you want to use abstract classes to provide polymorphism to a group of classes, they must all inherit from that class.
Abstract classes may also provide members that have already been implemented. Therefore, you can ensure a certain amount of identical functionality with an abstract class, but cannot with an interface.
Here are some recommendations to help you to decide whether to use an interface or an abstract class to provide polymorphism for your components.
If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting
classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.
If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces
are best suited for providing common functionality to unrelated classes.
If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.
If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces
contain no implementation for any members.
相关文章推荐
- C# - Recommendations for Abstract Classes vs. Interfaces
- Interfaces vs Abstract Classes...
- [Java] Abstract classes vs. interfaces
- Abstract Classes Vs Interfaces
- More Effective C# Item 7. Do Not Create Generic Specialization on Base Classes or Interfaces
- [Tip: C# commenting]Comment in C# for VS
- [Java]--Abstract Class vs Interfaces
- 《CLR Via C#》Developer Command Prompt for VS2013
- c# 中的 for vs foreach
- [转] Difference between Abstract classes and Interfaces
- for之Python vs C#
- Effective Java 18 Prefer interfaces to abstract classes
- Java学习笔记(13)Abstract Classes and Interfaces
- [Tip: C# in VS]Some quick tips for working on C# in VS
- Abstract class pattern for C#
- (转)C#中的 Interfaces (For 初学者们)
- C# Intermediate: Classes, Interfaces and OOP
- Part 33 Difference between abstract classes and interfaces
- Effective Java 18:接口优于抽象类 Prefer interfaces to abstract classes
- C# for VS foreach 性能对比