常見設計模式的解析和實現(C++)之二十-Visitor模式
阿新 • • 發佈:2018-12-27
作用:
表示一個作用於某物件結構中的各元素的操作.它使你可以在不改變各元素的類的前提下定義作用於這些元素的新操作.
UML結構圖:
解析:
Visitor模式把對結點的訪問封裝成一個抽象基類,通過派生出不同的類生成新的訪問方式.在實現的時候,在visitor抽象基類中聲明瞭對所有不同結點進行訪問的介面函式,如圖中的VisitConcreateElementA函式等,這樣也造成了Visitor模式的一個缺陷--新加入一個結點的時候都要新增Visitor中的對其進行訪問介面函式,這樣使得所有的Visitor及其派生類都要重新編譯了,也就是說Visitor模式一個缺點就是新增新的結點十分困難.另外,還需要指出的是Visitor模式採用了所謂的"雙重分派"的技術,拿上圖來作為例子,要對某一個結點進行訪問,首先需要產生一個Element的派生類物件,其次要傳入一個Visitor類派生類物件來呼叫對應的Accept函式,也就是說,到底對哪種Element採用哪種Visitor訪問,需要兩次動態繫結才可以確定下來,具體的實現可以參考下面實現程式碼中的Main.cpp部分是如何呼叫這些類的.
實現:
1)Visitor.h
/**//********************************************************************
created: 2006/08/09
filename: Visitor.h
author: 李創
http://www.cppblog.com/converse/
purpose: Visitor模式的演示程式碼
*********************************************************************/
#ifndef VISITOR_H
#define VISITOR_H
class Visitor;
class Element
{
public:
virtual~Element(){}
virtualvoid Accept(Visitor &rVisitor) =0;
protected:
Element(){}
};
class ConcreateElementA
: public Element
{
public:
virtual~ConcreateElementA() {}
virtualvoid Accept(Visitor &rVisitor);
};
class ConcreateElementB
: public Element
{
public:
virtual~ConcreateElementB() {}
virtualvoid Accept(Visitor &rVisitor);
};
class Visitor
{
public:
virtual~Visitor(){}
virtualvoid VisitConcreateElementA(ConcreateElementA *pConcreateElementA) =0;
virtualvoid VisitConcreateElementB(ConcreateElementB *pConcreateElementB) =0;
protected:
Visitor(){}
};
class ConcreateVisitorA
: public Visitor
{
public:
virtual~ConcreateVisitorA(){}
virtualvoid VisitConcreateElementA(ConcreateElementA *pConcreateElementA);
virtualvoid VisitConcreateElementB(ConcreateElementB *pConcreateElementB);
};
class ConcreateVisitorB
: public Visitor
{
public:
virtual~ConcreateVisitorB(){}
virtualvoid VisitConcreateElementA(ConcreateElementA *pConcreateElementA);
virtualvoid VisitConcreateElementB(ConcreateElementB *pConcreateElementB);
};
#endif
2)Visitor.cpp
/**//********************************************************************
created: 2006/08/09
filename: Visitor.cpp
author: 李創
http://www.cppblog.com/converse/
purpose: Visitor模式的演示程式碼
*********************************************************************/
#include "Visitor.h"
#include <iostream>
void ConcreateElementA::Accept(Visitor &rVisitor)
{
rVisitor.VisitConcreateElementA(this);
}
void ConcreateElementB::Accept(Visitor &rVisitor)
{
rVisitor.VisitConcreateElementB(this);
}
void ConcreateVisitorA::VisitConcreateElementA(ConcreateElementA *pConcreateElementA)
{
std::cout <<"VisitConcreateElementA By ConcreateVisitorA\n";
}
void ConcreateVisitorA::VisitConcreateElementB(ConcreateElementB *pConcreateElementA)
{
std::cout <<"VisitConcreateElementB By ConcreateVisitorA\n";
}
void ConcreateVisitorB::VisitConcreateElementA(ConcreateElementA *pConcreateElementA)
{
std::cout <<"VisitConcreateElementA By ConcreateVisitorB\n";
}
void ConcreateVisitorB::VisitConcreateElementB(ConcreateElementB *pConcreateElementA)
{
std::cout <<"VisitConcreateElementB By ConcreateVisitorB\n";
}
3)Main.cpp
/**//********************************************************************
created: 2006/08/09
filename: Main.cpp
author: 李創
http://www.cppblog.com/converse/
purpose: Visitor模式的測試程式碼
*********************************************************************/
#include "Visitor.h"
int main()
{
Visitor *pVisitorA =new ConcreateVisitorA();
Element *pElement =new ConcreateElementA();
pElement->Accept(*pVisitorA);
delete pElement;
delete pVisitorA;
return0;
}
表示一個作用於某物件結構中的各元素的操作.它使你可以在不改變各元素的類的前提下定義作用於這些元素的新操作.
UML結構圖:
解析:
Visitor模式把對結點的訪問封裝成一個抽象基類,通過派生出不同的類生成新的訪問方式.在實現的時候,在visitor抽象基類中聲明瞭對所有不同結點進行訪問的介面函式,如圖中的VisitConcreateElementA函式等,這樣也造成了Visitor模式的一個缺陷--新加入一個結點的時候都要新增Visitor中的對其進行訪問介面函式,這樣使得所有的Visitor及其派生類都要重新編譯了,也就是說Visitor模式一個缺點就是新增新的結點十分困難.另外,還需要指出的是Visitor模式採用了所謂的"雙重分派"的技術,拿上圖來作為例子,要對某一個結點進行訪問,首先需要產生一個Element的派生類物件,其次要傳入一個Visitor類派生類物件來呼叫對應的Accept函式,也就是說,到底對哪種Element採用哪種Visitor訪問,需要兩次動態繫結才可以確定下來,具體的實現可以參考下面實現程式碼中的Main.cpp部分是如何呼叫這些類的.
實現:
1)Visitor.h
created: 2006/08/09
filename: Visitor.h
author: 李創
http://www.cppblog.com/converse/
purpose: Visitor模式的演示程式碼
*********************************************************************/
#ifndef VISITOR_H
#define
class Visitor;
class Element
{
public:
virtual~Element(){}
virtualvoid Accept(Visitor &rVisitor) =0;
protected:
Element(){}
};
class ConcreateElementA
: public Element
{
public:
virtual~ConcreateElementA() {}
virtualvoid Accept(Visitor &rVisitor);
};
class ConcreateElementB
:
{
public:
virtual~ConcreateElementB() {}
virtualvoid Accept(Visitor &rVisitor);
};
class Visitor
{
public:
virtual~Visitor(){}
virtualvoid VisitConcreateElementA(ConcreateElementA *pConcreateElementA) =0;
virtualvoid VisitConcreateElementB(ConcreateElementB *pConcreateElementB) =0;
protected:
Visitor(){}
};
class ConcreateVisitorA
: public Visitor
{
public:
virtual~ConcreateVisitorA(){}
virtualvoid VisitConcreateElementA(ConcreateElementA *pConcreateElementA);
virtualvoid VisitConcreateElementB(ConcreateElementB *pConcreateElementB);
};
class ConcreateVisitorB
: public Visitor
{
public:
virtual~ConcreateVisitorB(){}
virtualvoid VisitConcreateElementA(ConcreateElementA *pConcreateElementA);
virtualvoid VisitConcreateElementB(ConcreateElementB *pConcreateElementB);
};
#endif
2)Visitor.cpp
/**//********************************************************************
created: 2006/08/09
filename: Visitor.cpp
author: 李創
http://www.cppblog.com/converse/
purpose: Visitor模式的演示程式碼
*********************************************************************/
#include "Visitor.h"
#include <iostream>
void ConcreateElementA::Accept(Visitor &rVisitor)
{
rVisitor.VisitConcreateElementA(this);
}
void ConcreateElementB::Accept(Visitor &rVisitor)
{
rVisitor.VisitConcreateElementB(this);
}
void ConcreateVisitorA::VisitConcreateElementA(ConcreateElementA *pConcreateElementA)
{
std::cout <<"VisitConcreateElementA By ConcreateVisitorA\n";
}
void ConcreateVisitorA::VisitConcreateElementB(ConcreateElementB *pConcreateElementA)
{
std::cout <<"VisitConcreateElementB By ConcreateVisitorA\n";
}
void ConcreateVisitorB::VisitConcreateElementA(ConcreateElementA *pConcreateElementA)
{
std::cout <<"VisitConcreateElementA By ConcreateVisitorB\n";
}
void ConcreateVisitorB::VisitConcreateElementB(ConcreateElementB *pConcreateElementA)
{
std::cout <<"VisitConcreateElementB By ConcreateVisitorB\n";
}
3)Main.cpp
/**//********************************************************************
created: 2006/08/09
filename: Main.cpp
author: 李創
http://www.cppblog.com/converse/
purpose: Visitor模式的測試程式碼
*********************************************************************/
#include "Visitor.h"
int main()
{
Visitor *pVisitorA =new ConcreateVisitorA();
Element *pElement =new ConcreateElementA();
pElement->Accept(*pVisitorA);
delete pElement;
delete pVisitorA;
return0;
}