Negative tive C # item19: defines and implements interfaces that are better than inheritance types

Source: Internet
Author: User

This topic is not just. net, in other object-oriented language environments, such as Java, there will be interfaces and abstract classes. There have been too many discussions about whether to choose an interface or abstract class, there is a design principle in both the design patterns: combination is superior to inheritance. Although this principle is not closely related to the topic to be discussed, we can see how to make a choice in this regard, there are no 10 thousand solutions available. Generally, You need to refer to the recruitment and splitting Measures to analyze specific problems.

Abstract classes are generally at the top of a class hierarchy. They generally have two purposes: 1) define the logical skeleton; 2) extract common methods. It provides a public abstraction for a set of related types, while interfaces are a contract-based design method and an interface-implemented type, methods defined in interfaces must be implemented.

It inherits a "is a" relationship, while interface implementation is a "behave as" relationship. abstract classes describe what objects are, while Interfaces describe the behavior of objects.

An interface cannot contain an implementation or any specific data member. An interface declares a contract, and all types of Implemented interfaces must fulfill their conventions.

Abstract classes can provide specific implementation for the derived type. We can specify the implementation, attributes, events, and indexer of data members, specific methods, and virtual methods. It can also implement some specific methods, so it can provide some common reusable code for sub-classes. Abstract classes can provide an implementation for any specific behavior. In this respect, interfaces are not allowed.

Choosing between abstract base classes and interfaces is actually a question about how to better support abstraction over time.The interface features stability: we encapsulate a set of functions in the interface as implementation contracts of other types, while the base class can be expanded over time, these extensions are part of each subclass.

In C #, we can use abstract classes and interfaces together, inherit a class, and then implement multiple interfaces.

An object-oriented design principle is interface-oriented programming. Therefore, whether an interface or an abstract class is used to transmit parameters, the abstract type or interface type is used, it is better than directly using the derived type or implementing the interface type, in the extended space.

When we use a class as the transmission mode in the process of passing parameters, we actually expose the interface of the entire class to the outside world by using the interface, we can choose to provide only the methods and attributes expected to the user. The class used to implement the interface is the implementation details and will change over time. (I personally think this is mainly because. NET Single inheritance, multi-interface mechanism, if it is a single inheritance, single interface mechanism, even in the process of passing values using interfaces, the interface method will also be exposed to the outside world ).

Let's take a look at the following code.

Code

  1     public interface InterfaceA
2 {
3 bool MethodA();
4 }
5
6 public interface InterfaceB
7 {
8 bool MethodB();
9 }
10
11 public abstract class BaseClass
12 {
13 public abstract void AbstractMethodA();
14 }
15
16 public class DerivedClass : BaseClass, InterfaceA, InterfaceB
17 {
18 public override void AbstractMethodA()
19 {
20 throw new NotImplementedException();
21 }
22
23 #region InterfaceA Members
24
25 public bool MethodA()
26 {
27 throw new NotImplementedException();
28 }
29
30 #endregion
31
32 #region InterfaceB Members
33
34 public bool MethodB()
35 {
36 throw new NotImplementedException();
37 }
38
39 #endregion
40 }

The code above defines two interfaces, an abstract class, and a specific class that inherits the self-abstract class to implement the two interfaces. For the difference between using classes and using interfaces in the process of passing values, see the following code.

Code

  1         private static void TestWithInterface(InterfaceA interfaceA)
2 {
3 interfaceA.MethodA();
4 }
5
6 private static void TestWithAbstractClass(BaseClass baseClass)
7 {
8 baseClass.AbstractMethodA();
9 }
10
11 private static void TestWithClass(DerivedClass derivedClass)
12 {
13 derivedClass.MethodA();
14 derivedClass.MethodB();
15 derivedClass.AbstractMethodA();
16 }

We have defined three methods for testing. These three methods use interfaces, abstract classes, and specific classes when passing values. We can see that parameters are passed using interfaces and abstract classes, you can only access methods defined in interfaces or abstract classes, and use a specific class to pass parameters. It can access methods in interfaces or methods in abstract classes.

Therefore, if we need to publish only a part of the function to the caller, we should use the abstract class or interface to pass the value.

 

Summary: The base class describes and implements a group of related types of common behaviors. Interfaces describe a group of closely related functions for other unrelated types. Both of them have their own applicability. Classes define the types we want to create, and Interfaces describe the types of behavior in the form of feature groups. If we understand the difference, we can create a more expressive and responsive design. You should use the class hierarchy to define related types, and then let them implement different interfaces to provide functions to the outside world through interfaces.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.