mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-13 12:14:51 +00:00
docs: collecting parameter docs + formatting
This commit is contained in:
1 parent
ea7bc2a4eb
commit
f80cc468b2
23 files changed
+1191
-794
No files matched your search
+18
-10
@@ -9,14 +9,15 @@ tag:
|
||||
|
||||
## Intent
|
||||
|
||||
The Acyclic Visitor pattern decouples operations from an object hierarchy, allowing you to add new operations without modifying the object structure directly.
|
||||
The Acyclic Visitor pattern decouples operations from an object hierarchy, allowing you to add new operations without
|
||||
modifying the object structure directly.
|
||||
|
||||
## Explanation
|
||||
|
||||
Real world example
|
||||
|
||||
> We have a hierarchy of modem classes. The modems in this hierarchy need to be visited by an external algorithm based
|
||||
> on filtering criteria (is it Unix or DOS compatible modem).
|
||||
> We have a hierarchy of modem classes. The modems in this hierarchy need to be visited by an external algorithm based
|
||||
> on filtering criteria (is it Unix or DOS compatible modem).
|
||||
|
||||
In plain words
|
||||
|
||||
@@ -24,7 +25,7 @@ In plain words
|
||||
|
||||
[WikiWikiWeb](https://wiki.c2.com/?AcyclicVisitor) says
|
||||
|
||||
> The Acyclic Visitor pattern allows new functions to be added to existing class hierarchies without affecting those
|
||||
> The Acyclic Visitor pattern allows new functions to be added to existing class hierarchies without affecting those
|
||||
> hierarchies, and without creating the dependency cycles that are inherent to the GangOfFour VisitorPattern.
|
||||
|
||||
**Programmatic Example**
|
||||
@@ -38,6 +39,7 @@ public abstract class Modem {
|
||||
|
||||
public class Zoom extends Modem {
|
||||
...
|
||||
|
||||
@Override
|
||||
public void accept(ModemVisitor modemVisitor) {
|
||||
if (modemVisitor instanceof ZoomVisitor) {
|
||||
@@ -50,6 +52,7 @@ public class Zoom extends Modem {
|
||||
|
||||
public class Hayes extends Modem {
|
||||
...
|
||||
|
||||
@Override
|
||||
public void accept(ModemVisitor modemVisitor) {
|
||||
if (modemVisitor instanceof HayesVisitor) {
|
||||
@@ -80,10 +83,12 @@ public interface AllModemVisitor extends ZoomVisitor, HayesVisitor {
|
||||
|
||||
public class ConfigureForDosVisitor implements AllModemVisitor {
|
||||
...
|
||||
|
||||
@Override
|
||||
public void visit(Hayes hayes) {
|
||||
LOGGER.info(hayes + " used with Dos configurator.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(Zoom zoom) {
|
||||
LOGGER.info(zoom + " used with Dos configurator.");
|
||||
@@ -92,6 +97,7 @@ public class ConfigureForDosVisitor implements AllModemVisitor {
|
||||
|
||||
public class ConfigureForUnixVisitor implements ZoomVisitor {
|
||||
...
|
||||
|
||||
@Override
|
||||
public void visit(Zoom zoom) {
|
||||
LOGGER.info(zoom + " used with Unix configurator.");
|
||||
@@ -102,10 +108,10 @@ public class ConfigureForUnixVisitor implements ZoomVisitor {
|
||||
Finally, here are the visitors in action.
|
||||
|
||||
```java
|
||||
var conUnix = new ConfigureForUnixVisitor();
|
||||
var conDos = new ConfigureForDosVisitor();
|
||||
var zoom = new Zoom();
|
||||
var hayes = new Hayes();
|
||||
var conUnix=new ConfigureForUnixVisitor();
|
||||
var conDos=new ConfigureForDosVisitor();
|
||||
var zoom=new Zoom();
|
||||
var hayes=new Hayes();
|
||||
hayes.accept(conDos);
|
||||
zoom.accept(conDos);
|
||||
hayes.accept(conUnix);
|
||||
@@ -130,7 +136,8 @@ Program output:
|
||||
This pattern can be used:
|
||||
|
||||
* When you need to add a new function to an existing hierarchy without the need to alter or affect that hierarchy.
|
||||
* When there are functions that operate upon a hierarchy, but which do not belong in the hierarchy itself. e.g. the ConfigureForDOS / ConfigureForUnix / ConfigureForX issue.
|
||||
* When there are functions that operate upon a hierarchy, but which do not belong in the hierarchy itself. e.g. the
|
||||
ConfigureForDOS / ConfigureForUnix / ConfigureForX issue.
|
||||
* When you need to perform very different operations on an object depending upon its type.
|
||||
* When the visited class hierarchy will be frequently extended with new derivatives of the Element class.
|
||||
* When the recompilation, relinking, retesting or redistribution of the derivatives of Element is very expensive.
|
||||
@@ -149,7 +156,8 @@ Benefits:
|
||||
|
||||
Trade-offs:
|
||||
|
||||
* Violates [Liskov's Substitution Principle](https://java-design-patterns.com/principles/#liskov-substitution-principle) by showing that it can accept all visitors but actually only being interested in particular visitors.
|
||||
* Violates [Liskov's Substitution Principle](https://java-design-patterns.com/principles/#liskov-substitution-principle)
|
||||
by showing that it can accept all visitors but actually only being interested in particular visitors.
|
||||
* Parallel hierarchy of visitors has to be created for all members in visitable class hierarchy.
|
||||
|
||||
## Related patterns
|
||||
|
||||
Reference in new issue
Block a user