C language and design mode (template Mode)

Source: Internet
Author: User

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

The template is no stranger to students studying C ++. Functions include template functions and templates. So what is the template mode? We can think about the nature of the template. For example, now we need to compile a simple comparison template function.

template <typename type>int compare (type a, type b){    return a > b ? 1 : 0;}    

The template function prompts us that as long as the comparison logic is determined, No matter what data type, a corresponding result will be obtained. Although the comparison process is relatively simple, it does not matter even if the template function is not used. However, what should I do if there are many splitting steps? If the problem persists, you will understand what the template mode is.

For example, now we need to design a process. This process has been completed in many small steps. However, there are a variety of methods for each step, and we can choose from many options. However, the logic of all steps is unique. What should we do? It is also simple. In the base class, all the step functions except the flow functions are set to virtual functions.

class basic{public:    void basic() {}    virtual ~basic() {}    virtual void step1() {}    virtual void step2() {}    void process()    {        step1();        step2();    }};

The basic class indicates that the basic process is unique, so what we need to do is to rewrite Step 1 and step 2.

class data_A : public basic{public:    data_A() {}    ~data_A() {}    void step1()     {        printf("step 1 in data_A!\n");    }    void step2()    {        printf("step 2 in data_A!\n");    }};

Therefore, according to my personal understanding, the template here is mainly a unified process, and the details are separated. Once you understand this idea, it is not difficult to use the C language to describe the template model.

typedef struct _Basic{    void* pData;    void (*step1) (struct _Basic* pBasic);    void (*step2) (struct _Basic* pBasic);    void (*process) (struct _Basic* pBasic);}Basic;

In C ++, the process function is directly inherited. This mechanism is not available in C. Therefore, for each process, the process function is unique, but we still need to copy the function pointer for each operation. While Step 1 and Step 2 are different, so various methods can be used to flexibly modify their processing logic, no problem.

void process(struct _Basic* pBasic){    pBasic->step1(pBasic);    pBasic->step2(pBasic);}

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.