Visitor Visitor Mode (behavioral mode)
motivation (motivation) in the process of software construction, due to the change of requirements, some class hierarchies often need to add new behavior (methods), if you make such a change directly in the base class, it will bring a heavy burden of change to the subclass, or even destroy the original design.
If, without changing the class hierarchy, you need to transparently add new operations to each class activity on the class hierarchy at run time to avoid this problem?
Intentions (Intent)
Represents an operation that acts on elements in an object structure. It can define new actions that act on these elements without changing the class of each element. --"Design pattern" GoF
Sample code
Public Abstract classShape { Public Abstract voidDraw (); //problem: Because of the new MoveTo method in shape, the individual subclasses will have to change Public Abstract voidMoveTo (point point); } Public classRectangle:shape { Public Override voidDraw () {//... } } Public classCircle:shape { Public Override voidDraw () {//... } } Public classLine:shape { Public Override voidDraw () {//... } }
In order not to change the subclasses:
Public Abstract classShape { Public Abstract voidDraw (); //expect to be able to introduce new operations in the future Public Abstract voidAccept (Shapevisitor visitor); } Public Abstract classShapevisitor { Public Abstract voidVisit (Rectangle shape); Public Abstract voidVisit (Circle shape); Public Abstract voidVisit (line shape); } Public classMyvisitor:shapevisitor { Public Override voidVisit (Rectangle shape) {//increase the operation of the rectangle } Public Override voidVisit (Circle shape) {//increase the operation of the Circle } Public Override voidVisit (line shape) {//increase the operation of line } } Public classRectangle:shape { Public Override voidDraw () {//... } Public Override voidAccept (Shapevisitor visitor) {visitor. Visit ( This);//Call Visit (Rectangle shape) } } Public classCircle:shape { Public Override voidDraw () {//... } Public Override voidAccept (Shapevisitor visitor) {visitor. Visit ( This);//Call Visit (Circle shape) } } Public classLine:shape { Public Override voidDraw () {//... } Public Override voidAccept (Shapevisitor visitor) {visitor. Visit ( This);//Call visit (line shape) } }
Client calls:
Static void Main (string[] args) { Shapevisitor visitor=new myvisitor ();
Line line =new line (); Line. Accept (visitor); Rectangle Rectangle=new Rectangle (); Rectangle. Accept (visitor); }
Structure (Structure)
Several points of visitor model
- Visitor mode transparently adds new operations to classes on the class hierarchy at run time by using the so-called dual distribution (double dispatch) without changing the element hierarchy.
- The so-called dual distribution, the visitor model, consists of two polymorphic distributions (note the polymorphic mechanism in it); The first one is the polymorphism analysis of the Accept method, and the second is the polymorphism of the visit method.
- The biggest disadvantage of the visitor pattern is that extending the class hierarchy (adding new element subclasses) can result in changes to the visitor class. Therefore, the visitor mode is suitable for "element class hierarchy stability, where operations are often subject to frequent changes."
Reprint please specify the source:
Jesselzj
Source: http://jesselzj.cnblogs.com
Design mode 23:visitor Visitor mode (behavioral mode)