Visitor (C ++ implementation)

Source: Internet
Author: User

// Visitor. cpp: defines the entry point of the console application.
//

# Include "stdafx. H"
# Include <iostream>
# Include <string>

Using namespace STD;

Class visitor;

Class Element
{
Public:
Virtual ~ Element (){}

Virtual void accept (visitor & rvisitor) = 0;

Protected:
Element (){}
};

Class concreateelementa
: Public element
{
Public:
Concreateelementa (string S): state_a (s)
{

}
Virtual ~ Concreateelementa (){}

Virtual void accept (visitor & rvisitor );

String getstate ()
{
Return state_a;
}

PRIVATE:
String state_a;
 
};

Class concreateelementb
: Public element
{
Public:
Concreateelementb (string S): state_ B (s)
{
}

Virtual void accept (visitor & rvisitor );
String getstate ()
{
Return state_ B;
}
PRIVATE:
String state_ B;
};

Class visitor
{
Public:
Virtual ~ Visitor (){}

Virtual void visitconcreateelementa (concreateelementa * pconcreateelementa) = 0;
Virtual void visitconcreateelementb (concreateelementb * pconcreateelementb) = 0;

Protected:
Visitor (){}
};

Class concreatevisitora
: Public visitor
{
Public:
Virtual ~ Concreatevisitora (){}

Virtual void visitconcreateelementa (concreateelementa * pconcreateelementa)
{
Cout <"++" <pconcreateelementa-> getstate () <"++" <Endl;
}
Virtual void visitconcreateelementb (concreateelementb * pconcreateelementb)
{
Cout <"-----" <pconcreateelementb-> getstate () <"-----" <Endl;
}
};

Class concreatevisitorb
: Public visitor
{
Public:
Virtual ~ Concreatevisitorb (){}

Virtual void visitconcreateelementa (concreateelementa * pconcreateelementa)
{
Cout <"@" <pconcreateelementa-> getstate () + "" <"" <Endl;
}
Virtual void visitconcreateelementb (concreateelementb * pconcreateelementb)
{
Cout <"#####" <pconcreateelementb-> getstate () <"#####" <Endl;
}
};

Void concreateelementa: accept (visitor & rvisitor)
{
Rvisitor. visitconcreateelementa (this );
}

Void concreateelementb: accept (visitor & rvisitor)
{
Rvisitor. visitconcreateelementb (this );
}

Int _ tmain (INT argc, _ tchar * argv [])
{
Visitor * pvisitora = new concreatevisitora ();
Visitor * pvisitorb = new concreatevisitorb ();
Element * pelement = new concreateelementa (string ("XXXXX "));

Pelement-> Accept (* pvisitora );
Pelement-> Accept (* pvisitorb );

Element * pelement2 = new concreateelementb (string ("yyyyy "));

Pelement2-> Accept (* pvisitora );
Pelement2-> Accept (* pvisitorb );

Delete pelement;
Delete pelement2;
Delete pvisitora;
Delete pvisitorb;

Return 0;
}

 

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.