轉(zhuǎn)載自:
http://patmusing.blog.163.com/blog/static/1358349602010150249596/
表示一個(gè)作用于某對(duì)象結(jié)構(gòu)中的各元素的操作。它可以在不改變各元素的類的前提下定義作用于這些元素的新的操作。
“Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.” – GoF
類層次結(jié)構(gòu)中可能經(jīng)常由于引入新的操作,從而將類型變得很脆弱。比如假定三個(gè)類Rectangle、Triangle和Line繼承自抽象類Shape,現(xiàn)在由于某種原因需要在抽象類Shape中增加一個(gè)abstract (或者pure virtual)方法,如果使用通常的方法,那么它所有的子類,即Rectangle、Triangle和Line都需要修改。
由于需求的改變,某些類層次結(jié)構(gòu)中常常需要增加新的行為(方法),如果直接在基類中做這樣的更改,將會(huì)給子類帶來很繁重的變更負(fù)擔(dān),甚至破壞原有的設(shè)計(jì)。Visitor設(shè)計(jì)模式就是在不更改類層次結(jié)構(gòu)的前提下,在運(yùn)行時(shí)根據(jù)需要?jiǎng)討B(tài)地為類層次結(jié)構(gòu)上的各個(gè)類動(dòng)態(tài)添加新的操作(方法)。
下面是Visitor模式的UML類圖:
Visitor設(shè)計(jì)模式要解決的問題:
- 類層次結(jié)構(gòu)不變,但類的方法需要增加;
- 預(yù)料到基類有更改,但不知道是何種具體更改。
要增加一個(gè)方法,實(shí)際上就是增加一個(gè)Visitor的子類。
業(yè)務(wù)實(shí)例:
有Rectangle和Circle兩種形狀,在不改動(dòng)他們代碼的情況下,分別為它們?cè)黾犹畛?/span>(Fill)和附加文字(AddText)的功能。
實(shí)現(xiàn)上面業(yè)務(wù)實(shí)例的C++代碼:
// Visitor.h
#include <iostream>
using namespace std;
class Visitor;
class Shape
{
public:
// draw為已經(jīng)確知需要的方法
virtual void draw() = 0;
// 1. 預(yù)料將來可能會(huì)引入新的方法,因此在這里預(yù)留一個(gè)accept方法
// 2. 可以通過下面的accept方法,為Shape的子類增加一個(gè)或多個(gè)方法
virtual void accept(Visitor *visitor) = 0;
public:
virtual ~Shape()
{
cout << "in the destructor of Shape..." << endl;
}
};
class Rectangle : public Shape
{
public:
void draw();
void accept(Visitor *visitor);
public:
~Rectangle()
{
cout << "in the destructor of Rectangle..." << endl;
}
};
class Circle : public Shape
{
public:
void draw();
void accept(Visitor *visitor);
public:
~Circle()
{
cout << "in the destructor of Circle..." << endl;
}
};
class Visitor
{
public:
// 注意此處參數(shù)為具體類,而非抽象類Shape。
// Shape有多少個(gè)子類,就需要在此處重載多少次visit方法。
// 除非使用RTTI,則可以只有一個(gè)visit方法,并且該方法的參數(shù)類型為抽象的Shape*,同時(shí)需要在visit方法中確定,
// 傳入的參數(shù)到底是Shape的哪個(gè)具體的子類。
virtual void visit(Rectangle *shape) = 0;
virtual void visit(Circle *shape) = 0;
public:
virtual ~Visitor()
{
cout << "in the destructor of Visitor..." << endl;
}
};
// 假定要為Shape的子類增加一個(gè)填充(Fill)操作,那么就為Visitor增加一個(gè)子類,即FillVisitor
class FillVisitor : public Visitor
{
public:
void visit(Rectangle *shape)
{
// 為類Rectangle增加一個(gè)方法,由于實(shí)現(xiàn)這個(gè)新方法可能需要用到原來對(duì)象中的屬性或者方法,因此需要
// 將自身(this)傳到這里。理論上而言,如果不用到原來對(duì)象的原有的屬性或者方法,那么visit方法是可以
// 沒有參數(shù)的;另外一種情況就是,如果必要,visit方法也可以擁有多個(gè)參數(shù)
cout << "Here is the newly added Fill() method for class Rectangle." << endl;
}
void visit(Circle *shape)
{
// 為類Circle增加一個(gè)方法,由于實(shí)現(xiàn)這個(gè)新方法可能需要用到原來對(duì)象中的屬性或者方法,因此需要
// 將自身(this)傳到這里。理論上而言,如果不用到原來對(duì)象的原有的屬性或者方法,那么visit方法是可以
// 沒有參數(shù)的;另外一種情況就是,如果必要,visit方法也可以擁有多個(gè)參數(shù)
cout << "Here is the newly added Fill() method for class Circle." << endl;
}
public:
~FillVisitor()
{
cout << "in the destructor of FillVisitor..." << endl;
}
};
// 假定要為Shape的子類增加一個(gè)附加文字(AddText)操作,那么就為Visitor增加一個(gè)子類,即AddTextVisitor
class AddTextVisitor : public Visitor
{
public:
void visit(Rectangle *shape)
{
// 為類Rectangle增加一個(gè)方法,由于實(shí)現(xiàn)這個(gè)新方法可能需要用到原來對(duì)象中的屬性或者方法,因此需要
// 將自身(this)傳到這里。理論上而言,如果不用到原來對(duì)象的原有的屬性或者方法,那么visit方法是可以
// 沒有參數(shù)的;另外一種情況就是,如果必要,visit方法也可以擁有多個(gè)參數(shù)
cout << "Here is the newly added AddText() method for class Rectangle." << endl;
}
void visit(Circle *shape)
{
// 為類Circle增加一個(gè)方法,由于實(shí)現(xiàn)這個(gè)新方法可能需要用到原來對(duì)象中的屬性或者方法,因此需要
// 將自身(this)傳到這里。理論上而言,如果不用到原來對(duì)象的原有的屬性或者方法,那么visit方法是可以
// 沒有參數(shù)的;另外一種情況就是,如果必要,visit方法也可以擁有多個(gè)參數(shù)
cout << "Here is the newly added AddText() method for class Circle." << endl;
}
public:
~AddTextVisitor()
{
cout << "in the destructor of AddTextVisitor..." << endl;
}
};
// Visitor.cpp
#include "Visitor.h"
void Rectangle::draw()
{
cout << "Implement method Rectangle::draw() here!" << endl;
}
void Rectangle::accept(Visitor *visitor)
{
visitor->visit(this); // 轉(zhuǎn)發(fā)到visitor的visit方法,并將this作為參數(shù)傳遞過去
}
void Circle::draw()
{
cout << "Implement method Circle::draw() here!" << endl;
}
void Circle::accept(Visitor *visitor)
{
visitor->visit(this); // 轉(zhuǎn)發(fā)到visitor的visit方法,并將this作為參數(shù)傳遞過去
}
// PatternClient.cpp
#include "Visitor.h"
int main(int argc, char **argv)
{
Visitor *visitor1 = new FillVisitor();
Shape *rectangle = new Rectangle();
// 調(diào)用該形狀已經(jīng)實(shí)現(xiàn)的方法
rectangle->draw();
// 通過visitor1調(diào)用新增加的方法
rectangle->accept(visitor1);
Shape *circle = new Circle();
// 調(diào)用該形狀已經(jīng)實(shí)現(xiàn)的方法
rectangle->draw();
// 通過visitor1調(diào)用新增加的方法
circle->accept(visitor1);
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
Visitor *visitor2 = new AddTextVisitor();
// 通過visitor2調(diào)用新增加的方法
rectangle->accept(visitor2);
// 通過visitor2調(diào)用新增加的方法
circle->accept(visitor2);
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
delete visitor1;
delete visitor2;
delete rectangle;
delete circle;
return 0;
}
運(yùn)行結(jié)果:
Implement method Rectangle::draw() here!
Here is the newly added Fill() method for class Rectangle.
Implement method Rectangle::draw() here!
Here is the newly added Fill() method for class Circle.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Here is the newly added AddText() method for class Rectangle.
Here is the newly added AddText() method for class Circle.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
in the destructor of FillVisitor...
in the destructor of Visitor...
in the destructor of AddTextVisitor...
in the destructor of Visitor...
in the destructor of Rectangle...
in the destructor of Shape...
in the destructor of Circle...
in the destructor of Shape...
結(jié)果符合預(yù)期。
由上面的代碼可以很清楚地知道,我們?nèi)绻獮?/span>Rectangle或Circle增加新方法,只需要多增加一個(gè)Visitor的子類即可,而Rectangle或Circle這兩個(gè)類本身的代碼,沒無任何改動(dòng)。
上面代碼對(duì)應(yīng)的UML類圖:
雙向分發(fā)(Double Dispatch)
- accept(Visitor *visitor)方法在Shape的子類 (如Rectangle)中;
- visit(Rectangle *rectangle)等visit方法在Visitor的子類 (如FillVisitor)中;
- 兩處重要的多態(tài)特性:
n 調(diào)用accept方法的對(duì)象之多態(tài)辨析,即是誰調(diào)用accept方法?(在本例中,是Rectangle還是Circle?)
n accept方法的參數(shù)之多態(tài)辨析,即accept方法的參數(shù)到底是那個(gè)Visitor的子類
正是這兩處多態(tài)特性的使用,就形成了所謂的雙向分發(fā)(Double Dispatch)機(jī)制,即首先將visitor作為參數(shù)傳遞給Shape的子類(在本例中即Rectangle和Circle);然后在Shape子類的accept方法中調(diào)用visitor的visit方法,而且visit方法的參數(shù)是this,這樣Shape子類就將自己傳遞到了visitor類的visit方法中,然后在visitor的visit方法中為Shape的子類增加需要的新的行為。這就是雙向分發(fā)的核心思想,即visitor被傳遞(分發(fā))了一次,即visitor被傳遞到Shape子類中一次,Shape的子類也被傳遞(分發(fā))了一次,即Shape的子類被傳遞到visitor中一次。
從更高層次來看這個(gè)問題:Shape中的accept()以Visitor作為參數(shù),Visitor中的visit以Shape作為參數(shù)。
上面給出代碼的調(diào)用順序: