Introduced
Concept: represents an operation that acts on elements in an object's structure. It allows you to define new actions that act on these elements without changing the class of each element.
Scenario: There are differences in internal attributes between entities, and the entity types often increase. They are called the same way, but the rules of the call change frequently.
Cons: Special Content access classes for entities need to be known.
Code:
Entity base class
Package Note.com.visitor; Public Abstract class Food { publicabstractvoid Show (Ivisitor visitor);}
Specific entities
PackageNote.com.visitor;/*** Noodles *@authorBr **/ Public classNoodleextendsfood{PrivateString Price = "5 Block"; PrivateString image = "* * *"; PrivateString Fooda = "Nutrition 1"; PrivateString Foodb = "Nutrition 2"; PrivateString FOODC = "Nutrition 3"; @Override Public voidShow (Ivisitor visitor) {Visitor.show ( This); } PublicString GetPrice () {returnPrice ; } Public voidSetprice (String price) { This. Price =Price ; } PublicString getImage () {returnimage; } Public voidsetimage (String image) { This. Image =image; } PublicString Getfooda () {returnFooda; } Public voidSetfooda (String fooda) { This. Fooda =Fooda; } PublicString Getfoodb () {returnFoodb; } Public voidSetfoodb (String foodb) { This. Foodb =Foodb; } PublicString getfoodc () {returnfoodc; } Public voidsetfoodc (String foodc) { This. FOODC =foodc; }}
PackageNote.com.visitor;/*** M *@authorBr **/ Public classSusanextendsfood{PrivateString Price = "2 Block"; PrivateString image = "&&&"; PrivateString Fooda = "Nutrition 1"; PrivateString foodd = "Nutrition 2"; PrivateString Foode = "Nutrition 3"; @Override Public voidShow (Ivisitor visitor) {Visitor.show ( This); } PublicString GetPrice () {returnPrice ; } Public voidSetprice (String price) { This. Price =Price ; } PublicString getImage () {returnimage; } Public voidsetimage (String image) { This. Image =image; } PublicString Getfooda () {returnFooda; } Public voidSetfooda (String fooda) { This. Fooda =Fooda; } PublicString getfoodd () {returnfoodd; } Public voidsetfoodd (String foodd) { This. foodd =foodd; } PublicString Getfoode () {returnFoode; } Public voidSetfoode (String foode) { This. Foode =Foode; }}
PackageNote.com.visitor;/*** Soup *@authorBr **/ Public classSoupextendsfood{PrivateString Price = "1 Block"; PrivateString image = "# # #"; PrivateString Fooda = "Nutrition 1"; PrivateString FOODF = "Nutrition 2"; PrivateString FOODG = "Nutrition 3"; @Override Public voidShow (Ivisitor visitor) {Visitor.show ( This); } PublicString GetPrice () {returnPrice ; } Public voidSetprice (String price) { This. Price =Price ; } PublicString getImage () {returnimage; } Public voidsetimage (String image) { This. Image =image; } PublicString Getfooda () {returnFooda; } Public voidSetfooda (String fooda) { This. Fooda =Fooda; } PublicString getfoodf () {returnFOODF; } Public voidsetfoodf (String foodf) { This. FOODF =FOODF; } PublicString getfoodg () {returnFOODG; } Public voidSETFOODG (String foodg) { This. FOODG =FOODG; }}
Visitor Interface Definition
Package Note.com.visitor; Public Interface Ivisitor { publicvoid showmenu (food foodo); Public void Show (Noodle Noodle); Public void Show (rice); Public void Show (Soup Soup);}
Visitor implementations
PackageNote.com.visitor;/*** Menu Display information (visitor: Get to be visitor, display the desired information as required) *@authorBr **/ Public classVisitorImplementsivisitor{@Override Public voidShowMenu (food food) {Food.show ( This); } //The following method destroys the encapsulation feature because it is clear what methods the object has, properties Public voidShow (Noodle Noodle) {String show= Noodle.getprice () + "" +noodle.getimage () + "" +noodle.getfooda () + "" +noodle.getfoodb () + "" +noodle.getfoodc (); System.out.println (show); } Public voidShow (Rice) {String show= Rice.getprice () + "" +rice.getimage () + "" +rice.getfooda () + "" +rice.getfoodd () + "" +Rice.getfoode (); System.out.println (show); } Public voidShow (Soup Soup) {String show= Soup.getprice () + "" +soup.getimage () + "" +soup.getfooda () + "" +SOUP.GETFOODF () + "" +SOUP.GETFOODG (); System.out.println (show); } }
Combination of visitor and entity class
PackageNote.com.visitor;Importjava.util.ArrayList;Importjava.util.List; Public classMenu {Privatelist<food> list =NewArraylist<food>(); /*Show Menu **/ Public voidShowMenu (Ivisitor visitor) { for(food f:list) {visitor.showmenu (f); } } Public voidAddfood (food food) {list.add (food); } }
Test class
PackageNote.com.visitor; Public classTest { Public Static voidMain (string[] args) {Menu Menu=NewMenu (); //Add three entitiesMenu.addfood (NewNoodle ()); Menu.addfood (NewRice ()); Menu.addfood (NewSoup ()); //incoming visitors get menu contentMenu.showmenu (NewVisitor ()); }}
Results:
5 Yuan * * * Nutrition 1 Nutrition 2 Nutrition 3
2 Yuan &&& Nutrition 1 Nutrition 2 Nutrition 3
1 Block # # # nutrition 1 Nutrition 2 Nutrition 3
Benefits:
1, if you want to change the specific display of the entities in the menu, you only need to change the access rules of the visitor class.
2, if you want to add a new entity, you only need to extend the visitor class's access rules.
(21) Visitor mode-code implementation