Effective C # Principle 19: Choose to define and implement interfaces rather than inherit

Source: Internet
Author: User
Tags abstract inheritance

Abstract classes provide a general "ancestor" in the inheritance of a class. An interface describes an atomic-level generic feature that can be implemented by other types. Different, but not the same. An interface is a contractual design: a type that implements the type of an interface must implement some expected methods. Abstract classes provide a general, abstract method for a collection of related classes. These are old fashioned things: it's like this, inheritance means that it's something (is a,), and the interface is that it has a function (behaves)! These cliches have been around for a long time, because they provide a description, and they differ in two structures: The base class describes what the object is, and the interface describes the behavior of the object.

An interface describes a set of functions, or a contract. You can create any placeholder elements in the interface (placeholder, which is defined first and then implemented later): methods, attributes, indexers, and events. The type of this interface for any implementation type must provide specific content for each element in the interface. You must implement all the methods, provide all the property accessors, indexers, and define all the events in the interface. You mark and construct reusable behavior in an interface. You can take the interface as a parameter or return value, and you can have more opportunities to reuse the code because different types can implement the same interface. More than that, developers can implement interfaces more easily than deriving from the base class you create. (No, not necessarily!)

You cannot provide a specific implementation of any member in the interface, no matter what, the interface can not be implemented. And the interface cannot contain any specific data members. You are defining a contract in which all types implementing the interface should be implemented.

Abstract base classes can provide specific implementations for derived classes and also describe some common behavior. You can describe data members in more detail, specific methods, implement virtual functions, attributes, events, and indexers. A base class can provide only partial implementations of the method, providing only a few public reusable concrete implementations. The elements of an abstract class can be virtual, abstract, or non-virtual. An abstract class can provide a workable implementation for the specific behavior, while the interface is not.

There is another benefit to reusing these implementations: If you add a method to the base class, all derived classes automatically add this method implicitly. This means that the base class provides an efficient way to extend the behavior of several (derived) types at any time: by adding and implementing methods to the base class, all derived classes immediately have these behaviors. Adding a method to an interface destroys all classes that originally implemented the interface. These classes do not contain new methods and can no longer be compiled. All of the implementations must be updated to add new methods.

The two modes can mix and reuse some of the implementation code, while also implementing multiple interfaces. System.Collections.CollectionBase is an example of a class that provides a base class. You can use this base class for your customers to provide some. NET missing security collection. For example, it has implemented several interfaces for you: IList, ICollection, and IEnumerable. In addition, it provides a protected method that you can overload to provide your own defined behavior for different uses. The IList interface contains an insert () method that adds a new object to the collection. To better provide an implementation of an Insert method, you can handle these events by overloading the CollectionBase class's OnInsert () or Oninsertccomplete () virtual methods:

public class IntList:System.Collections.CollectionBase
{
protected override void OnInsert (int index,    Object value
{
Try
{
int newvalue = System.Convert.ToInt32 (value);
Console.WriteLine ("Inserting {0} at position {1}",
Index. ToString (), value. ToString ());
Console.WriteLine ("List Contains {0} items",
this.) List.Count.ToString ());
}
catch (FormatException e)
{
throw new ArgumentException (
"Argument Type not a Integ Er ",
" value ", e);
}
}
protected override void Oninsertcomplete (int index,
Object value)
{
Console.writel INE ("Inserted {0} at position {1}",
Index. ToString (), value. ToString ());
Console.WriteLine ("List Contains {0} items",
this.) List.Count.ToString ());
}
}
public class Mainprogram
{
public static void Main ()
{
Intlist L = new intlist (); IList il = l As IList;
IL. Insert (0,3);
IL. Insert (0, "This are Bad");
}
}

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.