Design Pattern 15-Visitor

xiaoxiao2021-03-06  58

/ * Visitor Accessor mode defines the operation of each object in a target group. It allows you to define new operations for these objects without changing these object itself.

In Java, Visitor mode is actually an element that separates the Collection structure and operates these elements.

Why use Visitor? Java's Collection (including Vector and Hashtable) is our most frequently used technology, but the Collection seems to be a black large dyeing cylinder. Objects have been made in a variety of clear types of features, which is removed, these types It will disappear. Then we must use IF to judge, such as:

Iterator iterator = collection.iterator () while (iterator.hasNext ()) {Object o = iterator.next (); if (o instanceof Collection) messyPrintCollection ((Collection) o); else if (o instanceof String) System.out .println ("'" o.Tostring () "'"; Else if (o instanceof float) system.out.println (O.Tostring () "f"); else system.out.println (O . Tostring ());} In the above example, we used instanceof to determine the type of O.

Obviously, the shortcomings of this shortcoming IF ELSE IF is very cumbersome. We can use the Visitor mode to resolve it. * / Use system.collections;

Namespace pattern {///

/// summary description for class1. /// public interface visitable {void accept (visitor visitor);

Public class stringeElement: Visitable {private string v; public stringeElement (string str) {v = str;}

Public string getValue () {returnv;

/ / Define the specific content of Accept here is a very simple sentence call public void accept (visitor visitor) {visitor.visitstring (this);}}

Public class floatelement: Visitable {private float m_f; public floatelement (float f) {this.m_f = f;}

Public float getValue () {Return M_F;}

// Define the specific content of Accept This is a very simple sentence call public void accept (visitor visitor) {visitor.visitfloat (this);}}

Public interface visitor {

Void Visitstring (StringeElement Stringe); Void Visitfloat; Void Visitcollection;

}

public class ConcreteVisitor: Visitor {// In this method, we have achieved success in the Collection of the elements of access public void visitCollection (IList collection) {IEnumerator iterator = collection.GetEnumerator (); while (iterator.MoveNext ()) {Object o = Iterator.current; if (o is visitable o) .accept (this);}} public void visitstring (StringeElement stringe) {system.console .writeline ("'" stringe.getValue () "'");} Public void visitfloat (floateLement floate) {system.console .writeline (). Tostring () "f");}

}

} // Visitor mode // Visitor Visitor = New Concretevisitor (); /// StringeElement Stringe = New StringeElement ("I am a string"); // visitor.visitstring (stringe); /// // system .Collections. ArrayList List = new system.collections.arraylist (); // list.add (New StringeElement ("I am a string1")); // list.add (New StringeElement ("i am a string2")) ; // list.add (new floatelement (12)); // list.add (New StringeElement ("I am a string3")); // visitor.visitcollection (list);

转载请注明原文地址:https://www.9cbs.com/read-111823.html

New Post(0)