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 384df8a4d9c9..359c2db04634 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Hayes.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Hayes.java @@ -1,27 +1,3 @@ -/* - * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). - * - * The MIT License - * Copyright © 2014-2022 Ilkka Seppälä - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ package com.iluwatar.acyclicvisitor; import lombok.extern.slf4j.Slf4j; @@ -33,20 +9,19 @@ public class Hayes implements Modem { /** - * Accepts all visitors but honors only HayesVisitor. + * Accepts all visitors and interacts with SpecificModemVisitor. */ @Override public void accept(ModemVisitor modemVisitor) { - if (modemVisitor instanceof HayesVisitor) { - ((HayesVisitor) modemVisitor).visit(this); + if (modemVisitor instanceof SpecificModemVisitor) { + ((SpecificModemVisitor) modemVisitor).visit(this); } else { - LOGGER.info("Only HayesVisitor is allowed to visit Hayes modem"); + LOGGER.info("Unsupported visitor type for Hayes modem"); } - } /** - * Hayes' modem's toString method. + * Hayes modem's toString method. */ @Override public String toString() { diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/HayesVisitor.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/HayesVisitor.java index a33c87cfa645..e63d93b6ac2c 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/HayesVisitor.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/HayesVisitor.java @@ -1,32 +1,8 @@ -/* - * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). - * - * The MIT License - * Copyright © 2014-2022 Ilkka Seppälä - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ package com.iluwatar.acyclicvisitor; /** - * HayesVisitor interface. + * HayesVisitor interface for Hayes-specific logic. */ -public interface HayesVisitor extends ModemVisitor { - void visit(Hayes hayes); +public interface HayesVisitor extends SpecificModemVisitor { + void visit(Hayes hayes); // Supports Hayes-specific behavior } diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/SpecificModemVisitor.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/SpecificModemVisitor.java new file mode 100644 index 000000000000..5378685cb59f --- /dev/null +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/SpecificModemVisitor.java @@ -0,0 +1,8 @@ +package com.iluwatar.acyclicvisitor; + +/** + * General visitor interface for specific modems. + */ +public interface SpecificModemVisitor extends ModemVisitor { + void visit(Modem modem); +} diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/SpecificModemZoomVisitor.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/SpecificModemZoomVisitor.java new file mode 100644 index 000000000000..e69de29bb2d1 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 e9f02e1ad6fd..77b71cc2724b 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Zoom.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Zoom.java @@ -1,27 +1,3 @@ -/* - * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). - * - * The MIT License - * Copyright © 2014-2022 Ilkka Seppälä - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ package com.iluwatar.acyclicvisitor; import lombok.extern.slf4j.Slf4j; @@ -33,14 +9,14 @@ public class Zoom implements Modem { /** - * Accepts all visitors but honors only ZoomVisitor. + * Accepts all visitors but interacts generically with SpecificModemVisitor. */ @Override public void accept(ModemVisitor modemVisitor) { - if (modemVisitor instanceof ZoomVisitor) { - ((ZoomVisitor) modemVisitor).visit(this); + if (modemVisitor instanceof SpecificModemVisitor) { + ((SpecificModemVisitor) modemVisitor).visit(this); } else { - LOGGER.info("Only ZoomVisitor is allowed to visit Zoom modem"); + LOGGER.info("Unsupported visitor type for Zoom modem"); } } diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ZoomVisitor.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ZoomVisitor.java index 639af1c65777..fcbe5f4c2752 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ZoomVisitor.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ZoomVisitor.java @@ -1,32 +1,8 @@ -/* - * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). - * - * The MIT License - * Copyright © 2014-2022 Ilkka Seppälä - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ package com.iluwatar.acyclicvisitor; /** - * ZoomVisitor interface. + * ZoomVisitor interface, extending SpecificModemVisitor for Zoom-specific logic. */ -public interface ZoomVisitor extends ModemVisitor { - void visit(Zoom zoom); +public interface ZoomVisitor extends SpecificModemVisitor { + void visit(Zoom zoom); // Still supports Zoom-specific operations } diff --git a/extension-objects/src/main/java/units/Commander.java b/extension-objects/src/main/java/units/Commander.java new file mode 100644 index 000000000000..3681fdc22ecb --- /dev/null +++ b/extension-objects/src/main/java/units/Commander.java @@ -0,0 +1,24 @@ +package concreteextensions; + +import abstractextensions.CommanderExtension; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +/** + * Class defining Commander. + */ +@Slf4j +public class Commander implements CommanderExtension { + private final CommanderUnit unit; + + // Constructor now injects CommanderUnit + public Commander(CommanderUnit unit) { + this.unit = unit; + } + + @Override + public void commanderReady() { + LOGGER.info("[Commander] " + unit.getName() + " is ready!"); + } +} diff --git a/extension-objects/src/main/java/units/CommanderExtension b/extension-objects/src/main/java/units/CommanderExtension new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/extension-objects/src/main/java/units/CommanderExtension.java b/extension-objects/src/main/java/units/CommanderExtension.java new file mode 100644 index 000000000000..3c9d196fecb8 --- /dev/null +++ b/extension-objects/src/main/java/units/CommanderExtension.java @@ -0,0 +1,8 @@ +package abstractextensions; + +/** + * Interface for commander extensions. + */ +public interface CommanderExtension { + void commanderReady(); +} diff --git a/extension-objects/src/main/java/units/CommanderUnit.java b/extension-objects/src/main/java/units/CommanderUnit.java index cf62aaec84f1..908f903774f9 100644 --- a/extension-objects/src/main/java/units/CommanderUnit.java +++ b/extension-objects/src/main/java/units/CommanderUnit.java @@ -1,27 +1,3 @@ -/* - * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). - * - * The MIT License - * Copyright © 2014-2022 Ilkka Seppälä - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ package units; import abstractextensions.UnitExtension; @@ -33,17 +9,21 @@ */ public class CommanderUnit extends Unit { + private CommanderExtension unitExtension; + public CommanderUnit(String name) { super(name); } @Override public UnitExtension getUnitExtension(String extensionName) { - if (extensionName.equals("CommanderExtension")) { - return Optional.ofNullable(unitExtension).orElseGet(() -> new Commander(this)); + // Commander is now injected instead of being instantiated inside the method + if (unitExtension == null) { + unitExtension = new Commander(this); // Dependency injection here + } + return unitExtension; } - return super.getUnitExtension(extensionName); } }