C # static and dynamic combination programming of the four: generic delegate

Source: Internet
Author: User
Tags abstract inheritance

Polymorphic

What is polymorphism? Word: interface and implementation of the 1:N mapping. Polymorphism enables programs to invoke different implementations through a unified interface (a generalized interface, meaning specification), thereby enhancing the program's expressive power and flexibility. The polymorphic form we are most familiar with is a type polymorphism that includes interface inheritance:

var animals = new List<IAnimal>() {
         new Cat("Missy"),
         new Cat("Mr. Bojangles"),
         new Dog("Lassie")
       };
       foreach (var animal in animals) {
         Console.WriteLine(animal.Name + ": " + animal.Talk());
       }

Sometimes, we often confuse the relationship between inheritance and polymorphism, and even think that the two are different aspects of the same concept. Here I try to clarify the difference and connection between the two. In C #, inheritance has two functions: 1. Non-private members and methods of the derived class to reuse the base class; 2. Implement type Polymorphism. The essence of feature 1 is "reuse", that is, a derived class inherits the base class code by inheriting, the base class is at the same abstraction level as the derived class; the essence of feature 2 is "reused", that is, derived classes are reused by inheritance in the name of the base class/interface, and the base class and derived classes are abstract and specific.

We often refer to "base class" (base Class) at random as "parent", without distinction, which is questionable in concept. What is a "parent-child relationship"? The son inherited some characteristics of his father, emphasizing the reuse, the son and father is the same abstract level of things, only embodies the inherited function of reuse. "The relationship between animals and dogs" is clearly different from the "parent-child relationship", where animals and dogs are abstract and specific, and the main purpose of this relationship in the program is to be reused in the name of abstraction, rather than the reuse of base-class functions. Therefore, when the primary purpose of inheritance is to reuse, we can call the base class at the same level of abstraction a "parent class", and when the primary purpose of inheritance is to implement type polymorphism, the reference to the "parent class" is defective.

It is due to the lack of a clear understanding of the two aspects of the inheritance function, which leads to the so-called "abuse of inheritance". As mentioned earlier, inheritance is a very strong type constraint, when the purpose is only to reuse, if the use of inheritance, often bring some side effects, such as: In C #, the object in its life cycle can not change the inheritance relationship, son like Lao Tze must be like a lifetime. In fact, reuse is not OO patent, in almost every situation, we can use other ways to achieve reuse, such as the combination of methods. So here's a clear suggestion to avoid abusive inheritance: when the purpose is to reuse, consider using a combination rather than inheritance!

Generic delegate

Polymorphism and inheritance are not necessarily connected, and inheritance is just a means of realizing type polymorphism. I understand that all 1:n mappings that conform to interfaces and implementations belong to polymorphism. is the delegate in C # not polymorphic? Of course, I would call it a method polymorphism, and the caller of the delegate invokes a different method of implementation through a unified delegate. Compared with inherited polymorphism, delegates eliminate type constraints, and any method that conforms to the signature can be invoked by delegates and therefore more flexible. Also, generics are not polymorphic? Also. The creator of a generic class/method gives T-materialized work to the user of the generic class/method by using the uniform type parameter T expression program's behavior.

Delegates are polymorphic, generics are polymorphic, so is generic delegates polymorphic? Of course it is, and it is very powerful polymorphism! Its strength is the integration of generics and delegation of the abstract ability, but also without losing static type of security. Gof has long been aware of how powerful and abstract the generics can be with delegates. Fortunately, C # has already supported generic delegates directly at the language level (as far as I know, the C + + Boost library also provides generic function pointers similar to generic delegates).

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.