Java design Mode _ (Behavioral) _ Visitor mode __java

Source: Internet
Author: User

Quote Encyclopedia

The visitor mode (Visitor pattern) is one of the 23 design patterns proposed by GOF, which belongs to the behavior pattern. According to the "Big talk design Model" said that is the most complex is the most difficult to understand a pattern.
Definition (derived from gof "design pattern"): represents an operation that acts on each element of an object's structure. It allows you to define new operations that function on these elements without changing the various element classes.
From the definition you can see that the structure object is a prerequisite for using the visitor pattern, and that the struct object must have methods to traverse its own objects. This is similar to the collection concept in the Java language.


Related Roles

1. Abstract Visitor Visitor: An abstract class or interface that declares which elements a visitor can access, specifically in a program where the parameters in the Visit method define which objects can be accessed.
2. Specific visitor Concretevisitor.: Implements the method that the abstract visitor declares, and it affects what the visitor does after accessing a class, what to do.
3. Abstract element class element: an interface or abstract class that declares which type of visitor access is accepted, and is defined by the parameters in the Accept method. Abstract elements generally have two kinds of methods, one part of their business logic, and the other is to allow the type of visitors to receive access.
4. Specific element class Objectstructure: Implementing the Accept method declared by the abstract element class, usually visitor.visit (this), has basically formed a stereotype.
Structure object: A container of an element that generally contains a container containing a number of different classes and interfaces, such as list, Set, map, and so on, which is rarely abstracted from the role in the project.


Concrete Implementation



Related Code

1. Abstract Element class

Abstract element class public
abstract class Abstractelement {
	///Receive visitor access to public
	abstract void accept (Ivisitor visitor); The business logic of the c3/>//itself is public
	abstract void dosomething ();
}

2, the element concrete realization class
The element concretely implements class A public class
Concreteelementa extends Abstractelement {
	//itself's business logic public
	void DoSomething () C9/>system.out.println ("This is element 1");
	}

	Receive visitor access public
	void Accept (Ivisitor visitor) {
		visitor.visit (this);
	}

The element concretely implements B public
class Concreteelementb extends Abstractelement {
	//its own business logic public
	void DoSomething () {
		System.out.println ("This is Element 2");
	}

	Receive visitor access public
	void Accept (Ivisitor visitor) {
		visitor.visit (this);
	}


3. Visitor interface

Visitor interface Public
interface Ivisitor {public
	void visit (abstractelement element);
}

4. Specific visitors
Visitors specifically implement public
class Realvisitor implements Ivisitor {public

	void visit (abstractelement element) {
		Element.dosomething ();
	}

5. Structure Object Role
struct Object role
class Objectstruture {
	//imitation test element data public
	static list<abstractelement> getlist () {
		list<abstractelement> list = new arraylist<abstractelement> ();
		List.add (New Concreteelementa ());
		List.add (New Concreteelementb ());

		return list;
	}

6. Client-side test

public class Client {public
	static void Main (string[] args) {
		list<abstractelement> List = Objectstrutur E.getlist ();
		for (Abstractelement e:list) {
			//Add Visitor
			e.accept (New Realvisitor ());
		}
	}
}


The above code simply implements the observer mode and runs the output:

This is element 1.
This is element 2.


The pros and cons of the Observer model

Advantages

1) Consistent with the principle of single duty: In any scenario where the visitor pattern is applied, the action that needs to be encapsulated within the element class for the visitor must be an operation that is not related to the element class itself and is variable, and the visitor pattern conforms to a single responsibility principle, on the other hand, because the encapsulated operation is usually variable, So when a change occurs, it is possible to extend the change part without changing the element class itself.
2 Extensibility is good: element classes can be extended to different operations by accepting different audiences.

Disadvantage

1 It is difficult to change the structure of the object
It does not apply to situations where classes in the object structure often change, because the object structure has changed, and the visitor's interface and the visitor's implementation have to change accordingly, at a high price.
2) Damage Encapsulation
Visitor patterns often require an object structure to open internal data to visitors and objectstructrue, which undermines the encapsulation of the object.


Applicable situation

1 An object structure contains many classes of objects, they have different interfaces, and you want to implement some operations that depend on their specific class for these objects.
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. The visitor mode 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 The class that defines the object structure rarely changes, but it is often necessary 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.






Related Article

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.