Visitor patterns for Behavioral design patterns

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.