Illustrate the application of visitor pattern in the design pattern in Java programming _java

Source: Internet
Author: User
Tags rand

Visitor (Visitor) mode: encapsulates operations that function on elements of a data structure, and it can define new operations that function on these elements without altering the data structure. The structure of the visitor pattern is as follows:

You can see from the image above that he has the following roles:
Abstract Visitor (Visitor) Role: defines an interface that declares one or more access operations.
A specific visitor (concretevisitor) Role: implements an interface declared by an abstract visitor, which is the individual access actions that an abstract visitor declares.
abstract Element (visitable) Role: declares an accept operation that accepts a visitor object as a parameter.
The concrete element node (concreteelement) Role: implements the accepted operation as specified by the abstract node.
data Structure object (objectstructure) role: You can iterate through all the elements in the structure, providing an interface that allows visitors to access each element.
The simulation code is as follows:

Interface Visitor { 
  void visit (Gladiolus g); 
 
  void visit (chrysanthemum c); 
} 

Concrete visitor   Name Access 
class Stringvisitor implements visitor { 
  String s; 
 
  Public String toString () {return 
    s; 
  } 
 
  public void Visit (Gladiolus g) { 
    s = "Gladiolus"; 
  } 
 
  public void visit (Chrysanthemum c) { 
    s = ' chrysanthemum '; 
  } 
} 


Concrete Visitor   Bee Access 
class Beevisitor implements visitor {public 
  void visit (Gladiolus g) { 
    System.out.println ("Bees come to Visit gladiolus") 
  ; 
 
  public void visit (Chrysanthemum c) { 
    System.out.println ("Bees to access Chrysanthemum"); 
  } 
 
 
Interface Flower { 
  void accept (Visitor v); 
} 


 
 * * Concrete element   chrysanthemum 
/class Chrysanthemum implements Flower {public 
  void accept (Visitor v) { C6/>v.visit (this); 
  } 
 


Concrete element   Gladiolus 
class Gladiolus implements Flower {public 
  void accept (Visitor v) { 
    v.visit ( this); 
  } 
 


This is flower an object builder 
class Flowergenerator { 
  private static Random rand = new Random (); 
 
  public static Flower Newflower () { 
    switch (rand.nextint (2)) { 
    default: Case 
    0: return 
      new Gladiolus (); 
    Case 1: Return 
      new Chrysanthemum ();}} 
 
The public class Test {/ 
   * * First obtains a specific visitor role traversal object structure on the client to call the Accept method on each element, passing the specific visitor role in this completes the entire process. 
  static void Main (String args[]) { 
    list<flower> flowers = new arraylist<flower> (); 
    for (int i = 0; i < i++) 
      Flowers.add (Flowergenerator.newflower ()); 
    Visitor Visitor = new Stringvisitor (); 
    Iterator<flower> iterator = Flowers.iterator (); 
    while (Iterator.hasnext ()) { 
      iterator.next (). Accept (visitor); 
      System.out.println (visitor); 
    } 
    System.out.println ("---------------"); 
     * * A new Access behavior: Beevisitor Bee Access 
     * * 
    Visitor Visitor2 = new Beevisitor (); 
    for (Flower flower:flowers) { 
      flower.accept (VISITOR2);}}} 
 

Results:

Gladiolus 
chrysanthemum 
chrysanthemum 
gladiolus 
chrysanthemum 
Chrysanthemum Chrysanthemum 
chrysanthemum 
gladiolus 
gladiolus 
--------------- 
Bees to visit the Gladiolus 
bees. Visit the Chrysanthemum bees to visit the Chrysanthemum bees to visit the Gladiolus bees to visit the 
chrysanthemum 
bees to visit chrysanthemum
    bees come to visit the Chrysanthemum bees to visit the Chrysanthemum bees to visit the 
gladiolus 
bees to visit gladiolus 

You can consider using the visitor pattern in the following situations:

1, an object structure contains many classes of objects, they have different interfaces, and you want to implement some of these objects depending on their specific classes of operations.
2, 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 operations "contaminate" these objects. Visitor allows you to centralize related operations and define them in a class.
3. When the object structure is shared by many applications, the visitor mode allows each application to contain only the operations that need to be used.
4. Classes that define object structures rarely change, but often need to define new operations on this structure. Changing the object structure class requires redefining the interface to all visitors, which can be costly. If the object structure classes change frequently, it may be better to define them in these classes.
These individuals appear to be recommendations, and the project also needs specific analysis of specific issues.

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.