Repository: iluwatar/java-design-patterns
Issue: ClassCastException was throwed in App.java#52
Labels: type: bug

The row that has an exception was:
hayes.accept(conUnix); // Hayes modem with Unix configurator

Because of ConfigureForUnixVisitor didn't implements HayesVisitor.

#java-design-patterns/acyclic-visitor

Issue comments:
@iluwatar That is not a bug, it is expected to happen. I will change the log level and use `instanceof` instead to resolve the confusion.
Well done @npathai Thank you!

Pull request review comments:

Patch that resolved the issue:
diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/AllModemVisitor.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/AllModemVisitor.java
index e6f5834462d1..749ea00a25fb 100644
--- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/AllModemVisitor.java
+++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/AllModemVisitor.java
@@ -23,8 +23,9 @@
 package com.iluwatar.acyclicvisitor;
 
 /**
- * All ModemVisitor interface extends all visitor interfaces
+ * All ModemVisitor interface extends all visitor interfaces. This interface
+ * provides ease of use when a visitor needs to visit all modem types.
  */
-public interface AllModemVisitor extends ModemVisitor, ZoomVisitor, HayesVisitor{
+public interface AllModemVisitor extends ZoomVisitor, HayesVisitor{
 
 }
diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitor.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitor.java
index d3ef3bf6c018..c3546012166d 100644
--- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitor.java
+++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitor.java
@@ -27,9 +27,10 @@
 
 /**
  * ConfigureForUnixVisitor class implements zoom's visit method for Unix 
- * manufacturer
+ * manufacturer, unlike traditional visitor pattern, this class may selectively implement
+ * visit for other modems.
  */
-public class ConfigureForUnixVisitor implements ModemVisitor, ZoomVisitor {
+public class ConfigureForUnixVisitor implements ZoomVisitor {
 
   private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureForUnixVisitor.class);
 
diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Hayes.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Hayes.java
index 1128057bfd76..3ee76a812b49 100644
--- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Hayes.java
+++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Hayes.java
@@ -37,10 +37,10 @@ public class Hayes extends Modem {
    */
   @Override
   public void accept(ModemVisitor modemVisitor) {
-    try {
+    if (modemVisitor instanceof HayesVisitor) {
       ((HayesVisitor) modemVisitor).visit(this);
-    } catch (ClassCastException e) {
-      LOGGER.error("Unable to cast to HayesVisitor");
+    } else {
+      LOGGER.info("Only HayesVisitor is allowed to visit Hayes modem");
     }
 
   }
diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Zoom.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Zoom.java
index 7091d79dfff8..ee054851e514 100644
--- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Zoom.java
+++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Zoom.java
@@ -37,10 +37,10 @@ public class Zoom extends Modem {
    */
   @Override
   public void accept(ModemVisitor modemVisitor) {
-    try {
+    if (modemVisitor instanceof  ZoomVisitor) {
       ((ZoomVisitor) modemVisitor).visit(this);
-    } catch (ClassCastException e) {
-      LOGGER.error("Unable to cast to ZoomVisitor");
+    } else {
+      LOGGER.info("Only ZoomVisitor is allowed to visit Zoom modem");
     }
   }
   


Test patch: