structure |
|
intent |
|
applicability |
- An object structure contains many classes of objects that have different interfaces, and you want to implement some operations that depend on their specific classes for those objects.
- You need to do a lot of different and unrelated operations on objects in an object structure, and you want to avoid classes that let these actions "pollute" those objects. Vi s i t o r allows you to centralize related operations into one class. When the object structure is shared by many applications, the vi s i t o r mode allows each app to contain only the actions that need to be used.
-
|
1 usingSystem;2 3 Abstract classVisitor4 {5 Abstract Public voidVisitelementa (Concreteelementa a);6 Abstract Public voidVISITELEMENTB (Concreteelementb b);7 }8 9 classConcretevisitor1:visitorTen { One Override Public voidVisitelementa (Concreteelementa a) A { - - } the - Override Public voidVISITELEMENTB (CONCRETEELEMENTB b) - { - + } - } + A Abstract classElement at { - Abstract Public voidAccept (Visitor v); - } - - classconcreteelementa:element - { in PublicVisitor Myvisitor; - Override Public voidAccept (Visitor v) to { +Myvisitor =v; - } the * Public voidOperationa () $ {Panax Notoginseng - } the + Public voiddosomework () A { the //Do some + // . . . - $ //Get visitor to visit $Myvisitor.visitelementa ( This); - - //Do some the // . . . - }Wuyi } the - classconcreteelementb:element Wu { - Override Public voidAccept (Visitor v) About { $ - } - - Public voidoperationb () A { + the } - } $ the /// <summary> the ///Summary description for Client. the /// </summary> the Public classClient - { in Public Static intMain (string[] args) the { theConcreteelementa EA =NewConcreteelementa (); AboutCONCRETEELEMENTB EB =Newconcreteelementb (); theConcreteVisitor1 V1 =NewConcreteVisitor1 (); the the ea.accept (v1); + ea.dosomework (); - the return 0;Bayi } the}
Visitor Mode
Visitor patterns for Behavioral design patterns