Design Mode (23)-behavior-Visitor mode (visitor)

Source: Internet
Author: User
Overview
 
Indicates an operation that acts on each element in an object structure. It allows you to define new operations that act on these elements without changing the classes of each element.
Applicability
 
1. An object structure contains many class objects with different interfaces. You want to perform operations on these objects dependent on their specific classes. 2. You need to perform many different and unrelated operations on the objects in an object structure, and you want to avoid causing these operations to "pollute" the classes of these objects. Visitor allows you to define related operations in a class. When this object structure is shared by many applications, the visitor mode is used to allow each application to only include the required operations. 3. Classes defining the object structure are rarely changed, but new operations are often needed in this structure. Changing the object structure class requires redefining the interfaces for all visitors, which may be costly. If the object structure class changes frequently, it may be better to define these operations in these classes.
Participants
1. Visitor declares a visit operation for each class of concreteelement in the object structure. The operation name and features identify the class that sends a visit request to the visitor. This allows the visitor to determine the specific class of the element being accessed. In this way, visitors can directly access the element through a specific interface of the element. 2. concretevisitor implements each operation declared by the visitor. Implementation of each operationAlgorithmThe algorithm fragment corresponds to the class of objects in the structure. Concretevisitor provides context for the algorithm and stores its local state. This state often accumulates results while traversing the structure. 3. element defines an accept operation, which takes a visitor as the parameter. 4. concreteelement implements the accept operation, which takes a visitor as the parameter. 5. objectstructure can enumerate its elements. A high-level interface is provided to allow the visitor to access its elements. It can be a combination or a set, such as a list or an unordered set.

Class Diagram

Example

Package COM. sql9.actioned;/*** visitor mode example * @ author iihero **/interface visitor {// access the Chinese Emy of Sciences public void visitcas (CAS ); // access public void visituniversities (University); // access public void visitenterprises (Enterprise ent);} abstract class organization {public abstract void accept (visitor );} class CAS extends Organization {string extends utename; visitor; Public CAS (string name) {This. required utename = Name ;}@ override public void accept (visitor) {This. visitor = visitor;} public void specialjob () {visitor. visitcas (this); system. out. println ("some special job done for Cas ..... ") ;}} class university extends Organization {string name; visitor; Public University (string name) {This. name = Name ;}@ override public void accept (visitor) {This. visitor = visitor;} public void specialjob () {visitor. visituniversities (this); system. out. println ("some special job done for university ..... ") ;}} class enterprise extends Organization {string name; visitor; Public Enterprise (string name) {This. name = Name ;}@ override public void accept (visitor) {This. visitor = visitor;} public void specialjob () {visitor. visitenterprises (this); system. out. println ("some special job done for Enterprise ..... ") ;}} class concretevisitor implements visitor {@ override public void visitcas (CAS) {system. out. println ("concretevisitor visit Institute of CaS:" + CAS. required utename) ;}@ override public void visitenterprises (Enterprise ent) {system. out. println ("concretevisitor visit Enterprise:" + ent. name) ;}@ override public void visituniversities (university) {system. out. println ("concretevisitor visit University:" + University. name) ;}} public class visitortest {public static void main (string [] ARGs) {visitor = new concretevisitor (); CAS = new CAS ("Institute of mathematical sciences, Chinese Emy of Sciences"); CAS. accept (visitor); enterprise ent = new enterprise ("Sany Heavy Industry Group Co., Ltd."); ENT. accept (visitor); University = New University ("Tsinghua University"); University. accept (visitor); visitor. visitcas (CAS); visitor. visitenterprises (ENT); visitor. visituniversities (University );}}
Result

 
Concretevisitor visit Institute of CaS: concretevisitor visit Enterprise: Sany Heavy Industry Group Co., Ltd. concretevisitor visit University: Tsinghua University

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.