@@ -50,6 +50,9 @@ public abstract class Unit {
5050 }
5151}
5252
53+ ```
54+
55+ ``` java
5356public interface UnitVisitor {
5457
5558 void visit (Soldier soldier );
@@ -80,7 +83,9 @@ public class Commander extends Unit {
8083 return " commander" ;
8184 }
8285}
86+ ```
8387
88+ ``` java
8489public class Sergeant extends Unit {
8590
8691 public Sergeant (Unit ... children ) {
@@ -98,7 +103,9 @@ public class Sergeant extends Unit {
98103 return " sergeant" ;
99104 }
100105}
106+ ```
101107
108+ ``` java
102109public class Soldier extends Unit {
103110
104111 public Soldier (Unit ... children ) {
@@ -139,7 +146,9 @@ public class CommanderVisitor implements UnitVisitor {
139146 LOGGER . info(" Good to see you {}" , commander);
140147 }
141148}
149+ ```
142150
151+ ``` java
143152@Slf4j
144153public class SergeantVisitor implements UnitVisitor {
145154
@@ -158,7 +167,9 @@ public class SergeantVisitor implements UnitVisitor {
158167 // Do nothing
159168 }
160169}
170+ ```
161171
172+ ``` java
162173@Slf4j
163174public class SoldierVisitor implements UnitVisitor {
164175
@@ -182,23 +193,30 @@ public class SoldierVisitor implements UnitVisitor {
182193Finally, we can show the power of visitors in action.
183194
184195``` java
185- commander. accept(new SoldierVisitor ());
186- commander. accept(new SergeantVisitor ());
187- commander. accept(new CommanderVisitor ());
196+ public static void main(String [] args) {
197+
198+ var commander = new Commander (
199+ new Sergeant (new Soldier (), new Soldier (), new Soldier ()),
200+ new Sergeant (new Soldier (), new Soldier (), new Soldier ())
201+ );
202+ commander. accept(new SoldierVisitor ());
203+ commander. accept(new SergeantVisitor ());
204+ commander. accept(new CommanderVisitor ());
205+ }
188206```
189207
190208Program output:
191209
192210```
193- Greetings soldier
194- Greetings soldier
195- Greetings soldier
196- Greetings soldier
197- Greetings soldier
198- Greetings soldier
199- Hello sergeant
200- Hello sergeant
201- Good to see you commander
211+ 14:58:06.115 [main] INFO com.iluwatar.visitor.SoldierVisitor -- Greetings soldier
212+ 14:58:06.118 [main] INFO com.iluwatar.visitor.SoldierVisitor -- Greetings soldier
213+ 14:58:06.118 [main] INFO com.iluwatar.visitor.SoldierVisitor -- Greetings soldier
214+ 14:58:06.118 [main] INFO com.iluwatar.visitor.SoldierVisitor -- Greetings soldier
215+ 14:58:06.118 [main] INFO com.iluwatar.visitor.SoldierVisitor -- Greetings soldier
216+ 14:58:06.118 [main] INFO com.iluwatar.visitor.SoldierVisitor -- Greetings soldier
217+ 14:58:06.118 [main] INFO com.iluwatar.visitor.SergeantVisitor -- Hello sergeant
218+ 14:58:06.118 [main] INFO com.iluwatar.visitor.SergeantVisitor -- Hello sergeant
219+ 14:58:06.118 [main] INFO com.iluwatar.visitor.CommanderVisitor -- Good to see you commander
202220```
203221
204222## Class diagram
@@ -215,9 +233,9 @@ Use the Visitor pattern when
215233
216234## Tutorials
217235
218- * [ Visitor - Refactoring Guru] ( https://refactoring.guru/design-patterns/visitor )
219- * [ Visitor Pattern Tutorial with Java Examples - DZone] ( https://dzone.com/articles/design-patterns-visitor )
220- * [ Visitor Design Pattern - Sourcemaking] ( https://sourcemaking.com/design_patterns/visitor )
236+ * [ Visitor ( Refactoring Guru) ] ( https://refactoring.guru/design-patterns/visitor )
237+ * [ Visitor Pattern Tutorial with Java Examples ( DZone) ] ( https://dzone.com/articles/design-patterns-visitor )
238+ * [ Visitor Design Pattern ( Sourcemaking) ] ( https://sourcemaking.com/design_patterns/visitor )
221239
222240## Known uses
223241
0 commit comments