C # integration of dynamic and static programming 4: Generic Delegation

Source: Internet
Author: User
Polymorphism

[With the deep understanding of OO, I have realized that the description of polymorphism in this article is not accurate. The latest introduction is: polymorphism is about the substitution relationship between types. This article describes the binding between interfaces and implementations.]

What is polymorphism? One sentence: the same interface is bound to different implementations. Polymorphism allows the program to call different implementations through a unified interface (generalized interface, meaning a specification), thus enhancing the program's expressive ability and flexibility. The most familiar form of polymorphism is the type polymorphism including 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 differences and connections between the two. In C #, inheritance has two functions: 1. Reuse the non-private members and methods of the base class in the derived class; 2. Implement type polymorphism. Function 1 is essentially "Reusable", that is, the derived class inherits and reuses the base class code, and the base class and the derived class are in the same abstraction layer. function 2 is essentially "reused ", that is, the derived class is reused by inheriting the base class/interface name. The base class and the derived class are abstract and specific relationships.

We often call a base class A "parent class" without distinction. This is a concept that deserves discussion. What is "parent-child relationship? The son inherits some of his father's characteristics and emphasizes reuse. The son and his father are at the same abstract level and only reflect the inherited reuse function. "The relationship between animals and dogs" is obviously different from "parent-child relationship". Animals and dogs are abstract and specific relationships, the main purpose of establishing such a relationship in a program is to reuse the basic functions in the abstract name. Therefore, when the main purpose of inheritance is to reuse, we can call the base class at the same Abstraction Level "parent class ", however, when the main purpose of inheritance is to realize the recurrence of Type polymorphism, the "parent class" is inappropriate.

It is precisely because of lack of a clear understanding of the inheritance function that leads to the so-called "misuse of inheritance ". As mentioned above, inheritance is a strong type constraint. When the purpose is only for reuse, if the inheritance method is used, it will often bring some side effects, such as: in C, an object cannot change its inheritance relationship within its lifecycle. A son like Lao Tzu must be like a lifetime. In fact, reuse is not a patent of OO. In almost all cases, we can achieve reuse in other ways, such as combination. Therefore, here is a clear suggestion to avoid misuse of inheritance: when the purpose is to reuse, consider using combinations instead of inheritance!

Generic Delegation

Polymorphism is not necessarily related to inheritance. inheritance is only a means of realizing type polymorphism. I understand that all bindings that comply with interfaces and implementations are polymorphism. Is the delegate in C # polymorphism? Of course, it is called method polymorphism. The delegate caller calls different methods through a unified delegate. Compared with inheritance polymorphism, delegation eliminates type constraints. Any method that complies with the signature can be called by delegation, so it is more flexible. Also, is generic polymorphism? Yes. The compiler of generic classes/methods uses the unified type parameter T to express the behavior of the program and submits the specific work of T to users of generic classes/methods.

If the delegation is multi-state and the generic delegation is multi-state, is the generic delegation multi-state? Of course, it is also very strong polymorphism! Its strength lies in its ability to combine the abstract capabilities of generics and delegation, without losing the security of static types. GoF has long realized how powerful the combination of generics and delegation can be! Fortunately, C # has already directly supported generic delegation at the language level (as far as I know, the C ++ boost Library also provides generic function pointers similar to generic delegation ).

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.