设计模式的理会和实现(C++)之二十-Visitor模式
副标题#e#
浸染:
暗示一个浸染于某工具布局中的各元素的操纵.它使你可以在不改变各元素的类的前提下界说浸染于这些元素的新操纵.
UML布局图:
理会:
Visitor模式把对结点的会见封装成一个抽象基类,通过派生出差异的类生成新的会见方法.在实现的时候,在visitor抽象基类中声明白对所有差异结点举办会见的接口函数,如图中的VisitConcreateElementA函数等,这样也造成了Visitor模式的一个缺陷–新插手一个结点的时候都要添加Visitor中的对其举办会见接口函数,这样使得所有的Visitor及其派生类都要从头编译了,也就是说Visitor模式一个缺点就是添加新的结点十分坚苦.别的,还需要指出的是Visitor模式回收了所谓的"双重分配"的技能,拿上图来作为例子,要对某一个结点举办会见,首先需要发生一个Element的派生类工具,其次要传入一个Visitor类派生类工具来挪用对应的Accept函数,也就是说,到底对哪种Element回收哪种Visitor会见,需要两次动态绑定才可以确定下来,详细的实现可以参考下面实现代码中的Main.cpp部门是如何挪用这些类的.
#p#副标题#e#
实现:
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(){}
virtual void Accept(Visitor &rVisitor) = 0;
protected:
Element(){}
};
class ConcreateElementA
: public Element
{
public:
virtual ~ConcreateElementA() {}
virtual void Accept(Visitor &rVisitor);
};
class ConcreateElementB
: public Element
{
public:
virtual ~ConcreateElementB() {}
virtual void Accept(Visitor &rVisitor);
};
class Visitor
{
public:
virtual ~Visitor(){}
virtual void VisitConcreateElementA(ConcreateElementA *pConcreateElementA) = 0;
virtual void VisitConcreateElementB(ConcreateElementB *pConcreateElementB) = 0;
protected:
Visitor(){}
};
class ConcreateVisitorA
: public Visitor
{
public:
virtual ~ConcreateVisitorA(){}
virtual void VisitConcreateElementA(ConcreateElementA *pConcreateElementA);
virtual void VisitConcreateElementB(ConcreateElementB *pConcreateElementB);
};
class ConcreateVisitorB
: public Visitor
{
public:
virtual ~ConcreateVisitorB(){}
virtual void VisitConcreateElementA(ConcreateElementA *pConcreateElementA);
virtual void 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
#p#分页标题#e#
/**//********************************************************************
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;
return 0;
}