詳解C++設計形式編程中對拜訪者形式的應用。本站提示廣大學習愛好者:(詳解C++設計形式編程中對拜訪者形式的應用)文章只能為提供參考,不一定能成為您想要的結果。以下是詳解C++設計形式編程中對拜訪者形式的應用正文
拜訪者形式(visitor),表現一個感化於某對象構造中的各元素的操作。它使你可以在不轉變各元素的類的條件下界說感化於這些元素的新操作。拜訪者形式實用於數據構造絕對穩固的體系。它把數據構造和感化於構造上的操作之間的耦合擺脫開,使得操作聚集可以絕對自在地演變。拜訪者形式的目標是要把處置從數據構造分別出來。許多體系可以依照算法和數據構造離開,假如如許的體系有比擬穩固的數據構造,又有易於變更的算法的話,應用拜訪者形式就是比擬適合的,由於拜訪者形式使得算法操作的增長變得輕易。反之,假如如許的體系的數據構造對象易於變更,常常要有新的數據對象增長出去,就不合適應用拜訪者形式。
拜訪者形式的長處就是增長新的操作很輕易,由於增長新的操作就意味著增長一個新的拜訪者。拜訪者形式將有關的行動集中到一個拜訪者對象中。平日concreteVisitor可以零丁開辟,不用跟concreteElement寫在一路。拜訪者的缺陷其實也就是使增長新的數據構造變得艱苦了。
構造圖:
拜訪者形式根本示例代碼
拜訪者形式 visitor.h、concreteVisitor.h、element.h、concreteElement.h、objectStructure.h
客戶端 visitorApp.cpp
拜訪者形式
visitor.h
/************************************************************************
* description: 為該對象構造中ConcreteElement的每個類聲明一個visit操作
* remark:
************************************************************************/
#ifndef _VISITOR_H_
#define _VISITOR_H_
class concreteElementA;
class concreteElementB;
class visitor
{
public:
visitor(){};
virtual ~visitor(){};
virtual void visitConcreteElementA(concreteElementA* pConcreteElementA) = 0;
virtual void visitConcreteElementB(concreteElementB* pConcreteElementB) = 0;
};
#endif// _VISITOR_H_
concreteVisitor.h
/************************************************************************
* description: 詳細拜訪者,完成每一個由visitor聲明的操作。每一個操作完成算法
的一部門,而該算法片段乃是對應於構造中對象的類
* remark:
************************************************************************/
#ifndef _CONCRETE_VISITOR_H_
#define _CONCRETE_VISITOR_H_
#include "visitor.h"
#include <iostream>
using namespace std;
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;
}
};
#endif// _CONCRETE_VISITOR_H_
element.h
/************************************************************************
* description: 界說一個accept操作,它以一個拜訪者為參數
* remark:
************************************************************************/
#ifndef _ELEMENT_H_
#define _ELEMENT_H_
class visitor;
class element
{
public:
element(){};
virtual ~element(){};
virtual void accept(visitor* pVisitor) = 0;
};
#endif// _ELEMENT_H_
concreteElement.h
#ifndef _CONCRETE_ELEMENT_H_
#define _CONCRETE_ELEMENT_H_
#include "element.h"
#include <iostream>
using namespace std;
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;
}
};
#endif// _CONCRETE_ELEMENT_H_
objectStructure.h
/************************************************************************
* description: 列舉元素,可以供給一個高層的接口以許可拜訪者拜訪它的元素
* remark:
************************************************************************/
#ifndef _OBJECT_STRUCTURE_H_
#define _OBJECT_STRUCTURE_H_
#include "element.h"
#include "visitor.h"
#include <list>
using namespace std;
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<element*>::iterator Iter;
for (Iter = m_list.begin(); Iter != m_list.end(); ++Iter)
{
if (NULL != *Iter)
{
(*Iter)->accept(pVisitor);
}
}
}
private:
list<element*> m_list;
};
#endif// _OBJECT_STRUCTURE_H_
客戶端
visitorApp.cpp
// visitorApp.cpp : 界說掌握台運用法式的進口點。
//
#include "stdafx.h"
#include "objectStructure.h"
#include "concreteElement.h"
#include "concreteVisitor.h"
void freePtr(void* vptr)
{
if (NULL != vptr)
{
delete vptr;
vptr = NULL;
}
}
int _tmain(int argc, _TCHAR* 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;
}
應用拜訪者形式的長處和缺陷
拜訪者形式有以下的長處:
拜訪者形式有以下的缺陷:
拜訪者形式的實用場景: