mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-13 04:15:07 +00:00
docs: update visitor
This commit is contained in:
1 parent
cca57bfa5e
commit
2bbf893133
2 files changed
+33
-16
No files matched your search
+33
-15
@@ -50,6 +50,9 @@ public abstract class Unit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
public interface UnitVisitor {
|
public interface UnitVisitor {
|
||||||
|
|
||||||
void visit(Soldier soldier);
|
void visit(Soldier soldier);
|
||||||
@@ -80,7 +83,9 @@ public class Commander extends Unit {
|
|||||||
return "commander";
|
return "commander";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
public class Sergeant extends Unit {
|
public class Sergeant extends Unit {
|
||||||
|
|
||||||
public Sergeant(Unit... children) {
|
public Sergeant(Unit... children) {
|
||||||
@@ -98,7 +103,9 @@ public class Sergeant extends Unit {
|
|||||||
return "sergeant";
|
return "sergeant";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
public class Soldier extends Unit {
|
public class Soldier extends Unit {
|
||||||
|
|
||||||
public Soldier(Unit... children) {
|
public Soldier(Unit... children) {
|
||||||
@@ -139,7 +146,9 @@ public class CommanderVisitor implements UnitVisitor {
|
|||||||
LOGGER.info("Good to see you {}", commander);
|
LOGGER.info("Good to see you {}", commander);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class SergeantVisitor implements UnitVisitor {
|
public class SergeantVisitor implements UnitVisitor {
|
||||||
|
|
||||||
@@ -158,7 +167,9 @@ public class SergeantVisitor implements UnitVisitor {
|
|||||||
// Do nothing
|
// Do nothing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class SoldierVisitor implements UnitVisitor {
|
public class SoldierVisitor implements UnitVisitor {
|
||||||
|
|
||||||
@@ -182,23 +193,30 @@ public class SoldierVisitor implements UnitVisitor {
|
|||||||
Finally, we can show the power of visitors in action.
|
Finally, we can show the power of visitors in action.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
commander.accept(new SoldierVisitor());
|
public static void main(String[] args) {
|
||||||
commander.accept(new SergeantVisitor());
|
|
||||||
commander.accept(new CommanderVisitor());
|
var commander = new Commander(
|
||||||
|
new Sergeant(new Soldier(), new Soldier(), new Soldier()),
|
||||||
|
new Sergeant(new Soldier(), new Soldier(), new Soldier())
|
||||||
|
);
|
||||||
|
commander.accept(new SoldierVisitor());
|
||||||
|
commander.accept(new SergeantVisitor());
|
||||||
|
commander.accept(new CommanderVisitor());
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Program output:
|
Program output:
|
||||||
|
|
||||||
```
|
```
|
||||||
Greetings soldier
|
14:58:06.115 [main] INFO com.iluwatar.visitor.SoldierVisitor -- Greetings soldier
|
||||||
Greetings soldier
|
14:58:06.118 [main] INFO com.iluwatar.visitor.SoldierVisitor -- Greetings soldier
|
||||||
Greetings soldier
|
14:58:06.118 [main] INFO com.iluwatar.visitor.SoldierVisitor -- Greetings soldier
|
||||||
Greetings soldier
|
14:58:06.118 [main] INFO com.iluwatar.visitor.SoldierVisitor -- Greetings soldier
|
||||||
Greetings soldier
|
14:58:06.118 [main] INFO com.iluwatar.visitor.SoldierVisitor -- Greetings soldier
|
||||||
Greetings soldier
|
14:58:06.118 [main] INFO com.iluwatar.visitor.SoldierVisitor -- Greetings soldier
|
||||||
Hello sergeant
|
14:58:06.118 [main] INFO com.iluwatar.visitor.SergeantVisitor -- Hello sergeant
|
||||||
Hello sergeant
|
14:58:06.118 [main] INFO com.iluwatar.visitor.SergeantVisitor -- Hello sergeant
|
||||||
Good to see you commander
|
14:58:06.118 [main] INFO com.iluwatar.visitor.CommanderVisitor -- Good to see you commander
|
||||||
```
|
```
|
||||||
|
|
||||||
## Class diagram
|
## Class diagram
|
||||||
@@ -215,9 +233,9 @@ Use the Visitor pattern when
|
|||||||
|
|
||||||
## Tutorials
|
## Tutorials
|
||||||
|
|
||||||
* [Visitor - Refactoring Guru](https://refactoring.guru/design-patterns/visitor)
|
* [Visitor (Refactoring Guru)](https://refactoring.guru/design-patterns/visitor)
|
||||||
* [Visitor Pattern Tutorial with Java Examples - DZone](https://dzone.com/articles/design-patterns-visitor)
|
* [Visitor Pattern Tutorial with Java Examples (DZone)](https://dzone.com/articles/design-patterns-visitor)
|
||||||
* [Visitor Design Pattern - Sourcemaking](https://sourcemaking.com/design_patterns/visitor)
|
* [Visitor Design Pattern (Sourcemaking)](https://sourcemaking.com/design_patterns/visitor)
|
||||||
|
|
||||||
## Known uses
|
## Known uses
|
||||||
|
|
||||||
|
|||||||
@@ -48,6 +48,5 @@ public class App {
|
|||||||
commander.accept(new SoldierVisitor());
|
commander.accept(new SoldierVisitor());
|
||||||
commander.accept(new SergeantVisitor());
|
commander.accept(new SergeantVisitor());
|
||||||
commander.accept(new CommanderVisitor());
|
commander.accept(new CommanderVisitor());
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user