Skip to content

Commit 95e513b

Browse files
committed
Edit readme
1 parent 1940405 commit 95e513b

1 file changed

Lines changed: 31 additions & 17 deletions

File tree

‎visitor/README.md‎

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,28 @@ tags:
99
---
1010

1111
## Intent
12-
Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.
12+
13+
Represent an operation to be performed on the elements of an object structure. Visitor lets you
14+
define a new operation without changing the classes of the elements on which it operates.
1315

1416
## Explanation
1517

1618
Real world example
1719

18-
> Consider a tree structure with army units. Commander has two sergeants under it and each sergeant has three soldiers under them. Given that the hierarchy implements the visitor pattern, we can easily create new objects that interact with the commander, sergeants, soldiers or all of them.
20+
> Consider a tree structure with army units. Commander has two sergeants under it and each sergeant
21+
> has three soldiers under them. Given that the hierarchy implements the visitor pattern, we can
22+
> easily create new objects that interact with the commander, sergeants, soldiers or all of them.
1923
2024
In plain words
2125

2226
> Visitor pattern defines operations that can be performed on the nodes of the data structure.
2327
2428
Wikipedia says
2529

26-
> In object-oriented programming and software engineering, the visitor design pattern is a way of separating an algorithm from an object structure on which it operates. A practical result of this separation is the ability to add new operations to existing object structures without modifying the structures.
30+
> In object-oriented programming and software engineering, the visitor design pattern is a way of
31+
> separating an algorithm from an object structure on which it operates. A practical result of this
32+
> separation is the ability to add new operations to existing object structures without modifying
33+
> the structures.
2734
2835
**Programmatic Example**
2936

@@ -111,7 +118,7 @@ public class Soldier extends Unit {
111118
}
112119
```
113120

114-
And then some concrete visitors.
121+
Here are then some concrete visitors.
115122

116123
```java
117124
public class CommanderVisitor implements UnitVisitor {
@@ -175,32 +182,39 @@ public class SoldierVisitor implements UnitVisitor {
175182
}
176183
```
177184

178-
Finally we can show the power of visitors in action.
185+
Finally, we can show the power of visitors in action.
179186

180187
```java
181188
commander.accept(new SoldierVisitor());
182-
// Greetings soldier
183-
// Greetings soldier
184-
// Greetings soldier
185-
// Greetings soldier
186-
// Greetings soldier
187-
// Greetings soldier
188189
commander.accept(new SergeantVisitor());
189-
// Hello sergeant
190-
// Hello sergeant
191190
commander.accept(new CommanderVisitor());
192-
// Good to see you commander
191+
```
192+
193+
Program output:
194+
195+
```
196+
Greetings soldier
197+
Greetings soldier
198+
Greetings soldier
199+
Greetings soldier
200+
Greetings soldier
201+
Greetings soldier
202+
Hello sergeant
203+
Hello sergeant
204+
Good to see you commander
193205
```
194206

195207
## Class diagram
208+
196209
![alt text](./etc/visitor_1.png "Visitor")
197210

198211
## Applicability
212+
199213
Use the Visitor pattern when
200214

201-
* An object structure contains many classes of objects with differing interfaces, and you want to perform operations on these objects that depend on their concrete classes
202-
* Many distinct and unrelated operations need to be performed on objects in an object structure, and you want to avoid "polluting" their classes with these operations. Visitor lets you keep related operations together by defining them in one class. When the object structure is shared by many applications, use Visitor to put operations in just those applications that need them
203-
* The classes defining the object structure rarely change, but you often want to define new operations over the structure. Changing the object structure classes requires redefining the interface to all visitors, which is potentially costly. If the object structure classes change often, then it's probably better to define the operations in those classes
215+
* An object structure contains many classes of objects with differing interfaces, and you want to perform operations on these objects that depend on their concrete classes.
216+
* Many distinct and unrelated operations need to be performed on objects in an object structure, and you want to avoid "polluting" their classes with these operations. Visitor lets you keep related operations together by defining them in one class. When the object structure is shared by many applications, use Visitor to put operations in just those applications that need them.
217+
* The classes defining the object structure rarely change, but you often want to define new operations over the structure. Changing the object structure classes requires redefining the interface to all visitors, which is potentially costly. If the object structure classes change often, then it's probably better to define the operations in those classes.
204218

205219
## Real world examples
206220

0 commit comments

Comments
 (0)