The 20th chapter of the design pattern-visitor Mode (Java implementation)
"Hey, your face is so red." "Refreshed." "Why is it yellow again?" "Afraid of cold, painted, painted, wax." "The body still has the wine, the betrayed, originally is drinks drinks the AH." "Hey, let, let you find out, a few friends today, and then get a little drink, a few cups." "Good truly." "That is natural, otherwise, go and have two drinks." "I have some important things to do, fish brother, you don't hang me." "What, what matters, can compare, drink ah". "Go, accompany me, accompany me to drink two cups." (the author has been pulled away.) ) visitors.
Self-introduction of visitor patterns
Tired of dead my jet (ladies and gentlemen), hello everyone, my earth native mode, called the visitor. The definition is: represent an operation to is performed on the elements of an object strctrue. Visitor lets you define a new operation without changing the classes of the elements on which it operates. It means: encapsulate something that acts on a The operation of elements in a data structure that defines new operations that act on these elements without altering the data structure. The General class diagram is:
There are definitions and explanations for each class, and I will not go into the details because of the detailed explanations in the diagram.
Self-analysis of visitor patterns
I know what to do unwinnable self-analysis, well, your taste is the first to listen to the shortcomings of the merits, I do not, I would like to introduce my advantages:
- First of all, the sum is in line with the legendary single responsibility principle, the specific element role is responsible for the data loading, visitor for each class declaration of a visit operation, two different responsibilities are very clear separation, the respective deductive changes.
- Secondly, I have excellent extensibility. Because of the separation of responsibilities, it is easy to increase the operation of the data.
- Finally pinch, flexibility is very high.
Next, I'll talk about my shortcomings, Isaac:
- Specific element access publishes details to the visitor, and does not conform to the Dimitri rule.
- Then it is difficult to change the specific elements.
- Finally, I accidentally violated the dependency inversion principle. A visitor relies on a specific element, not an abstract element, which destroys the dependency inversion principle.
Well, the pros and cons seem to be so much, if you think there is anything else, you can make it.
Implementation of access patterns
In the code of the Impromptu code, I will take the fish brother dinner as an example to raise a chestnut bar, first of all, nature is the realization of abstract elements:
1 public abstract class element{ 2 // business logic 3 public abstract void Dosth (); 4 Allow visitors 5 public abstract void accept (Ivisitor visitor) ; 6 }
The definition of abstract elements is relatively simple, two abstract methods, one is business logic, one is to accept visitors visiting the abstract method, followed by the implementation of specific elements of the class:
1 Public classCOncreteElement2extendselement{2 //have a big meal3 Public voiddosth () {4System.out.println ("Eat a big meal");5 }6 Public voidAccept (Ivisitor visitor) {7Visitor.visit ( This);8 }9}
This is the implementation of the first abstract class, in fact, is one of the theme of the meal, eating Satan, followed by the second major theme of the dinner, drink Satan, did not hear the leadership often said: "Everyone eat good drink ah ~":
1 Public classConcreteElement1extendselement{2 //drinking3 Publicviod dosth () {4SYSTEM.OUT.PRINTLN ("Dinner is for drinking");5 }6 Public voidAccept (Ivisitor visitor) {7Visitor.visit ( This);8 } 9}
View Code
Next, it is the interface visitor:
1 Public Interface ivisitor{2 Public void visit (ConcreteElement1 el1); 3 Public void visit (ConcreteElement2 el2); 4 }
What, isn't it super simple? In fact, there are a few specific elements of the class, there are several access methods corresponding to it. Then there is the implementation of the specific visitor:
1 Public classVisitorImplementsivisitor{2 Public voidvisit (ConcreteElement1 el1) {3 el1.dosth ();4 }5 Public voidvisit (ConcreteElement2 el2) {6 el2.dosth ();7 }8}
In fact, two elements of access, nothing. A struct object is an interface that provides an access to an object:
1 Public classobjectstruture{2 //Object Builder3 Public StaticElement createelement () {4 //toss a coin, decide whether to eat first or drink first5Random Rand =NewRandom ();6 if(Rand.nextint (2) >1){7 return NewConcreteElement1 ();8 }9 Else {Ten return NewConcreteElement2 (); One } A } -}
View Code
Okay, here's the implementation.
Application Scenarios for visitor patterns
- An object structure contains many classes of objects, and they have different interfaces, and you want to implement some operations that depend on their specific classes for these objects. Use me.
- When you need to do a lot of different and unrelated things to an object in an object structure, you don't want these operations to "contaminate" these object classes. Please use the visitor.
- When you define object structures, the classes rarely change, but often need to define new operations in this structure. Come and try the visitors.
Not early, should go back to wash and sleep, fish elder brother still waiting for me to drink, then goodbye you now ~ How to foresee funeral, and listen to tell within you that ~
PS: This blog welcome forwarding, but please specify the blog address and author ~
Blog Address: http://www.cnblogs.com/voidy/
Blog: http://voidy.net
<. ))) ≦
The 20th chapter of the design pattern-visitor Mode (Java implementation)