C++设计模式——访问者模式

#include 
#include 
using namespace std;

class concreteElementA;  
class concreteElementB;  
class visitor  
{  
public:  
    visitor(){};  
    virtual ~visitor(){};  
    virtual void visitConcreteElementA(concreteElementA* pConcreteElementA) = 0;  
    virtual void visitConcreteElementB(concreteElementB* pConcreteElementB) = 0;  
};  

class concreteVisitor1 : public visitor  
{  
public:  
    concreteVisitor1(){};  
    ~concreteVisitor1(){};  
    virtual void visitConcreteElementA(concreteElementA* pConcreteElementA)  
    {  
        cout << "concreteElementA被concreteVisitor1访问" << endl;       
    }  
    virtual void visitConcreteElementB(concreteElementB* pConcreteElementB)  
    {  
        cout << "concreteElementB被concreteVisitor1访问" << endl;          
    }  
};  
  
class concreteVisitor2 : public visitor  
{  
public:  
    concreteVisitor2(){};  
    ~concreteVisitor2(){};  
    virtual void visitConcreteElementA(concreteElementA* pConcreteElementA)  
    {  
        cout << "concreteElementA被concreteVisitor2访问" << endl;       
    }  
    virtual void visitConcreteElementB(concreteElementB* pConcreteElementB)  
    {  
        cout << "concreteElementB被concreteVisitor2访问" << endl;          
    }  
};  

class element  
{  
public:  
    element(){};  
    virtual ~element(){};  
    virtual void accept(visitor* pVisitor) = 0;  
};  

class concreteElementA : public element  
{  
public:  
    concreteElementA(){};  
    ~concreteElementA(){};  
    // 充分利用双分派技术,实现处理与数据结构的分离  
    virtual void accept(visitor* pVisitor)  
    {  
        if (NULL != pVisitor)  
        {  
            pVisitor->visitConcreteElementA(this);  
        }  
    }  
    // 其他的相关方法  
    void operationA()  
    {  
        cout << "具体元素A的其他相关方法" << endl;  
    }  
};  
class concreteElementB : public element  
{  
public:  
    concreteElementB(){};  
    ~concreteElementB(){};  
    // 充分利用双分派技术,实现处理与数据结构的分离  
    virtual void accept(visitor* pVisitor)  
    {  
        if (NULL != pVisitor)  
        {  
            pVisitor->visitConcreteElementB(this);  
        }  
    }  
    // 其他的相关方法  
    void operationB()  
    {  
        cout << "具体元素B的其他相关方法" << endl;  
    }  
};  


class objectStructure  
{  
public:  
    void attach(element* pElement)  
    {  
        m_list.push_back(pElement);  
    }  
    void detach(element* pElement)  
    {  
        m_list.remove(pElement);  
    }  
    void accept(visitor* pVisitor)  
    {  
        list::iterator Iter;  
        for (Iter = m_list.begin(); Iter != m_list.end(); ++Iter)  
        {  
            if (NULL != *Iter)  
            {  
                (*Iter)->accept(pVisitor);  
            }              
        }  
    }  
 int  GetSize()
 {
  return m_list.size();
 }
private:  
    list  m_list;  
};  


void freePtr(void* vptr)  
{  
    if (NULL != vptr)  
    {  
        delete vptr;  
        vptr = NULL;  
    }  
}  
int main(int argc, char* argv[])  
{  
    objectStructure* pObject = new objectStructure();  
    if (NULL != pObject)  
    {  
        element* pElementA = new concreteElementA();  
        element* pElementB = new concreteElementB();  
        pObject->attach(pElementA);  
        pObject->attach(pElementB);  
        concreteVisitor1* pVisitor1 = NULL;  
        pVisitor1 = new concreteVisitor1();  
        concreteVisitor2* pVisitor2 = NULL;  
        pVisitor2 = new concreteVisitor2();  
        pObject->accept(pVisitor1);  
        pObject->accept(pVisitor2);  
        system("pause");  
        freePtr(pVisitor2);  
        freePtr(pVisitor1);  
        freePtr(pElementB);  
        freePtr(pElementA);  
        freePtr(pObject);  
    }  
    return 0;  
}  

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