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:
|
2021-04-04 13:09:08 +00:00
|
|
|
|
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:
|
2021-04-04 13:09:08 +00:00
|
|
|
|
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
|