VISITOR模式:将操作独立于类之外,类根据自己需要的操作而接受相应的访问者。
使用了访问者,我们可以看到,如果需要在Element类中实现一个新的操作,我们就不用修改类的结构了,
例如类是封装在DLL或者COM里的话,我们就不用重新编译了,只需要定义一个新的Visitor,例如VisitorC,然后在Element中接受它,一切OK!
这样做的好处是如果需要实现一个新操作,类的结构不用变,特别是整个类层次,如果要变,代价是比较大的。
使用Visitor模式就可以保证增加操作非常简单方便,并且符合OCP了。
Visitor模式有一个吓人的概念:双分派,其实所谓的双分派也只不过是指:要定义一个操作,需要两个对象来决定, 这两个对象是Element 对象和Visitor对象,例如ElementA对象接受VisitorA对象和接受VisitorB对象的执行结果是不一样的。
下面的例子大家应该能比较清楚的看到“双分派”的意义。
// Visitor.cpp
#include
<
iostream
>
#include < string >
using namespace std;
class Element;
/// Visitor类及其子类
class Visitor
{
public:
virtual void Visit(Element *e) = 0;
} ;
class VisitorA : public Visitor
{
public:
virtual void Visit(Element *e)
{
cout << "执行操作A" << endl;
}
} ;
class VisitorB : public Visitor
{
public:
virtual void Visit(Element *e)
{
cout << "执行操作B" << endl;
}
} ;
/// Element类及其子类
class Element
{
public:
virtual void Accept(Visitor *v)
{
cout << m_name ;
v->Visit(this);
}
string &GetName()
{
return m_name;
}
protected:
string m_name;
} ;
class ElementA : public Element
{
public:
ElementA(string s)
{
m_name = s;
}
} ;
class ElementB : public Element
{
public:
ElementB(string s)
{
m_name = s;
}
} ;
/// 测试上面的类
void main()
{
ElementA ea("元素A");
ElementB eb("元素B");
VisitorA va;
VisitorB vb;
// 双分派,“接受者”和“访问者”决定了一个操作
ea.Accept(&va);
ea.Accept(&vb);
eb.Accept(&va);
eb.Accept(&vb);
}
#include < string >
using namespace std;
class Element;
/// Visitor类及其子类
class Visitor
{
public:
virtual void Visit(Element *e) = 0;
} ;
class VisitorA : public Visitor
{
public:
virtual void Visit(Element *e)
{
cout << "执行操作A" << endl;
}
} ;
class VisitorB : public Visitor
{
public:
virtual void Visit(Element *e)
{
cout << "执行操作B" << endl;
}
} ;
/// Element类及其子类
class Element
{
public:
virtual void Accept(Visitor *v)
{
cout << m_name ;
v->Visit(this);
}
string &GetName()
{
return m_name;
}
protected:
string m_name;
} ;
class ElementA : public Element
{
public:
ElementA(string s)
{
m_name = s;
}
} ;
class ElementB : public Element
{
public:
ElementB(string s)
{
m_name = s;
}
} ;
/// 测试上面的类
void main()
{
ElementA ea("元素A");
ElementB eb("元素B");
VisitorA va;
VisitorB vb;
// 双分派,“接受者”和“访问者”决定了一个操作
ea.Accept(&va);
ea.Accept(&vb);
eb.Accept(&va);
eb.Accept(&vb);
}
例如类是封装在DLL或者COM里的话,我们就不用重新编译了,只需要定义一个新的Visitor,例如VisitorC,然后在Element中接受它,一切OK!