[design mode]<10>. C + + and template method patterns

Source: Internet
Author: User

The Silent Eeer

Original address: http://www.cnblogs.com/hebaichuanyeah/p/6092152.html

Once written in Qt, there is a need to add a custom event to all of the program's interfaces. The event is triggered by a thread.

It is really troublesome to add a single event binding to each interface object.

So I wrote an event's parent interface, binding the thread to an event filter function in an event, and then deciding whether to trigger an event in the event filter, and then invoking an event response function, which is a pure virtual function that is actually implemented in a subclass. And the interface class is inherited from this parent class.

Later, I turned to "design mode" and found that this set of things in fact a "template method" mode.

Intent: Define an operation algorithm skeleton, and put some steps into the subclass implementation.

The implementation of the above event mechanism. The BaseEvent class cannot be instantiated and can only be inherited.

Class Baseevent{public:    baseevent ()    {        Binds the thread that triggered the event to the Eventfiler event filter.    }protected:    virtual void envent () =0;    Event filter    void Eventfiler ()    {        if (current interface gets focus)        {            //the event function is actually implemented in subclasses.            envent ();            Qdebug () << Print current object information.        }    }    }

Another chestnut, in the parent class, has an action function that opens the document, but the document is opened with different algorithms for different formats.

void Baseclass::opendocument (string path) {    if (path does not exist)    {        return;    }    Document * doc =  open (path);    Read (doc);}

In the above code wisdom Oh you, the actual open function, and the read function are all pure virtual functions, for different formats of the document, in different subclasses to implement the specific open and read algorithm.

Previous examples of customizing event base classes in Qt.

[design mode]<10>. C + + and template method patterns

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.