Visitor

#include 

using namespace std;

class ElementA;
class ElementB;

class Visitor
{
public:
    virtual ~Visitor(){}
    virtual void visitElementA(ElementA & element) = 0;
    virtual void visitElementB(ElementB & element) = 0;
};

class Element{
public:
    virtual ~Element(){}
    virtual void accept(Visitor & visitor) = 0;
};


class ElementA : public Element{
public:
    void accept(Visitor & visitor) override{
        visitor.visitElementA(*this);
    }
};

class ElementB : public Element{
public:
    void accept(Visitor & visitor)override{
        visitor.visitElementB(*this);
    }
};



class Visitor1 : public Visitor{
public:
    void visitElementA(ElementA & element) override{
        cout << "Visitor1 is procesdsing ElementA" <

 

你可能感兴趣的:(设计模式)