C ++ visitor Mode

Source: Internet
Author: User
/** Visitor mode: The operation is independent of the class, and the class accepts the corresponding visitor according to the operation you need. The advantage of doing so is that if you need to implement a new operation, the structure of the class does not need to be changed, especially the operation at the class level. If you want to change, the cost is relatively high. By using the visitor mode, you can ensure that the added operations are simple and convenient, and comply with the OCP. The visitor mode has a scary concept: Double dispatch. In fact, the so-called double dispatch only means that to define an operation, two objects are required to decide, these two objects are element objects and visitor objects. For example, the execution results of elementa objects accepting visitora objects are different from those of visitorb objects. The following example shows the meaning of "Double dispatch. In fact, you don't have to think too much about it. The class of the structure relationship like below is "Double dispatch": What kind of structure relationship? It is not that the visit function uses element as the parameter, but the accept function uses visitor as the parameter! Isn't it actually mutual dependency? */# Include <iostream> # include <string> using namespace STD; Class element; // The visitor class and its subclass class visitor {public: Virtual void visit (element * E) = 0 ;}; class visitora: public visitor {public: Virtual void visit (element * E) {cout <"perform operation a" <Endl ;}; class visitorb: public visitor {public: Virtual void visit (element * E) {cout <"execute Operation B" <Endl ;}; // element class and its subclass class element {P Ublic: Virtual void accept (visitor * V) {cout <m_name; V-> visit (this) ;}string & getname () {return m_name;} protected: string m_name ;}; class elementa: public element {public: elementa (string s) {m_name = s ;}}; class elementb: public element {public: elementb (string S) {m_name = s ;}}; // test the void main () {elementa EA ("element a"); elementb EB ("Element B "); visitora Va; visitorb Vb; // double dispatch, The "recipient" and "visitor" determine an operation for EA. accept (& VA); EA. accept (& VB); EB. accept (& VA); EB. accept (& VB);}/** when a visitor is used, we can see that if you need to implement a new operation in the element class, we do not need to modify the structure of the class, for example, if the class is encapsulated in DLL or com, we don't need to re-compile it. Instead, we only need to define a new visitor, for example, visitorc. In this way, you can easily add new operations for the class. */

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.