Object-Oriented Design-coordination and Inverter

Source: Internet
Author: User

In the object-oriented design, we have always pursued a kind of result, that is, good reusability. Based on this concept, the object-oriented design includes covariance and contravariance) here are two concepts.


Introduction:

Covariant: changes from subclass to parent class, marked with the out keyword
Invert: changes from parent class to subclass, with the in keyword

For example, animal is a parent class, and dog is a subclass inherited from animal. If an object type is dog, it must be animal. One method to get pets must accept the dog parameter, and the other method to accept the animal parameter can certainly accept the parameter of this method. This is the inverse of animal's transformation to dog. If the returned value of a method is animal, the method that returns dog must meet the returned value. This is a covariant of the dog-to-animal direction.


Code example: the following three instances are different applications for the degree to which the covariant and inverter are abstracted. They are also intended to be understood through these three instances, A technology brings about different changes at different abstract levels. Whether it is a cool or an extra one, please be careful!
I. array covariant:

<span style="font-size:18px;">Animal[] animalArray = new Dog[]{};</span>

The declared array data type is animal. In fact, the assigned value is an array of dog data type, and every dog object can be safely converted to animal. The transformation from dog to animal method is a transformation in the direction of parent-child inheritance, which is called covariant. The application of covariant is often used in the design of program code!


Ii. Coordination and inverter in Delegation


1. covariant in Delegation
<Span style = "font-size: 18px;"> // the return value of the delegate definition is of the animal type ~ Parent class // get the pet Public Delegate animal getpet (); // the return value in the delegate method implementation is dog, a subclass // get the dog static dog getdog () {return new dog ();} // The returned value of getdog is dog, and dog is a subclass of animal. Returning a dog is equivalent to returning an animal; therefore, the assignment to the next assignment is valid. // the assignment to get the PET is added to get the dog. This is a covariant, is legal </span> <PRE name = "code" class = "CSHARP"> <span style = "font-size: 18px; "> getpet <span style =" font-family: Arial, Helvetica, sans-serif; "> getmethod = getdog; </span>
 


2. Inverter in Delegation
<Span style = "font-size: 18px;"> // The parameter type defined in the delegate is dog // dog Public Delegate void feedpet (DOG target ); // In the actual method, the parameter type is animal // The static void feedanimal (animal target) {} // feedanimal is an effective method entrusted by feeddog, because the type of the parameter accepted by the delegate is dog and the dog can be implicitly converted to animal, the delegate can safely perform type conversion and correctly execute the delegate method; // pet feeding can be changed to animal feeding, this is an inverter and is also legal </span> <PRE name = "code" class = "CSHARP"> <span style = "font-size: 18px; "> feedpet <span style =" font-family: Arial, Helvetica, sans-serif; "> feeddogmethod = feedanimal; </span>
 

Iii. Coordination and inverter of generic delegation:


1. covariant in generic delegation
The following Commission statement:
<Span style = "font-size: 18px;"> // The generic delegate to be searched: Out indicates a possible covariant, T is the identifier Public Delegate t find <out T> (); </span>

The find delegate returns an instance of the generic type T. There is an out keyword in the angle brackets of the generic type. This keyword indicates that the T type may be subject to covariant.


<Span style = "font-size: 18px;"> // declare find <dog> delegate find <dog> finddog = () => new dog (); // declare the find <animal> delegate and assign the finddog value to findanimal. The type T is converted from dog to animal and the type T is changed to find <animal> findanimal = finddog; </span>


2. Inverter in generic delegation

Delegate statement:

<Span style = "font-size: 18px;"> // The feeding generic delegate: In indicates that the inverter may be involved, T is the identifier Public Delegate void feed <in T> (T target); </span>


The feed delegate accepts a generic type T. Note that there is an in keyword in the angle brackets of the generic type. This keyword is used to tell the compiler that the type T may be used for inverter when assigning values to the delegate.


<Span style = "font-size: 18px;"> // declare a delegated feed whose T is animal <animal> feedanimalmethod = A => console. writeline ("feed animal Lambda"); // assign the T-as-animal Delegate to the T-as-dog delegate variable. This is legal because the in keyword is used to define a generic delegate, if you remove the in keyword, the compiler considers it illegal to feed <dog> feeddogmethod = feedanimalmethod; </span>


Summary:

In fact, covariant and inverter are not a new concept. In the past, we used more covariant and less inverter, especially in generic and delegated use, after so much research, we found that this technology improves the reusability of our code on a certain basis. We do not need to establish a method for each class to use covariant and inverter, we can do a lot of reusable design!

This is actually a good understanding in life. What a father can do will be taught to his son. So where a father appears, the son can appear. The legendary tiger father has no dogs, where the son appears, the father will choose to appear, because the father will give his gifts, but the son may have other different things!

No wonder the teacher often said that some people's code is like a heap of firewood, while some people's code is like a work of art. This is not just a matter of code neat, but an issue of abstraction and design philosophy!

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.