Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Visitor pattern of the Design pattern

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "what is the Visitor pattern". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Visitor Mode ❝

The operations acting on the elements in a certain data structure are separated and encapsulated into independent classes, so that new operations acting on these elements can be added without changing the data structure. provides multiple access methods for each element in the data structure. It separates the operation of the data from the data structure, and is one of the most complex behavioral patterns.

❞, come straight to the point.

The visitor pattern is "people like their names", which means that different visitors have different access to the same object. Why is it different? Because this visitor is defined by ourselves, we want him to be different.

And the actual situation is even more so. I summed up from several sources that the so-called visitors in this visitor model are actually the same access rights that we want to control. Because any "visitor" can see all the contents of the specific data, he only selectively "does not look", thus distinguishing the "visitor" from the "content" or "restricting" the visitor's "permission".

Maybe what I said is a little roundabout, a little sorry, let me simplify this content.

Network language

"put aside the appearance and look at the essence."

If we put aside the professional definition of the visitor pattern and simply understand the meaning of the visitor pattern, I think it is most appropriate to use one thing. That's "Internet language."

I don't know if you have heard the popular Baidu ad "what are you talking about" single? The Chaoyang aunt in the song is a visitor who does not know the focus or is restricted. Of course, there are many network languages spoken in his song. I don't know what it is. If you've never heard of it, go and hear it.

And the recent Versailles literature I thought it was a place name when I was not Baidu, so my authority was also restricted.

I happen to mention this, so let's use Versailles literature as a network language to learn the visitor model.

Versailles literature

"first of all, we have to put aside the appearance and look at the essence again."

Here is the interpretation I have collected about Versailles:

Versailles, the satellite city of Paris and the provincial capital of Evelyn, was once the administrative center of the French dynasty. Versailles is a feature film directed by Pierre Sule. A TV series set against the background of Louis XIV of France. Versailles literature, a hot word on the Internet, refers to inadvertently revealing "clues to aristocratic life" from the perspective of suppression, self-answer or the third person. What?

There are five interpretations of Versailles, and his structure should be like this.

Public class Versailles {

Private final String interpretation1 = "Versailles is the satellite city of Paris and the provincial capital of Evelyn. It was once the administrative center of the French dynasty."

Private final String interpretation2 = "Versailles" is a drama directed by Pierre Sule. "

Private final String interpretation3 = "TV series set in Louis XIV of France."

Private final String interpretation4 = "Versailles literature, a buzzword on the Internet, refers to the inadvertent exposure of" clues to aristocratic life "through the perspective of suppression, self-answer, or the third person."

Private final String interpretation5 = "what?"

}

Because we still have to access this data, we also need to add an access method # visit to it.

Public class Versailles {

.

....

...

/ *

* visit

, /

Public void visit () {

}

}

Since you want to visit, there must be visitors, because there are a lot of visitors, such as me, my little partner, and you.

So we use the dependency inversion principle to define a visitor interface Visitor and then have an access method, and then give Versailles to the visitor to access it himself, and the code should be implemented like this.

Visitor interface

Public interface Visitor {

Void visit (Versailles versailles)

}

Adjust the access method of Versailles, and the final complete class is as follows

Public class Versailles {

Private final String interpretation1 = "Versailles is the satellite city of Paris and the provincial capital of Evelyn. It was once the administrative center of the French dynasty."

Private final String interpretation2 = "Versailles" is a feature film directed by Pierre Sule. "

Private final String interpretation3 = "TV series set in Louis XIV of France."

Private final String interpretation4 = "Versailles literature, a buzzword on the Internet, refers to the inadvertent exposure of" clues to aristocratic life "through the perspective of suppression, self-answer, or the third person."

Private final String interpretation5 = "what?"

/ * *

* provide the object to visitors for access

* @ param visitor visitors

* it is better to change the name of the method to accept, which means that the class accepts a visitor to access itself.

, /

Public void accept (Visitor visitor) {

Visitor.visit (this)

}

}

Then there are the specific visitors. I'll define some visitors according to the actual situation.

IMyFriendYou

There are three visitors.

"I" (myself)

I am more knowledgeable and knowledgeable. I know that Versailles is a place name, a movie and a TV show.

"MyFriend" (dog brother)

I don't know the way he looks.

Brother Dog doesn't know anything.

"You" how about you? )

I'll take it that you know the literature of Versailles and have realized the realm of invisible pretence.

Take a look at these three categories.

/ * *

* I am more knowledgeable and knowledgeable

*

* Welcome to study with me, Wechat (lvgocc) official account search: a friend of Stardust

*

* @ author lvgorice@gmail.com

* @ version 1.0

* @ blog @ see http://lvgo.org

* @ CSDN @ see https://blog.csdn.net/sinat_34344123

* @ date 2020-12-1

, /

Public class I implements Visitor {

@ Override

Public void visit (Versailles versailles) {

System.out.println (versailles.getInterpretation1 ())

System.out.println (versailles.getInterpretation2 ())

System.out.println (versailles.getInterpretation3 ())

}

}

/ / Brother mentally retarded Dog

Public class MyFriend implements Visitor {

@ Override

Public void visit (Versailles versailles) {

System.out.println (versailles.getInterpretation5 ())

}

}

/ / Internet talent

Public class You implements Visitor {

@ Override

Public void visit (Versailles versailles) {

System.out.println (versailles.getInterpretation4 ())

}

}

Finally, we're going to simulate what's going on.

Class VisitorTest {

@ Test

Void visit () {

Versailles versailles = new Versailles ()

System.out.println ("lvgo, do you know Versailles?")

Versailles.accept (new I ())

System.out.println (\ nDog, do you know Versailles?)

Versailles.accept (new MyFriend ())

System.out.println ("\ n do you know Versailles?")

Versailles.accept (new You ())

}

}

As a result, the dog pulls the hip.

Lvgo, do you know Versailles?

Versailles, the satellite city of Paris and the provincial capital of Evelyn, was once the administrative center of the French dynasty.

Versailles is a feature film directed by Pierre Sule.

A TV series set against the background of Louis XIV of France.

Dog, do you know Versailles?

What?

Do you know Versailles?

Versailles literature, a hot word on the Internet, refers to inadvertently revealing "clues to aristocratic life" from the perspective of suppression, self-answer or the third person.

Different visitors see different results in the data structure. Let's take a look at the definition of visitors.

Multiple access methods are provided for each element in a collection without changing the collection elements, that is, each element has multiple visitor object access.

Although we are using an object here, try to turn it into a collection (multiple network languages). Use a loop to "send" each element to the visitor. Save this for you to try, and leave it for yourself to be forced to use your brain when you come back to see it later. Really do not want to move, pay attention to reply to the "source code"! ?

Visitor pattern class diagram?

Finally, let's take a look at the standard visitor pattern structure diagram

Visitor class diagram

This structure is rather complicated.

Client-side high-level module Client; visitor interface, dependent on inverted interface Visitor; accessed element, Element; last, ObjectStructure object structure

The only thing that may need to be explained here is the ObjectStructure, which is used to define the object carrier that manages the Element. It can be any carrier object that needs to be accessed in our business scenario. For example, in the above example, if we want to put this structure into it, then I can define a word class Word, which can have NetWordLanguage,Professional vocabulary and other objects. As shown below

Public class Word {

/ * *

* Network language

, /

Private final List netWordLanguages = new ArrayList ()

Public void addWord (NetWordLanguage netWordLanguage) {

If (! netWordLanguages.contains (netWordLanguage)) {

NetWordLanguages.add (netWordLanguage)

}

}

} ❝

Visitors follow all the source code and reply to get the "source code"

This is the end of ❞'s "what is the Visitor pattern". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report