Look at "Chen Wei Video" Understanding polymorphism mechanism-do you remember "oranges, apples, examples?"

Source: Internet
Author: User

"What do you think of the interface?" "I'll just say decoupling before I get back to this episode of video. Why decoupling, maybe I thought that as long as the b layer and the D layer are interspersed, then b layer will not directly call D layer of the class method, this way to achieve understanding of the effect of decoupling. Later I found that I hit the code of a misunderstanding, that is, I am knocking on the computer room charge system only in accordance with the seven-layer style, each definition of an interface when the implementation of a class, resulting in my understanding is that an interface only corresponds to a class. But is the interface just like this?

I think the Chen Wei Video 49 episode gave me a very good inspiration-polymorphic.

Review

There is a box that just can't get in, fruit has two kinds of apples and bananas, the original weight is 50, 30 respectively. Put in the box, moisture loss, weight reduction, respectively, reduce 4, 3, until the 3/5 is no longer light.

Box function:

1, the total weight of a day ago

2. Output of apples

3. Number of output oranges

4. Output the total weight of the fruit in one day box

5, the current total weight

Analysis

From this example above, we can analyze it by the domain model:

(i) Use cases and roles:

Customer (hidden role), weighing total weight (use case)


(ii) model

Business entities: Customers, boxes, apples, bananas, fruits,

Business rules: 1. The original weights are 50, 30, respectively.

2. Each day is reduced by 4, 3, respectively, until it reaches its own 3/5 is no longer light.

Relationships: Implementing Relationships

(iii) Graph analysis

Principle

( when VB is running )

(C # runtime)

Analysis:

In fact, we really knock this code will find that the client has this paragraph specifically to instantiate Apple and orange code, this is why? Because there is no real interface class in VB, if you use abstract class as an interface class, you need to write a conversion. But when we directly use C # or other knocks, the fruit directly into the interface layer, so this conversion we do not have to write, we can directly through the fruit interface class to achieve the use of Apple or orange class instantiation. Therefore, C # in the runtime than the principle of closer-users in the use of the interface can automatically get the user needs of the class, so the user does not need to know when compiling the apple or orange, know the instantiation of the interface class is good.

Thinking

From the above example, it is not difficult to see, the box usually only need to adjust the fruit interface, until the actual call will be automatically based on your input is the apple or orange to achieve different results. In other words, the implementation of the fruit interface through the realization of apple, orange, the same method of different implementations.

Polymorphism Benefits: Different objects accept the same things to produce different results. In this way we can add different kinds of fruit and don't care what they are when we use them.

Reflection

Many people and I reflect that this stage is not very familiar with polymorphism, from the video I realize that often ask a why, do not just use, but also to know what this is for what.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Look at "Chen Wei Video" Understanding polymorphism mechanism-do you remember "oranges, apples, examples?"

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.