DesignPattern/24.VisitorPattern/2.Code/Demo.h

61 lines
991 B
C
Raw Normal View History

2019-11-10 14:59:08 +00:00
#ifndef __DEMO_H__
#define __DEMO_H__
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Visitor
class Visitor
{
public:
virtual ~Visitor() {}
2019-11-10 14:59:08 +00:00
virtual void visit(ConcreteElementA*) = 0;
virtual void visit(ConcreteElementB*) = 0;
};
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ConcreteVisitor
class ConcreteVisitor :public Visitor
{
public:
// ʵ<><CAB5>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD>Ԫ<EFBFBD>صķ<D8B5><C4B7>ʲ<EFBFBD><CAB2><EFBFBD>
void visit(ConcreteElementA*){
// Ԫ<><D4AA>A<EFBFBD>ķ<EFBFBD><C4B7>ʲ<EFBFBD><CAB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
void visit(ConcreteElementB*){
// Ԫ<><D4AA>B<EFBFBD>ķ<EFBFBD><C4B7>ʲ<EFBFBD><CAB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
};
// <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
class Element
{
public:
virtual ~Element() {}
2019-11-10 14:59:08 +00:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󷽷<EFBFBD><F3B7BDB7><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߵ<EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
virtual void accept(Visitor*) = 0;
};
// <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
class ConcreteElement :public Element
{
public:
void accept(Visitor* visitor){
visitor->visit(this);
}
};
// <20><><EFBFBD><EFBFBD><EFBFBD>
class ObjectStructure
{
public:
// <20><EFBFBD>ӿڽ<D3BF><DABD>ܷ<EFBFBD><DCB7><EFBFBD><EFBFBD>߷<EFBFBD><DFB7><EFBFBD>
void accept(Visitor* visitor){
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>Ԫ<EFBFBD><D4AA>
for (){
elementList[i]->accept(visitor);
}
}
void addElement(){}
void removeElement(){}
private:
lsit<Element*>elementList;
};
#endif