Design Pattern simple code: template method pattern (catch up with mm)

Source: Internet
Author: User

Author: Yi Yu Tian (http://blog.csdn.net/dylgsy ). You are welcome to post this article, and please keep this information

Template Method mode:

In fact, it is to divide a task into several steps. Each step is a method (also called an atomic operation ). That is to say, I have set a few steps to do one thing (a template is defined), but the customer decides what to do in each step. Corresponding to C ++ is the use of virtual functions. (Therefore, virtual functions are a very important concept in object-oriented systems. Their function is to delay implementation, that is, to provide interfaces ).

Let's take a look at an example-Chase mm. I divided the chase mm into three steps: Know Her, approach her, and touch her (then, can I catch up with her luck, may be your sister, haha). The pseudocode is as follows:
Chase mm
{
Know her;
Approaching her;
Touched her;
}
Note that the preceding three steps have been defined, and the relationship between them cannot be changed. What can be changed is the actual content of each step.

John completes the three steps in this way:
1. Meet her: she used to know her. James Zhang: "Oh, we seem to have met somewhere. "Mm thought:" It's an old man ".
2. Approaching her: James: "Are you interested in studying together at night? "Mm thought:" Sorry, why did all men come here? "
3. She was moved: James: "I want to tell the world that I love you !! "Mm thought:" It's boring. What do you do if you love me? "
As a result, Michael fails.

Yi Yutian completed three steps in this way:
1. Meet her: Yi Yutian said, "The XX in your dormitory is so beautiful !! ". MM thought: "How can this person not pay attention to me? Am I worse than someone else? "(Hey, the jealousy of a woman will be used in this way)
2. Approaching her: Yi Yutian said, "I'm going to run. You fat girl, don't come with me! "Mm said," Well, I just want to go. How is it? ". (In turn)
3. Moved her: Yi Yutian said: "cut your steak for you, busy all day" and cut it carefully. MM thought: "No one ever helped me cut a steak like this... ". Hehe, win and chase. Yi Yutian said with deep affection: "Let me cut your steak for the rest of my life. "Mm: speechless (tears, moved ). (Note: mm should be moved from a small place)
As a result, yiyutian succeeded.

Well, let's get to know the template method mode. Let's take a look at the code below.
Wait, there is another problem here. How can we find atomic operations?
We can apply one sentence:
When you pay attention to it, it is an object. Similarly, when you pay attention to it, it is an atomic operation.

Okay. Let's see how the above instance is expressed in C ++.

# Include <iostream>
Using namespace STD;

// Catch the MM Template
Class catchmmtemplate
{
Public:
Bool catchmm ()
{
// If the number of failures is greater than or equal to 2, there is no such thing.
Int nfailedcnt = 0;
If (! Renshi ())
Nfailedcnt ++;
If (! Jiejin ())
Nfailedcnt ++;
If (! Gandong ())
Nfailedcnt ++;

If (nfailedcnt> = 2)
Return false;
Return true;
}

Char szname [20];
 
Protected:
Virtual bool Renshi () = 0;
Virtual bool jiejin () = 0;
Virtual bool Gandong () = 0;
 
};

Class zhansan: Public catchmmtemplate
{
Public:
Zhansan (char * pszname)
{
Strcpy (szname, pszname );
Cout <"I am" <szname <Endl;
}
 
// You only need to overload three functions.
Protected:
Virtual bool Renshi ()
{
Cout <"oh, we seem to have seen it somewhere. "<Endl;
// If mm does not like it, false is returned.
Return false;
}
Virtual bool jiejin ()
{
Cout <"are you interested in studying together at night? "<Endl;
// If mm does not like it, false is returned.
Return false;
}
Virtual bool Gandong ()
{
Cout <"I want to tell the world that I love you !! "<Endl;
// If mm does not like it, false is returned.
Return false;
}
};

Class Yutian: Public catchmmtemplate
{
Public:
Yutian (char * pszname)
{
Strcpy (szname, pszname );
Cout <"I am" <szname <Endl;
}
// You only need to overload three functions.
Protected:
Virtual bool Renshi ()
{
Cout <"the XX in your dormitory is so beautiful !! "<Endl;
// If you like MM, true is returned.
Return true;
}
Virtual bool jiejin ()
{
Cout <"I'm going to run. You fat girl, don't come with me! "<Endl;
// If you like MM, true is returned.
Return true;
}
Virtual bool Gandong ()
{
Cout <"helping you cut your steak, busy all day" <Endl;
// If you like MM, true is returned.
Return true;
}
};

Void catch (catchmmtemplate * C)
{
If (! C-> catchmm ())
{
Cout <c-> szname <"failed" <Endl;
}
Else
{
Cout <c-> szname <"success" <Endl;
}
}

// Write a romantic love in the script of God
Void main ()
{
Zhansan Z ("James ");
Catch (& Z );
 
Yutian y ("Yi Yu Tian ");
Catch (& Y );
}

// Please copy the code for compilation and execution

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.