@@ -50,6 +50,9 @@ public abstract class Unit {
50
50
}
51
51
}
52
52
53
+ ```
54
+
55
+ ``` java
53
56
public interface UnitVisitor {
54
57
55
58
void visit (Soldier soldier );
@@ -80,7 +83,9 @@ public class Commander extends Unit {
80
83
return " commander" ;
81
84
}
82
85
}
86
+ ```
83
87
88
+ ``` java
84
89
public class Sergeant extends Unit {
85
90
86
91
public Sergeant (Unit ... children ) {
@@ -98,7 +103,9 @@ public class Sergeant extends Unit {
98
103
return " sergeant" ;
99
104
}
100
105
}
106
+ ```
101
107
108
+ ``` java
102
109
public class Soldier extends Unit {
103
110
104
111
public Soldier (Unit ... children ) {
@@ -139,7 +146,9 @@ public class CommanderVisitor implements UnitVisitor {
139
146
LOGGER . info(" Good to see you {}" , commander);
140
147
}
141
148
}
149
+ ```
142
150
151
+ ``` java
143
152
@Slf4j
144
153
public class SergeantVisitor implements UnitVisitor {
145
154
@@ -158,7 +167,9 @@ public class SergeantVisitor implements UnitVisitor {
158
167
// Do nothing
159
168
}
160
169
}
170
+ ```
161
171
172
+ ``` java
162
173
@Slf4j
163
174
public class SoldierVisitor implements UnitVisitor {
164
175
@@ -182,23 +193,30 @@ public class SoldierVisitor implements UnitVisitor {
182
193
Finally, we can show the power of visitors in action.
183
194
184
195
``` 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
+ }
188
206
```
189
207
190
208
Program output:
191
209
192
210
```
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
202
220
```
203
221
204
222
## Class diagram
@@ -215,9 +233,9 @@ Use the Visitor pattern when
215
233
216
234
## Tutorials
217
235
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 )
221
239
222
240
## Known uses
223
241
0 commit comments