Replacing conditional expressions with polymorphism

Source: Internet
Author: User

1. Case studies

Well, it's been a really hot day, anyway, I've been watching my big breasts all night.

We see that the live room is roughly divided into 3 categories of audiences:

1. General Audience

2. Housing

3. Super Tube

How do you differentiate between these three audiences? We found that the average audience only shows their nickname, in front of their nickname there is an eye-catching "housing" mark, of course, the nickname of the Super Tube has a prominent "super-tube" mark. Watching live on the hope that they have a mark, because this can be installed 13 ...

2. Conditional expressions

Now, we write code to differentiate these 3 audiences.

1. Write 3 classes.
Class commonaudience;//general Audience class housemanager;//class supermanager;//Super Tube
2. After the browser-side user logs in, a member variable is used to mark the user's identity.
String strusertype;//User Type
3. When the user sends the barrage, it uses the conditional selection statement to determine the user type and decide which tag method to use.
if (Strusertype = = "Ordinary Audience") {//Don't charge money you want to pretend to be forced? Commonaudience->showmark ("");} else if (Strusertype = = "Housing") {Housemgr->showmark ("housing");} else if (Strusertype = = "Super Tube") {Supermgr->showmark ("super Tube");}

Well, let's think about it, it's a bit bad to judge every time the bomb screen is made.

Is there any way to avoid judgment? First observe these three objects, ordinary audience, housing, super-tube, although the type is not the same, but there is a certain connection, they are our PANDATV users oh.

Thus, in C + +, the role of polymorphism is to take different behaviors based on the different types of objects.

Recalling the use of polymorphism, declaring a pointer to a base class, using that pointer to any subclass object, calling the corresponding virtual function, you can implement different methods according to the different sub-classes that are pointed to.

3. polymorphic

The code is refactored as follows:

1. Create a user base class
Class user{virtual void Showmark ();};

Declare a user-sensitive function for sending the barrage, note that the virtual function, why use virtual function please Baidu.

2. Create the three subclasses that inherit it, implement different contents of Showmark
Class Commonaudience:user{void Showmark () {/* Don't show anything */}};class housemanager:user{void Showmark () {/* Show housing tag */}};class Supermanager:user{void Showmark () {/* Displays the super-tube Tag */}};
3. After the user logs in, create the appropriate user object based on the user type

Like what

User *user = new Housemanager;user->showmark ();

In this case, each time the bomb screen sent, do not judge the user type, just call the Showmark () function, you can display the nickname "2 words!"

Using polymorphic substitution conditional expressions does not mean that you can completely replace, "the conditionals is getting pushed up to the top of the chain." For example, when you log in, you need to determine what kind of user you are. This only needs to be judged once, each time after the sending of the barrage no longer need to judge.

The above is purely entertainment!

Replacing conditional expressions with polymorphism

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.