From 8bf53fe91ddfc6617362c166ecd61e5069f77206 Mon Sep 17 00:00:00 2001 From: Argyro-Sioziou Date: Sun, 27 May 2018 15:11:06 +0300 Subject: [PATCH 01/32] Adding acyclic-visitor module in pom.xml --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index e1d025c13..00b0de4f1 100644 --- a/pom.xml +++ b/pom.xml @@ -161,6 +161,7 @@ dirty-flag trampoline serverless + acyclic-visitor From 9e15abbafc23318356ce10a771e810ec520fe90d Mon Sep 17 00:00:00 2001 From: Argyro-Sioziou Date: Sun, 27 May 2018 15:49:35 +0300 Subject: [PATCH 02/32] Adding pom.xml file --- acyclic-visitor/pom.xml | 76 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 acyclic-visitor/pom.xml diff --git a/acyclic-visitor/pom.xml b/acyclic-visitor/pom.xml new file mode 100644 index 000000000..4cb1ccaff --- /dev/null +++ b/acyclic-visitor/pom.xml @@ -0,0 +1,76 @@ + + + 4.0.0 + + + com.iluwatar + java-design-patterns + 1.20.0-SNAPSHOT + + + acyclic-visitor + + + UTF-8 + 1.8 + 1.8 + + + + + + org.assertj + assertj-core + 3.9.1 + test + + + + uk.org.lidalia + slf4j-test + 1.0.0 + test + + + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.jupiter + junit-jupiter-engine + test + + + org.mockito + mockito-all + 1.9.5 + test + + + From 18a75c06208289633e1f98edd5ee20174615450b Mon Sep 17 00:00:00 2001 From: Argyro-Sioziou Date: Sun, 27 May 2018 16:12:31 +0300 Subject: [PATCH 03/32] Adding ModemVisitor interface --- .../iluwatar/acyclicvisitor/ModemVisitor.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ModemVisitor.java diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ModemVisitor.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ModemVisitor.java new file mode 100644 index 000000000..eac2bd4a8 --- /dev/null +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ModemVisitor.java @@ -0,0 +1,31 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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 + */ + +public interface ModemVisitor { + // Visitor is a degenerate base class for all visitors. +} From 3f92b8f84e2481157d748b06f2dce8eea181fbfa Mon Sep 17 00:00:00 2001 From: Argyro-Sioziou Date: Sun, 27 May 2018 16:37:13 +0300 Subject: [PATCH 04/32] Adding ModemTest --- .../iluwatar/acyclicvisitor/ModemTest.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ModemTest.java diff --git a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ModemTest.java b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ModemTest.java new file mode 100644 index 000000000..0f7d3d629 --- /dev/null +++ b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ModemTest.java @@ -0,0 +1,30 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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; + +/** + * Modem test class + */ +public abstract class ModemTest { + +} From 75734a6b7d3e77492d38b71bd0ca9a46dbb2ce7a Mon Sep 17 00:00:00 2001 From: giorgosmav21 Date: Sun, 27 May 2018 16:45:43 +0300 Subject: [PATCH 05/32] Adding Modem abstract class --- .../com/iluwatar/acyclicvisitor/Modem.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Modem.java diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Modem.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Modem.java new file mode 100644 index 000000000..b7e4d3f6b --- /dev/null +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Modem.java @@ -0,0 +1,31 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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; + +/** + * Modem abstract class + */ + +public abstract class Modem { + public abstract void accept(ModemVisitor modemVisitor); +} From 323395daaff39381cb52a465d5cdeb5dfadd4f13 Mon Sep 17 00:00:00 2001 From: giorgosmav21 Date: Sun, 27 May 2018 16:49:05 +0300 Subject: [PATCH 06/32] Adding ModemVisitorTest --- .../acyclicvisitor/ModemVisitorTest.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ModemVisitorTest.java diff --git a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ModemVisitorTest.java b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ModemVisitorTest.java new file mode 100644 index 000000000..681925f05 --- /dev/null +++ b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ModemVisitorTest.java @@ -0,0 +1,30 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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; + +/** + * ModemVisitor test class + */ +public interface ModemVisitorTest { + +} From bbdf1cd738af19a8fec33def061bfed226822b5e Mon Sep 17 00:00:00 2001 From: giorgosmav21 Date: Sun, 27 May 2018 16:51:26 +0300 Subject: [PATCH 07/32] Adding Hayes and Zoom classes --- .../com/iluwatar/acyclicvisitor/Hayes.java | 62 +++++++++++++++++++ .../com/iluwatar/acyclicvisitor/Zoom.java | 61 ++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Hayes.java create mode 100644 acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Zoom.java diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Hayes.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Hayes.java new file mode 100644 index 000000000..d829e7f02 --- /dev/null +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Hayes.java @@ -0,0 +1,62 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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 org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Hayes class implements its + * accept method + */ + +public class Hayes extends Modem { + + private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureForDosVisitor.class); + + public Hayes() { + + } + + /** + * Accept visitor + */ + @Override + public void accept(ModemVisitor modemVisitor) { + try { + ((HayesVisitor) modemVisitor).visit(this); + } catch (ClassCastException e) { + LOGGER.error("Unable to cast to HayesVisitor"); + } + + } + + /** + * Hayes' modem's toString + * method + */ + @Override + public String toString() { + return "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 new file mode 100644 index 000000000..099bdc63d --- /dev/null +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Zoom.java @@ -0,0 +1,61 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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 org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Zoom class implements its + * accept method + */ + +public class Zoom extends Modem { + + private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureForDosVisitor.class); + + public Zoom() { + + } + + /** + * Accept visitor + */ + @Override + public void accept(ModemVisitor modemVisitor) { + try { + ((ZoomVisitor) modemVisitor).visit(this); + } catch (ClassCastException e) { + LOGGER.error("Unable to cast to ZoomVisitor"); + } + } + + /** + * Zoom modem's toString + * method + */ + @Override + public String toString() { + return "Zoom modem"; + } +} From 97718846d1bbb9a66f94b75bc8d5722ce43ffca3 Mon Sep 17 00:00:00 2001 From: Argyro-Sioziou Date: Sun, 27 May 2018 16:55:08 +0300 Subject: [PATCH 08/32] Adding HayesVisitor and ZoomVisitor --- .../iluwatar/acyclicvisitor/HayesVisitor.java | 30 ++++++++++++++++++ .../iluwatar/acyclicvisitor/ZoomVisitor.java | 31 +++++++++++++++++++ .../acyclicvisitor/HayesVisitorTest.java | 30 ++++++++++++++++++ .../acyclicvisitor/ZoomVisitorTest.java | 30 ++++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/HayesVisitor.java create mode 100644 acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ZoomVisitor.java create mode 100644 acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/HayesVisitorTest.java create mode 100644 acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ZoomVisitorTest.java diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/HayesVisitor.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/HayesVisitor.java new file mode 100644 index 000000000..90e8a2a1b --- /dev/null +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/HayesVisitor.java @@ -0,0 +1,30 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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 + */ +public interface HayesVisitor { + void visit(Hayes hayes); +} diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ZoomVisitor.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ZoomVisitor.java new file mode 100644 index 000000000..5f6fa0cd3 --- /dev/null +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ZoomVisitor.java @@ -0,0 +1,31 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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 + */ + +public interface ZoomVisitor { + void visit(Zoom zoom); +} diff --git a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/HayesVisitorTest.java b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/HayesVisitorTest.java new file mode 100644 index 000000000..96244f879 --- /dev/null +++ b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/HayesVisitorTest.java @@ -0,0 +1,30 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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 test class + */ +public interface HayesVisitorTest { + +} \ No newline at end of file diff --git a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ZoomVisitorTest.java b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ZoomVisitorTest.java new file mode 100644 index 000000000..88aab9636 --- /dev/null +++ b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ZoomVisitorTest.java @@ -0,0 +1,30 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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 test class + */ +public interface ZoomVisitorTest { + +} From 5e1da6361fd91d923dac256b209b7ba14ce9c662 Mon Sep 17 00:00:00 2001 From: Argyro-Sioziou Date: Sun, 27 May 2018 16:58:38 +0300 Subject: [PATCH 09/32] Adding ConfigureForDosVisitor and ConfigureForUnixVisitor --- .../ConfigureForDosVisitor.java | 46 ++++++++++++ .../ConfigureForUnixVisitor.java | 40 ++++++++++ .../ConfigureForDosVisitorTest.java | 75 +++++++++++++++++++ .../ConfigureForUnixVisitorTest.java | 75 +++++++++++++++++++ 4 files changed, 236 insertions(+) create mode 100644 acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitor.java create mode 100644 acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitor.java create mode 100644 acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitorTest.java create mode 100644 acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitorTest.java diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitor.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitor.java new file mode 100644 index 000000000..aee1dfa38 --- /dev/null +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitor.java @@ -0,0 +1,46 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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 org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * CongigureForDosVisitor class + * implements both zoom's and + * hayes' visit method for Dos + * manufacturer + */ + +public class ConfigureForDosVisitor implements ModemVisitor, HayesVisitor, ZoomVisitor { + + private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureForDosVisitor.class); + + public void visit(Hayes hayes) { + LOGGER.info(hayes + " used with Dos configurator."); + } + + public void visit(Zoom zoom) { + LOGGER.info(zoom + " used with Dos configurator."); + } +} diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitor.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitor.java new file mode 100644 index 000000000..c2402d2bc --- /dev/null +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitor.java @@ -0,0 +1,40 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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 org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * CongigureForDosVisitor class + * implements both zoom's visit + * method for Unix manufacturer + */ +public class ConfigureForUnixVisitor implements ModemVisitor, ZoomVisitor { + + private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureForUnixVisitor.class); + + public void visit(Zoom zoom) { + LOGGER.info(zoom + " used with Unix configurator."); + } +} \ No newline at end of file diff --git a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitorTest.java b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitorTest.java new file mode 100644 index 000000000..78d442caa --- /dev/null +++ b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitorTest.java @@ -0,0 +1,75 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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 static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.groups.Tuple.tuple; +import static org.mockito.Mockito.mock; +import static uk.org.lidalia.slf4jext.Level.INFO; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import com.iluwatar.acyclicvisitor.ConfigureForDosVisitor; +import com.iluwatar.acyclicvisitor.Hayes; +import com.iluwatar.acyclicvisitor.HayesVisitor; +import com.iluwatar.acyclicvisitor.Zoom; +import com.iluwatar.acyclicvisitor.ZoomVisitor; + +import uk.org.lidalia.slf4jtest.TestLogger; +import uk.org.lidalia.slf4jtest.TestLoggerFactory; + +/** + * ConfigureForDosVisitor test class + */ +public class ConfigureForDosVisitorTest { + + TestLogger logger = TestLoggerFactory.getTestLogger(ConfigureForDosVisitor.class); + + @Test + public void testVisitForZoom() { + ConfigureForDosVisitor conDos = new ConfigureForDosVisitor(); + Zoom zoom = mock(Zoom.class); + + ((ZoomVisitor)conDos).visit(zoom); + + assertThat(logger.getLoggingEvents()).extracting("level", "message").contains( + tuple(INFO, zoom + " used with Dos configurator.")); + } + + @Test + public void testVisitForHayes() { + ConfigureForDosVisitor conDos = new ConfigureForDosVisitor(); + Hayes hayes = mock(Hayes.class); + + ((HayesVisitor)conDos).visit(hayes); + + assertThat(logger.getLoggingEvents()).extracting("level", "message").contains( + tuple(INFO, hayes + " used with Dos configurator.")); + } + + @AfterEach + public void clearLoggers() { + TestLoggerFactory.clear(); + } +} diff --git a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitorTest.java b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitorTest.java new file mode 100644 index 000000000..4346ffd30 --- /dev/null +++ b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitorTest.java @@ -0,0 +1,75 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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 static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.groups.Tuple.tuple; +import static uk.org.lidalia.slf4jext.Level.INFO; +import static org.mockito.Mockito.mock; + +import uk.org.lidalia.slf4jtest.TestLogger; +import uk.org.lidalia.slf4jtest.TestLoggerFactory; +import org.junit.jupiter.api.Test; + +import com.iluwatar.acyclicvisitor.ConfigureForUnixVisitor; +import com.iluwatar.acyclicvisitor.Hayes; +import com.iluwatar.acyclicvisitor.HayesVisitor; +import com.iluwatar.acyclicvisitor.Zoom; +import com.iluwatar.acyclicvisitor.ZoomVisitor; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; + +/** + * ConfigureForUnixVisitor test class + */ +public class ConfigureForUnixVisitorTest { + + TestLogger logger = TestLoggerFactory.getTestLogger(ConfigureForUnixVisitor.class); + + @AfterEach + public void clearLoggers() { + TestLoggerFactory.clear(); + } + + @Test + public void testVisitForZoom() { + ConfigureForUnixVisitor conUnix = new ConfigureForUnixVisitor(); + Zoom zoom = mock(Zoom.class); + + ((ZoomVisitor)conUnix).visit(zoom); + + assertThat(logger.getLoggingEvents()).extracting("level", "message").contains( + tuple(INFO, zoom + " used with Unix configurator.")); + } + + @Test + public void testVisitForHayes() { + ConfigureForUnixVisitor conUnix = new ConfigureForUnixVisitor(); + Hayes hayes = mock(Hayes.class); + + Assertions.assertThrows(ClassCastException.class, () -> { + ((HayesVisitor)conUnix).visit(hayes); + }); + } +} From 36a14778dc9bf05649d9869990f2a1e9ef020651 Mon Sep 17 00:00:00 2001 From: giorgosmav21 Date: Sun, 27 May 2018 17:02:56 +0300 Subject: [PATCH 10/32] Adding App class --- .../java/com/iluwatar/acyclicvisitor/App.java | 66 +++++++++++++++++++ .../com/iluwatar/acyclicvisitor/AppTest.java | 39 +++++++++++ 2 files changed, 105 insertions(+) create mode 100644 acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/App.java create mode 100644 acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/AppTest.java diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/App.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/App.java new file mode 100644 index 000000000..be57406dd --- /dev/null +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/App.java @@ -0,0 +1,66 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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; + +/** + * 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 GoF Visitor pattern, by making the + * Visitor base class degenerate + *

+ * In this example the visitor base + * class is {@link ModemVisitor}. + * The base class of the visited + * hierarchy is {@link Modem} and + * has two children {@link Hayes} + * and {@link Zoom} each one having + * its own visitor interface {@link Hayes} + * and {@link ZoomVisitor} respectively. + * {@link ConfigureForUnixVisitor} and + * {@link ConfigureForDosVisitor} implement + * each derivative's visit method + * only if it is required + */ + +public class App { + + /** + * Program's entry point + */ + + public static void main(String[] args) { + ConfigureForUnixVisitor conUnix = new ConfigureForUnixVisitor(); + ConfigureForDosVisitor conDos = new ConfigureForDosVisitor(); + + Zoom zoom = new Zoom(); + Hayes hayes = new Hayes(); + + hayes.accept(conDos); // Hayes modem with Unix configurator + zoom.accept(conDos); // Zoom modem with Dos configurator + hayes.accept(conUnix); // Hayes modem with Unix configurator + zoom.accept(conUnix); // Zoom modem with Unix configurator + } +} diff --git a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/AppTest.java b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/AppTest.java new file mode 100644 index 000000000..056401ca2 --- /dev/null +++ b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/AppTest.java @@ -0,0 +1,39 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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 org.junit.jupiter.api.Test; + +import com.iluwatar.acyclicvisitor.App; + +/** + * Tests that the Acyclic Visitor example runs without errors. + */ +public class AppTest { + + @Test + public void test() { + String[] args = {}; + App.main(args); + } +} \ No newline at end of file From 27f58e0925297271c6c023c4cc1009b6e8c03df5 Mon Sep 17 00:00:00 2001 From: giorgosmav21 Date: Sun, 27 May 2018 17:04:22 +0300 Subject: [PATCH 11/32] Adding HayesTest and ZoomTest --- .../iluwatar/acyclicvisitor/HayesTest.java | 60 +++++++++++++++++++ .../com/iluwatar/acyclicvisitor/ZoomTest.java | 59 ++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/HayesTest.java create mode 100644 acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ZoomTest.java diff --git a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/HayesTest.java b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/HayesTest.java new file mode 100644 index 000000000..ba5139bf6 --- /dev/null +++ b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/HayesTest.java @@ -0,0 +1,60 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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 static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; + +import org.junit.jupiter.api.Test; + +import com.iluwatar.acyclicvisitor.ConfigureForDosVisitor; +import com.iluwatar.acyclicvisitor.ConfigureForUnixVisitor; +import com.iluwatar.acyclicvisitor.Hayes; +import com.iluwatar.acyclicvisitor.HayesVisitor; + +/** + * Hayes test class + */ +public class HayesTest { + + @Test + public void testAcceptForDos() { + Hayes hayes = new Hayes(); + ConfigureForDosVisitor mockVisitor = mock(ConfigureForDosVisitor.class); + + hayes.accept(mockVisitor); + verify((HayesVisitor)mockVisitor).visit(eq(hayes)); + } + + @Test + public void testAcceptForUnix() { + Hayes hayes = new Hayes(); + ConfigureForUnixVisitor mockVisitor = mock(ConfigureForUnixVisitor.class); + + hayes.accept(mockVisitor); + + verifyZeroInteractions(mockVisitor); + } +} diff --git a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ZoomTest.java b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ZoomTest.java new file mode 100644 index 000000000..22391fb2f --- /dev/null +++ b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ZoomTest.java @@ -0,0 +1,59 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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 static org.mockito.Matchers.eq; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.mock; + +import org.junit.jupiter.api.Test; + +import com.iluwatar.acyclicvisitor.ConfigureForDosVisitor; +import com.iluwatar.acyclicvisitor.ConfigureForUnixVisitor; +import com.iluwatar.acyclicvisitor.Zoom; +import com.iluwatar.acyclicvisitor.ZoomVisitor; + +/** + * Zoom test class + */ +public class ZoomTest { + + @Test + public void testAcceptForDos() { + Zoom zoom = new Zoom(); + ConfigureForDosVisitor mockVisitor = mock(ConfigureForDosVisitor.class); + + zoom.accept(mockVisitor); + verify((ZoomVisitor)mockVisitor).visit(eq(zoom)); + } + + @Test + public void testAcceptForUnix() { + Zoom zoom = new Zoom(); + ConfigureForUnixVisitor mockVisitor = mock(ConfigureForUnixVisitor.class); + + zoom.accept(mockVisitor); + verify((ZoomVisitor)mockVisitor).visit(eq(zoom)); + } +} From 17164c282a7b707f75ca478629ec8bb998208230 Mon Sep 17 00:00:00 2001 From: Argyro-Sioziou Date: Sun, 27 May 2018 17:07:34 +0300 Subject: [PATCH 12/32] Adding class diagram --- acyclic-visitor/etc/Acyclic Visitor.ucls | 115 +++++++++++++++++++++++ acyclic-visitor/etc/acyclic-visitor.png | Bin 0 -> 26645 bytes 2 files changed, 115 insertions(+) create mode 100644 acyclic-visitor/etc/Acyclic Visitor.ucls create mode 100644 acyclic-visitor/etc/acyclic-visitor.png diff --git a/acyclic-visitor/etc/Acyclic Visitor.ucls b/acyclic-visitor/etc/Acyclic Visitor.ucls new file mode 100644 index 000000000..03b6c77dd --- /dev/null +++ b/acyclic-visitor/etc/Acyclic Visitor.ucls @@ -0,0 +1,115 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/acyclic-visitor/etc/acyclic-visitor.png b/acyclic-visitor/etc/acyclic-visitor.png new file mode 100644 index 0000000000000000000000000000000000000000..636532c4d5d6443c94173fec7b0782eb8b661fe9 GIT binary patch literal 26645 zcmaI71yqz>7dAW!2qGaOQVu9BjWk0kB_JTu(k0z8bP0&mAQA!+(kP6-tYT=*J26GxzE{W$F;A$?@jP4MHzft3S1BfgfII-@-+yAF$DtM*uuF9 z{Kg_Ck`)9pn~{}#rs|rwm1GyjBjdFy6HKEO!nzeI>7GW{J@>@t5bJTieOFnRtZS4` zufgJ8_d+5z2cH!@0PA51f$@a3J(U>D(B`>j5OE!JqWA--=f+W(4UQG&;o6JeQ@F$T z?prwFOfc%Odyvjrv^GpGN%3fKUli5m*JcC%^7A+MhBWdglaseM;`6@SU99U@oC2bO zKn5RU*D@4%JGoky@vk}Pfp;CHIn&>6ZGkApJ~8ak^u7W68T zoMVOeV41vP1n`sMvv#|CNP*XZU~CY`9+%lW$^ah8j1&NYd{thdzG6aPs)q0zAW-7_ zka;@3DU@FJ*oO5NpI+ZXTmBicMyT9`1F%c>w?+Qj(E9nE?aE~gTbG+MFkrj5hcp{+ z@Kf!;e!!!JQxgsgUXTWz>7EM2IFzGuigJ7@ykS}0szxILOg-T>o0)0DU-SUDNq4#^ z2|n{9OH7$xW_+^X?ay^G(f#rruJoD~p_w5qy)d8aww| zkuKBT*JRC^@*;v|{Bvc}R}K(QkdP`*PH_&=L%)(*zAZ$~S2PygG4c6tea>`66EyM1blbhnf2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Lfa^9g;A%zW++!VN!N+AF9%l*@zQz3fhX8 zY7)7%LdCkyL!~ zr3~Ai*O%P&E@!oga-weZZJ7VN1F4q9FhE*2ZtPa7K-mjJs5WUZd!}uszKqIwy~J)i zz;{2-H!{36!`9`8Ohz~(pY1d5oSDCM`(M;D+qO9*K|T)q!gY6H^Q_wt1OuK{$5u-2 zOTDb<`xFaPs)OFL2_0j2zeh=f7JzXfvcn-CQ-1iZ@@HB!W$xcr^1m%MIY8m)cIfl? z$!p)NKATclDAng@&ao;ht~2T;S16ZUQDDs+ePA(WAt&JYy}F2TocwI%9g+ETR7T4Y za$+_vUMg6caPRY<_`E*GM0K!Ziods?J7TTutN-(Q^#X&;Vb2auQNd{)pIF^pkZH|e z+Ms&i^8->beq!cyF$qRuH|nE_|Fl_p&bJFO9z*b@ndEp9noaTvDiKv}k{n^Wl}`48NN z?#$NOnU|e)*SZ7Al9O!HDOb#!Sa&1uh#bi9YCw4wgKHzs9=pA0SQ4V0YT8T}1hj00 zMPBz}`C;8ZgwWUDJoAeUZ_rWc?X#6KKEmZj0Jh|}Yt?2HbqCr32+-_k7hC8g*oJ=J zt$B1^h~0#fN{`$3sS3ZbH}(ObV7=Ked~p86vvK~^Unla`idgpv=>882l1v`V<|m|? z54o!T-Za5&DmBf&gN6TuHh6f37O$Rr1N--I2rh Date: Sun, 27 May 2018 17:10:20 +0300 Subject: [PATCH 13/32] Adding README.md file --- acyclic-visitor/README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 acyclic-visitor/README.md diff --git a/acyclic-visitor/README.md b/acyclic-visitor/README.md new file mode 100644 index 000000000..33c894ce7 --- /dev/null +++ b/acyclic-visitor/README.md @@ -0,0 +1,29 @@ +--- +layout: pattern +title: Acyclic Visitor +folder: acyclic-visitor +permalink: /patterns/acyclic-visitor/ +categories: Behavioral +tags: + - Java + - Difficulty-Intermediate +--- + +![alt text](./etc/acyclic-visitor.png "Acyclic Visitor") + +## Intent +Allow new functions to be added to existing class hierarchies without affecting those hierarchies, and without creating the troublesome dependency cycles that are inherent to the GOF VISITOR Pattern. + +## Applicability +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 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. + +## Related patterns +* [Visitor Pattern](../visitor/README.md) + +## Credits +* [Acyclic Visitor](http://condor.depaul.edu/dmumaugh/OOT/Design-Principles/acv.pdf) \ No newline at end of file From afe85e22e78778b0b4d9dad46aec70dca62bdfba Mon Sep 17 00:00:00 2001 From: Argyro-Sioziou Date: Tue, 29 May 2018 01:37:53 +0300 Subject: [PATCH 14/32] Enhancing code format --- .../java/com/iluwatar/acyclicvisitor/App.java | 29 ++++++------------- .../ConfigureForDosVisitor.java | 7 ++--- .../ConfigureForUnixVisitor.java | 5 ++-- .../com/iluwatar/acyclicvisitor/Hayes.java | 4 +-- .../com/iluwatar/acyclicvisitor/Modem.java | 1 - .../iluwatar/acyclicvisitor/ModemVisitor.java | 1 - .../com/iluwatar/acyclicvisitor/Zoom.java | 4 +-- .../iluwatar/acyclicvisitor/ZoomVisitor.java | 1 - 8 files changed, 15 insertions(+), 37 deletions(-) diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/App.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/App.java index be57406dd..97fdddf14 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/App.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/App.java @@ -23,28 +23,17 @@ package com.iluwatar.acyclicvisitor; /** - * 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 GoF Visitor pattern, by making the - * Visitor base class degenerate + * 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 GoF Visitor pattern, by making the Visitor base class + * degenerate *

- * In this example the visitor base - * class is {@link ModemVisitor}. - * The base class of the visited - * hierarchy is {@link Modem} and - * has two children {@link Hayes} - * and {@link Zoom} each one having - * its own visitor interface {@link Hayes} - * and {@link ZoomVisitor} respectively. - * {@link ConfigureForUnixVisitor} and - * {@link ConfigureForDosVisitor} implement - * each derivative's visit method - * only if it is required + * In this example the visitor base class is {@link ModemVisitor}. The base class of the + * visited hierarchy is {@link Modem} and has two children {@link Hayes} and {@link Zoom} + * each one having its own visitor interface {@link Hayes} and {@link ZoomVisitor} + * respectively. {@link ConfigureForUnixVisitor} and {@link ConfigureForDosVisitor} + * implement each derivative's visit method only if it is required */ - public class App { /** diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitor.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitor.java index aee1dfa38..b71893474 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitor.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitor.java @@ -26,12 +26,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * CongigureForDosVisitor class - * implements both zoom's and - * hayes' visit method for Dos - * manufacturer + * CongigureForDosVisitor class implements both zoom's and hayes' visit method + * for Dos manufacturer */ - public class ConfigureForDosVisitor implements ModemVisitor, HayesVisitor, ZoomVisitor { private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureForDosVisitor.class); 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 c2402d2bc..9e35ea0a6 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitor.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitor.java @@ -26,9 +26,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * CongigureForDosVisitor class - * implements both zoom's visit - * method for Unix manufacturer + * CongigureForDosVisitor class implements both zoom's visit method for Unix + * manufacturer */ public class ConfigureForUnixVisitor implements ModemVisitor, ZoomVisitor { 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 d829e7f02..30408a758 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Hayes.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Hayes.java @@ -26,10 +26,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * Hayes class implements its - * accept method + * Hayes class implements its accept method */ - public class Hayes extends Modem { private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureForDosVisitor.class); diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Modem.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Modem.java index b7e4d3f6b..57207a466 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Modem.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Modem.java @@ -25,7 +25,6 @@ package com.iluwatar.acyclicvisitor; /** * Modem abstract class */ - public abstract class Modem { public abstract void accept(ModemVisitor modemVisitor); } diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ModemVisitor.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ModemVisitor.java index eac2bd4a8..f5668756c 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ModemVisitor.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ModemVisitor.java @@ -25,7 +25,6 @@ package com.iluwatar.acyclicvisitor; /** * ZoomVisitor interface */ - public interface ModemVisitor { // Visitor is a degenerate base class for all visitors. } 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 099bdc63d..176a22ea4 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Zoom.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Zoom.java @@ -26,10 +26,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * Zoom class implements its - * accept method + * Zoom class implements its accept method */ - public class Zoom extends Modem { private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureForDosVisitor.class); 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 5f6fa0cd3..85646921c 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ZoomVisitor.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ZoomVisitor.java @@ -25,7 +25,6 @@ package com.iluwatar.acyclicvisitor; /** * ZoomVisitor interface */ - public interface ZoomVisitor { void visit(Zoom zoom); } From 73925cef2ea6d99a8f75d4ad845925a03deba20e Mon Sep 17 00:00:00 2001 From: Ovidijus Okinskas Date: Sun, 3 Jun 2018 22:20:40 +0100 Subject: [PATCH 15/32] Ambassador adds logging, imitates trying to connect to remote service and provides new client-side functionality. Need to clean up code, add tests and add descriptive comments. --- ambassador/pom.xml | 15 ++++ .../java/com/iluwatar/ambassador/App.java | 19 +++++ .../java/com/iluwatar/ambassador/Client.java | 42 +++++++++ .../iluwatar/ambassador/RemoteService.java | 54 ++++++++++++ .../ambassador/RemoteServiceInterface.java | 28 ++++++ .../ambassador/ServiceAmbassador.java | 85 +++++++++++++++++++ pom.xml | 1 + 7 files changed, 244 insertions(+) create mode 100644 ambassador/pom.xml create mode 100644 ambassador/src/main/java/com/iluwatar/ambassador/App.java create mode 100644 ambassador/src/main/java/com/iluwatar/ambassador/Client.java create mode 100644 ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java create mode 100644 ambassador/src/main/java/com/iluwatar/ambassador/RemoteServiceInterface.java create mode 100644 ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java diff --git a/ambassador/pom.xml b/ambassador/pom.xml new file mode 100644 index 000000000..b230a2736 --- /dev/null +++ b/ambassador/pom.xml @@ -0,0 +1,15 @@ + + + + java-design-patterns + com.iluwatar + 1.20.0-SNAPSHOT + + 4.0.0 + + ambassador + + + \ No newline at end of file diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/App.java b/ambassador/src/main/java/com/iluwatar/ambassador/App.java new file mode 100644 index 000000000..0293c1004 --- /dev/null +++ b/ambassador/src/main/java/com/iluwatar/ambassador/App.java @@ -0,0 +1,19 @@ +package com.iluwatar.ambassador; + +public class App { + + /** + * Entry point + */ + public static void main(String[] args) { + + Client host1 = new Client(); + Client host2 = new Client(); + + host1.useService(12); + host2.useService(73); + + host1.useNewService(12); + host2.useNewService(73); + } +} diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/Client.java b/ambassador/src/main/java/com/iluwatar/ambassador/Client.java new file mode 100644 index 000000000..0c7bb0394 --- /dev/null +++ b/ambassador/src/main/java/com/iluwatar/ambassador/Client.java @@ -0,0 +1,42 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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.ambassador; + +public class Client { + + private ServiceAmbassador serviceAmbassador; + + Client() { + serviceAmbassador = new ServiceAmbassador(); + } + + void useService(int value) { + long result = serviceAmbassador.doRemoteFunction(value); + System.out.println(result); + } + + void useNewService(int value) { + long result = serviceAmbassador.doAddedFunction(value); + System.out.println(result); + } +} diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java b/ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java new file mode 100644 index 000000000..0c608c151 --- /dev/null +++ b/ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java @@ -0,0 +1,54 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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.ambassador; + +import static java.lang.Thread.sleep; + +public class RemoteService implements RemoteServiceInterface { + + private static RemoteService service = null; + + static synchronized RemoteService getRemoteService() { + if (service == null) { + service = new RemoteService(); + } + return service; + } + + private RemoteService() { + + } + + @Override + public long doRemoteFunction(int value) { + + long waitTime = (long) Math.floor(Math.random() * 1000); + + try { + sleep(waitTime); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return waitTime >= 200 ? value * 10 : -1; + } +} diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/RemoteServiceInterface.java b/ambassador/src/main/java/com/iluwatar/ambassador/RemoteServiceInterface.java new file mode 100644 index 000000000..50a097316 --- /dev/null +++ b/ambassador/src/main/java/com/iluwatar/ambassador/RemoteServiceInterface.java @@ -0,0 +1,28 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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.ambassador; + +interface RemoteServiceInterface { + + long doRemoteFunction(int value) throws Exception; +} diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java b/ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java new file mode 100644 index 000000000..1c9a2325f --- /dev/null +++ b/ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java @@ -0,0 +1,85 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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.ambassador; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static java.lang.Thread.sleep; + +public class ServiceAmbassador implements RemoteServiceInterface { + + private static final Logger LOGGER = LoggerFactory.getLogger(ServiceAmbassador.class); + private static final int RETRIES = 3; + private static final int DELAY_MS = 3000; + + ServiceAmbassador() { + + } + + @Override + public long doRemoteFunction(int value) { + + return safeCall(value); + } + + long doAddedFunction(int value) { + return safeCall(value) * 5; + } + + private long checkLatency(int value) { + RemoteService service = RemoteService.getRemoteService(); + long startTime = System.currentTimeMillis(); + long result = service.doRemoteFunction(value); + long timeTaken = System.currentTimeMillis() - startTime; + + LOGGER.info("Time taken (ms): " + timeTaken); + return result; + } + + private long safeCall(int value) { + + int retries = 0; + long result = -1; + + for (int i = 0; i < RETRIES; i++) { + + if (retries >= RETRIES) { + return -1; + } + + if ((result = checkLatency(value)) == -1) { + LOGGER.info("Failed to reach remote: (" + (i + 1) + ")"); + retries++; + try { + sleep(DELAY_MS); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } else { + break; + } + } + return result; + } +} diff --git a/pom.xml b/pom.xml index e1d025c13..b44c6b843 100644 --- a/pom.xml +++ b/pom.xml @@ -161,6 +161,7 @@ dirty-flag trampoline serverless + ambassador From 6b10f4bdd1ca1ac97b03cb0ea5f2fc9eea0be00e Mon Sep 17 00:00:00 2001 From: Ovidijus Okinskas Date: Mon, 4 Jun 2018 20:36:10 +0100 Subject: [PATCH 16/32] Adding appropriate comments on classes and full description in App.java. Removing added function in ServiceAmbassador as it's not appropriate for the example. --- .../java/com/iluwatar/ambassador/App.java | 43 +++++++++++++++++-- .../java/com/iluwatar/ambassador/Client.java | 8 ++-- .../iluwatar/ambassador/RemoteService.java | 9 ++++ .../ambassador/RemoteServiceInterface.java | 3 ++ .../ambassador/ServiceAmbassador.java | 11 +++-- 5 files changed, 61 insertions(+), 13 deletions(-) diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/App.java b/ambassador/src/main/java/com/iluwatar/ambassador/App.java index 0293c1004..32c5f0f1c 100644 --- a/ambassador/src/main/java/com/iluwatar/ambassador/App.java +++ b/ambassador/src/main/java/com/iluwatar/ambassador/App.java @@ -1,5 +1,44 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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.ambassador; +/** + * + * The ambassador pattern creates a helper service that sends network requests on behalf of a + * client. It is often used in cloud-based applications to offload features of a remote service. + * + * An ambassador service can be thought of as an out-of-process proxy that is co-located with + * the client. Similar to the proxy design pattern, the ambassador service provides an interface + * for another remote service. In addition to the interface, the ambassador provides extra + * functionality and features, specifically offloaded common connectivity tasks. This usually + * consists of monitoring, logging, routing, security etc. This is extremely useful in + * legacy applications where the codebase is difficult to modify and allows for improvements + * in the application's networking capabilities. + * + * In this example, we will the ({@link ServiceAmbassador}) class represents the ambassador while the + * ({@link RemoteService}) class represents a remote application. + * + */ public class App { /** @@ -9,11 +48,7 @@ public class App { Client host1 = new Client(); Client host2 = new Client(); - host1.useService(12); host2.useService(73); - - host1.useNewService(12); - host2.useNewService(73); } } diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/Client.java b/ambassador/src/main/java/com/iluwatar/ambassador/Client.java index 0c7bb0394..30bd834e6 100644 --- a/ambassador/src/main/java/com/iluwatar/ambassador/Client.java +++ b/ambassador/src/main/java/com/iluwatar/ambassador/Client.java @@ -22,6 +22,9 @@ */ package com.iluwatar.ambassador; +/** + * A simple Client + */ public class Client { private ServiceAmbassador serviceAmbassador; @@ -34,9 +37,4 @@ public class Client { long result = serviceAmbassador.doRemoteFunction(value); System.out.println(result); } - - void useNewService(int value) { - long result = serviceAmbassador.doAddedFunction(value); - System.out.println(result); - } } diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java b/ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java index 0c608c151..668e776ce 100644 --- a/ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java +++ b/ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java @@ -24,6 +24,9 @@ package com.iluwatar.ambassador; import static java.lang.Thread.sleep; +/** + * A remote legacy application represented by a Singleton implementation. + */ public class RemoteService implements RemoteServiceInterface { private static RemoteService service = null; @@ -39,6 +42,12 @@ public class RemoteService implements RemoteServiceInterface { } + /** + * Remote function takes a value and multiplies it by 10 taking a random amount of time. + * Will sometimes return -1. This immitates connectivity issues a client might have to account for. + * @param value integer value to be multiplied. + * @return if waitTime is more than 200ms, it returns value * 10, otherwise -1. + */ @Override public long doRemoteFunction(int value) { diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/RemoteServiceInterface.java b/ambassador/src/main/java/com/iluwatar/ambassador/RemoteServiceInterface.java index 50a097316..9f1112da9 100644 --- a/ambassador/src/main/java/com/iluwatar/ambassador/RemoteServiceInterface.java +++ b/ambassador/src/main/java/com/iluwatar/ambassador/RemoteServiceInterface.java @@ -22,6 +22,9 @@ */ package com.iluwatar.ambassador; +/** + * Interface shared by ({@link RemoteService}) and ({@link ServiceAmbassador}). + */ interface RemoteServiceInterface { long doRemoteFunction(int value) throws Exception; diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java b/ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java index 1c9a2325f..77c7197ff 100644 --- a/ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java +++ b/ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java @@ -27,6 +27,13 @@ import org.slf4j.LoggerFactory; import static java.lang.Thread.sleep; +/** + * + * ServiceAmbassador provides an interface for a ({@link Client}) to access ({@link RemoteService}). + * The interface adds logging, latency testing and usage of the service in a safe way that will not + * add stress to the remote service when connectivity issues occur. + * + */ public class ServiceAmbassador implements RemoteServiceInterface { private static final Logger LOGGER = LoggerFactory.getLogger(ServiceAmbassador.class); @@ -43,10 +50,6 @@ public class ServiceAmbassador implements RemoteServiceInterface { return safeCall(value); } - long doAddedFunction(int value) { - return safeCall(value) * 5; - } - private long checkLatency(int value) { RemoteService service = RemoteService.getRemoteService(); long startTime = System.currentTimeMillis(); From 74190e36bb049182ac92889551b5eeedb0dde377 Mon Sep 17 00:00:00 2001 From: Ovidijus Okinskas Date: Mon, 4 Jun 2018 21:20:04 +0100 Subject: [PATCH 17/32] Adding tests for each class. --- ambassador/pom.xml | 16 +++++++++ .../java/com/iluwatar/ambassador/Client.java | 3 +- .../java/com/iluwatar/ambassador/AppTest.java | 36 +++++++++++++++++++ .../com/iluwatar/ambassador/ClientTest.java | 15 ++++++++ .../ambassador/RemoteServiceTest.java | 16 +++++++++ .../ambassador/ServiceAmbassadorTest.java | 13 +++++++ 6 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 ambassador/src/test/java/com/iluwatar/ambassador/AppTest.java create mode 100644 ambassador/src/test/java/com/iluwatar/ambassador/ClientTest.java create mode 100644 ambassador/src/test/java/com/iluwatar/ambassador/RemoteServiceTest.java create mode 100644 ambassador/src/test/java/com/iluwatar/ambassador/ServiceAmbassadorTest.java diff --git a/ambassador/pom.xml b/ambassador/pom.xml index b230a2736..fc3dbf37c 100644 --- a/ambassador/pom.xml +++ b/ambassador/pom.xml @@ -10,6 +10,22 @@ 4.0.0 ambassador + + + org.testng + testng + RELEASE + test + + + junit + junit + + + org.junit.jupiter + junit-jupiter-api + + \ No newline at end of file diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/Client.java b/ambassador/src/main/java/com/iluwatar/ambassador/Client.java index 30bd834e6..39c0e7273 100644 --- a/ambassador/src/main/java/com/iluwatar/ambassador/Client.java +++ b/ambassador/src/main/java/com/iluwatar/ambassador/Client.java @@ -33,8 +33,9 @@ public class Client { serviceAmbassador = new ServiceAmbassador(); } - void useService(int value) { + long useService(int value) { long result = serviceAmbassador.doRemoteFunction(value); System.out.println(result); + return result; } } diff --git a/ambassador/src/test/java/com/iluwatar/ambassador/AppTest.java b/ambassador/src/test/java/com/iluwatar/ambassador/AppTest.java new file mode 100644 index 000000000..686a18af3 --- /dev/null +++ b/ambassador/src/test/java/com/iluwatar/ambassador/AppTest.java @@ -0,0 +1,36 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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.ambassador; + +import org.junit.jupiter.api.Test; + +/** + * Application test + */ +public class AppTest { + + @Test + public void test() { + App.main(new String[]{}); + } +} diff --git a/ambassador/src/test/java/com/iluwatar/ambassador/ClientTest.java b/ambassador/src/test/java/com/iluwatar/ambassador/ClientTest.java new file mode 100644 index 000000000..e02321fea --- /dev/null +++ b/ambassador/src/test/java/com/iluwatar/ambassador/ClientTest.java @@ -0,0 +1,15 @@ +package com.iluwatar.ambassador; + +import org.junit.jupiter.api.Test; + +public class ClientTest { + + @Test + public void test() { + + Client client = new Client(); + long result = client.useService(10); + assert result == 100 || result == -1; + + } +} diff --git a/ambassador/src/test/java/com/iluwatar/ambassador/RemoteServiceTest.java b/ambassador/src/test/java/com/iluwatar/ambassador/RemoteServiceTest.java new file mode 100644 index 000000000..0e7b6a58d --- /dev/null +++ b/ambassador/src/test/java/com/iluwatar/ambassador/RemoteServiceTest.java @@ -0,0 +1,16 @@ +package com.iluwatar.ambassador; + +import org.junit.jupiter.api.Test; + +public class RemoteServiceTest { + + @Test + public void test() { + + RemoteService remoteService = RemoteService.getRemoteService(); + long result = remoteService.doRemoteFunction(10); + + assert result == 100 || result == -1; + } + +} diff --git a/ambassador/src/test/java/com/iluwatar/ambassador/ServiceAmbassadorTest.java b/ambassador/src/test/java/com/iluwatar/ambassador/ServiceAmbassadorTest.java new file mode 100644 index 000000000..9bd87d3f8 --- /dev/null +++ b/ambassador/src/test/java/com/iluwatar/ambassador/ServiceAmbassadorTest.java @@ -0,0 +1,13 @@ +package com.iluwatar.ambassador; + +import org.junit.jupiter.api.Test; + +public class ServiceAmbassadorTest { + + @Test + public void test() { + ServiceAmbassador ambassador = new ServiceAmbassador(); + long result = ambassador.doRemoteFunction(10); + assert result == 100 || result == -1; + } +} From bae51f5803df47a857d7084a866c8adb1f314cda Mon Sep 17 00:00:00 2001 From: Ovidijus Okinskas Date: Mon, 4 Jun 2018 21:27:04 +0100 Subject: [PATCH 18/32] Adding license to test classes. --- .../com/iluwatar/ambassador/ClientTest.java | 22 +++++++++++++++++++ .../ambassador/RemoteServiceTest.java | 22 +++++++++++++++++++ .../ambassador/ServiceAmbassadorTest.java | 22 +++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/ambassador/src/test/java/com/iluwatar/ambassador/ClientTest.java b/ambassador/src/test/java/com/iluwatar/ambassador/ClientTest.java index e02321fea..efdb5f133 100644 --- a/ambassador/src/test/java/com/iluwatar/ambassador/ClientTest.java +++ b/ambassador/src/test/java/com/iluwatar/ambassador/ClientTest.java @@ -1,3 +1,25 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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.ambassador; import org.junit.jupiter.api.Test; diff --git a/ambassador/src/test/java/com/iluwatar/ambassador/RemoteServiceTest.java b/ambassador/src/test/java/com/iluwatar/ambassador/RemoteServiceTest.java index 0e7b6a58d..e8dfc62c2 100644 --- a/ambassador/src/test/java/com/iluwatar/ambassador/RemoteServiceTest.java +++ b/ambassador/src/test/java/com/iluwatar/ambassador/RemoteServiceTest.java @@ -1,3 +1,25 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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.ambassador; import org.junit.jupiter.api.Test; diff --git a/ambassador/src/test/java/com/iluwatar/ambassador/ServiceAmbassadorTest.java b/ambassador/src/test/java/com/iluwatar/ambassador/ServiceAmbassadorTest.java index 9bd87d3f8..613592dd4 100644 --- a/ambassador/src/test/java/com/iluwatar/ambassador/ServiceAmbassadorTest.java +++ b/ambassador/src/test/java/com/iluwatar/ambassador/ServiceAmbassadorTest.java @@ -1,3 +1,25 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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.ambassador; import org.junit.jupiter.api.Test; From 5393c96247bc5e8b3a29116add497ed0e4fdb049 Mon Sep 17 00:00:00 2001 From: Ovidijus Okinskas Date: Mon, 4 Jun 2018 22:32:05 +0100 Subject: [PATCH 19/32] Adding README.md --- ambassador/README.md | 181 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 ambassador/README.md diff --git a/ambassador/README.md b/ambassador/README.md new file mode 100644 index 000000000..44d0a8de8 --- /dev/null +++ b/ambassador/README.md @@ -0,0 +1,181 @@ +--- +layout: pattern +title: Ambassador +folder: ambassador +permalink: /patterns/ambassador/ +categories: Structural +tags: + - Java + - Difficulty-Intermediate +--- + +## Intent +Provide a helper service that sends network requests on behalf of a client and offload common additional connectivity tasks. + +## Explanation +Real world example + +> A remote service has many clients accessing a function it provides. The service is a legacy application and is impossible to update. Large numbers of requests from users are causing connectivity issues. New rules for request frequency should be implemented along with latency checks and client-side logging. + +In plain words + +> Using the ambassador pattern, we can implement less-frequent polling from clients along with latency checks and logging. + +Microsoft documentation states + +> An ambassador service can be thought of as an out-of-process proxy that is co-located with the client. + This pattern can be useful for offloading common client connectivity tasks such as monitoring, logging, routing, security (such as TLS), and resiliency patterns in a language agnostic way. It is often used with legacy applications, or other applications that are difficult to modify, in order to extend their networking capabilities. It can also enable a specialized team to implement those features. + +**Programmatic Example** + +With the above example in mind we will imitate the functionality in a simple manner. We have an interface implemented by the remote service as well as the ambassador service: + +```java +interface RemoteServiceInterface { + + long doRemoteFunction(int value) throws Exception; +} +``` + +A remote services represented as a singleton. + +```java +public class RemoteService implements RemoteServiceInterface { + + private static RemoteService service = null; + + static synchronized RemoteService getRemoteService() { + if (service == null) { + service = new RemoteService(); + } + return service; + } + + private RemoteService() { + + } + + @Override + public long doRemoteFunction(int value) { + + long waitTime = (long) Math.floor(Math.random() * 1000); + + try { + sleep(waitTime); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return waitTime >= 200 ? value * 10 : -1; + } +} +``` + +A service ambassador adding additional features such as logging, latency checks + +```java +public class ServiceAmbassador implements RemoteServiceInterface { + + private static final Logger LOGGER = LoggerFactory.getLogger(ServiceAmbassador.class); + private static final int RETRIES = 3; + private static final int DELAY_MS = 3000; + + ServiceAmbassador() { + + } + + @Override + public long doRemoteFunction(int value) { + + return safeCall(value); + } + + private long checkLatency(int value) { + RemoteService service = RemoteService.getRemoteService(); + long startTime = System.currentTimeMillis(); + long result = service.doRemoteFunction(value); + long timeTaken = System.currentTimeMillis() - startTime; + + LOGGER.info("Time taken (ms): " + timeTaken); + return result; + } + + private long safeCall(int value) { + + int retries = 0; + long result = -1; + + for (int i = 0; i < RETRIES; i++) { + + if (retries >= RETRIES) { + return -1; + } + + if ((result = checkLatency(value)) == -1) { + LOGGER.info("Failed to reach remote: (" + (i + 1) + ")"); + retries++; + try { + sleep(DELAY_MS); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } else { + break; + } + } + return result; + } +} +``` + +A client has a local service ambassador used to interact with the remote service: + +```java +public class Client { + + private ServiceAmbassador serviceAmbassador; + + Client() { + serviceAmbassador = new ServiceAmbassador(); + } + + long useService(int value) { + long result = serviceAmbassador.doRemoteFunction(value); + System.out.println(result); + return result; + } +} +``` + +And here are two clients using the service. + +```java +Client host1 = new Client(); +Client host2 = new Client(); +host1.useService(12); +host2.useService(73); +``` + +## Applicability +Ambassador is applicable when working with a legacy remote service that cannot +be modified or would be extremely difficult to modify. Connectivity features can +be implemented on the client avoiding the need for changes on the remote service. + +* Ambassador provides a local interface for a remote service. +* Ambassador provides logging, circuit breaking, retries and security on the client. + +## Typical Use Case + +* Control access to another object +* Implement logging +* Implement circuit breaking +* Offload remote service tasks +* Facilitate network connection + +## Real world examples + +* [Kubernetes-native API gateway for microservices](https://github.com/datawire/ambassador) + +## Credits + +* [Ambassador pattern](https://docs.microsoft.com/en-us/azure/architecture/patterns/ambassador) +* [Designing Distributed Systems: Patterns and Paradigms for Scalable, Reliable Services](https://books.google.co.uk/books?id=6BJNDwAAQBAJ&pg=PT35&lpg=PT35&dq=ambassador+pattern+in+real+world&source=bl&ots=d2e7GhYdHi&sig=Lfl_MDnCgn6lUcjzOg4GXrN13bQ&hl=en&sa=X&ved=0ahUKEwjk9L_18rrbAhVpKcAKHX_KA7EQ6AEIWTAI#v=onepage&q=ambassador%20pattern%20in%20real%20world&f=false) From ff579fabcf500d92c048c19f2fc4b4e7177b7280 Mon Sep 17 00:00:00 2001 From: Ovidijus Okinskas Date: Mon, 4 Jun 2018 22:50:59 +0100 Subject: [PATCH 20/32] Fixing CheckStyle issues. --- .../java/com/iluwatar/ambassador/App.java | 19 +++-- .../java/com/iluwatar/ambassador/Client.java | 18 ++--- .../iluwatar/ambassador/RemoteService.java | 52 +++++++------- .../ambassador/RemoteServiceInterface.java | 4 +- .../ambassador/ServiceAmbassador.java | 72 +++++++++---------- 5 files changed, 82 insertions(+), 83 deletions(-) diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/App.java b/ambassador/src/main/java/com/iluwatar/ambassador/App.java index 32c5f0f1c..e4b4d392a 100644 --- a/ambassador/src/main/java/com/iluwatar/ambassador/App.java +++ b/ambassador/src/main/java/com/iluwatar/ambassador/App.java @@ -41,14 +41,13 @@ package com.iluwatar.ambassador; */ public class App { - /** - * Entry point - */ - public static void main(String[] args) { - - Client host1 = new Client(); - Client host2 = new Client(); - host1.useService(12); - host2.useService(73); - } + /** + * Entry point + */ + public static void main(String[] args) { + Client host1 = new Client(); + Client host2 = new Client(); + host1.useService(12); + host2.useService(73); + } } diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/Client.java b/ambassador/src/main/java/com/iluwatar/ambassador/Client.java index 39c0e7273..eb02b9ee3 100644 --- a/ambassador/src/main/java/com/iluwatar/ambassador/Client.java +++ b/ambassador/src/main/java/com/iluwatar/ambassador/Client.java @@ -27,15 +27,15 @@ package com.iluwatar.ambassador; */ public class Client { - private ServiceAmbassador serviceAmbassador; + private ServiceAmbassador serviceAmbassador; - Client() { - serviceAmbassador = new ServiceAmbassador(); - } + Client() { + serviceAmbassador = new ServiceAmbassador(); + } - long useService(int value) { - long result = serviceAmbassador.doRemoteFunction(value); - System.out.println(result); - return result; - } + long useService(int value) { + long result = serviceAmbassador.doRemoteFunction(value); + System.out.println(result); + return result; + } } diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java b/ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java index 668e776ce..cd34ae398 100644 --- a/ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java +++ b/ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java @@ -29,35 +29,35 @@ import static java.lang.Thread.sleep; */ public class RemoteService implements RemoteServiceInterface { - private static RemoteService service = null; + private static RemoteService service = null; - static synchronized RemoteService getRemoteService() { - if (service == null) { - service = new RemoteService(); - } - return service; + static synchronized RemoteService getRemoteService() { + if (service == null) { + service = new RemoteService(); } + return service; + } - private RemoteService() { + private RemoteService() { + } + + /** + * Remote function takes a value and multiplies it by 10 taking a random amount of time. + * Will sometimes return -1. This immitates connectivity issues a client might have to account for. + * @param value integer value to be multiplied. + * @return if waitTime is more than 200ms, it returns value * 10, otherwise -1. + */ + @Override + public long doRemoteFunction(int value) { + + long waitTime = (long) Math.floor(Math.random() * 1000); + + try { + sleep(waitTime); + } catch (InterruptedException e) { + e.printStackTrace(); } - - /** - * Remote function takes a value and multiplies it by 10 taking a random amount of time. - * Will sometimes return -1. This immitates connectivity issues a client might have to account for. - * @param value integer value to be multiplied. - * @return if waitTime is more than 200ms, it returns value * 10, otherwise -1. - */ - @Override - public long doRemoteFunction(int value) { - - long waitTime = (long) Math.floor(Math.random() * 1000); - - try { - sleep(waitTime); - } catch (InterruptedException e) { - e.printStackTrace(); - } - return waitTime >= 200 ? value * 10 : -1; - } + return waitTime >= 200 ? value * 10 : -1; + } } diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/RemoteServiceInterface.java b/ambassador/src/main/java/com/iluwatar/ambassador/RemoteServiceInterface.java index 9f1112da9..940313de3 100644 --- a/ambassador/src/main/java/com/iluwatar/ambassador/RemoteServiceInterface.java +++ b/ambassador/src/main/java/com/iluwatar/ambassador/RemoteServiceInterface.java @@ -26,6 +26,6 @@ package com.iluwatar.ambassador; * Interface shared by ({@link RemoteService}) and ({@link ServiceAmbassador}). */ interface RemoteServiceInterface { - - long doRemoteFunction(int value) throws Exception; + + long doRemoteFunction(int value) throws Exception; } diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java b/ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java index 77c7197ff..54426f6cc 100644 --- a/ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java +++ b/ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java @@ -36,53 +36,53 @@ import static java.lang.Thread.sleep; */ public class ServiceAmbassador implements RemoteServiceInterface { - private static final Logger LOGGER = LoggerFactory.getLogger(ServiceAmbassador.class); - private static final int RETRIES = 3; - private static final int DELAY_MS = 3000; + private static final Logger LOGGER = LoggerFactory.getLogger(ServiceAmbassador.class); + private static final int RETRIES = 3; + private static final int DELAY_MS = 3000; - ServiceAmbassador() { + ServiceAmbassador() { - } + } - @Override - public long doRemoteFunction(int value) { + @Override + public long doRemoteFunction(int value) { - return safeCall(value); - } + return safeCall(value); + } - private long checkLatency(int value) { - RemoteService service = RemoteService.getRemoteService(); - long startTime = System.currentTimeMillis(); - long result = service.doRemoteFunction(value); - long timeTaken = System.currentTimeMillis() - startTime; + private long checkLatency(int value) { + RemoteService service = RemoteService.getRemoteService(); + long startTime = System.currentTimeMillis(); + long result = service.doRemoteFunction(value); + long timeTaken = System.currentTimeMillis() - startTime; - LOGGER.info("Time taken (ms): " + timeTaken); - return result; - } + LOGGER.info("Time taken (ms): " + timeTaken); + return result; + } - private long safeCall(int value) { + private long safeCall(int value) { - int retries = 0; - long result = -1; + int retries = 0; + long result = -1; - for (int i = 0; i < RETRIES; i++) { + for (int i = 0; i < RETRIES; i++) { - if (retries >= RETRIES) { - return -1; - } + if (retries >= RETRIES) { + return -1; + } - if ((result = checkLatency(value)) == -1) { - LOGGER.info("Failed to reach remote: (" + (i + 1) + ")"); - retries++; - try { - sleep(DELAY_MS); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } else { - break; - } + if ((result = checkLatency(value)) == -1) { + LOGGER.info("Failed to reach remote: (" + (i + 1) + ")"); + retries++; + try { + sleep(DELAY_MS); + } catch (InterruptedException e) { + e.printStackTrace(); } - return result; + } else { + break; + } } + return result; + } } From c4dd94a019d937973b4832f679a4a0711cae2a0e Mon Sep 17 00:00:00 2001 From: Ovidijus Okinskas Date: Tue, 5 Jun 2018 07:32:29 +0100 Subject: [PATCH 21/32] Fixing CheckStyle issues in Test classes. --- .../java/com/iluwatar/ambassador/AppTest.java | 8 ++++---- .../java/com/iluwatar/ambassador/ClientTest.java | 15 +++++++++------ .../iluwatar/ambassador/RemoteServiceTest.java | 16 +++++++++------- .../ambassador/ServiceAmbassadorTest.java | 15 +++++++++------ 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/ambassador/src/test/java/com/iluwatar/ambassador/AppTest.java b/ambassador/src/test/java/com/iluwatar/ambassador/AppTest.java index 686a18af3..cb072c1eb 100644 --- a/ambassador/src/test/java/com/iluwatar/ambassador/AppTest.java +++ b/ambassador/src/test/java/com/iluwatar/ambassador/AppTest.java @@ -29,8 +29,8 @@ import org.junit.jupiter.api.Test; */ public class AppTest { - @Test - public void test() { - App.main(new String[]{}); - } + @Test + public void test() { + App.main(new String[]{}); + } } diff --git a/ambassador/src/test/java/com/iluwatar/ambassador/ClientTest.java b/ambassador/src/test/java/com/iluwatar/ambassador/ClientTest.java index efdb5f133..9354c4f86 100644 --- a/ambassador/src/test/java/com/iluwatar/ambassador/ClientTest.java +++ b/ambassador/src/test/java/com/iluwatar/ambassador/ClientTest.java @@ -24,14 +24,17 @@ package com.iluwatar.ambassador; import org.junit.jupiter.api.Test; +/** + * Test for {@link Client} + */ public class ClientTest { - @Test - public void test() { + @Test + public void test() { - Client client = new Client(); - long result = client.useService(10); - assert result == 100 || result == -1; + Client client = new Client(); + long result = client.useService(10); - } + assert result == 100 || result == -1; + } } diff --git a/ambassador/src/test/java/com/iluwatar/ambassador/RemoteServiceTest.java b/ambassador/src/test/java/com/iluwatar/ambassador/RemoteServiceTest.java index e8dfc62c2..e2e0e9be9 100644 --- a/ambassador/src/test/java/com/iluwatar/ambassador/RemoteServiceTest.java +++ b/ambassador/src/test/java/com/iluwatar/ambassador/RemoteServiceTest.java @@ -24,15 +24,17 @@ package com.iluwatar.ambassador; import org.junit.jupiter.api.Test; +/** + * Test for {@link RemoteService} + */ public class RemoteServiceTest { - @Test - public void test() { + @Test + public void test() { - RemoteService remoteService = RemoteService.getRemoteService(); - long result = remoteService.doRemoteFunction(10); - - assert result == 100 || result == -1; - } + RemoteService remoteService = RemoteService.getRemoteService(); + long result = remoteService.doRemoteFunction(10); + assert result == 100 || result == -1; + } } diff --git a/ambassador/src/test/java/com/iluwatar/ambassador/ServiceAmbassadorTest.java b/ambassador/src/test/java/com/iluwatar/ambassador/ServiceAmbassadorTest.java index 613592dd4..ba701d426 100644 --- a/ambassador/src/test/java/com/iluwatar/ambassador/ServiceAmbassadorTest.java +++ b/ambassador/src/test/java/com/iluwatar/ambassador/ServiceAmbassadorTest.java @@ -24,12 +24,15 @@ package com.iluwatar.ambassador; import org.junit.jupiter.api.Test; +/** + * Test for {@link ServiceAmbassador} + */ public class ServiceAmbassadorTest { - @Test - public void test() { - ServiceAmbassador ambassador = new ServiceAmbassador(); - long result = ambassador.doRemoteFunction(10); - assert result == 100 || result == -1; - } + @Test + public void test() { + ServiceAmbassador ambassador = new ServiceAmbassador(); + long result = ambassador.doRemoteFunction(10); + assert result == 100 || result == -1; + } } From 868e4561b5749a33842e4988bdbf869f25d4df0e Mon Sep 17 00:00:00 2001 From: Ovidijus Okinskas Date: Tue, 5 Jun 2018 21:16:16 +0100 Subject: [PATCH 22/32] Updating pom dependencies and adding license. --- ambassador/pom.xml | 69 +++++++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/ambassador/pom.xml b/ambassador/pom.xml index fc3dbf37c..f47b76f9a 100644 --- a/ambassador/pom.xml +++ b/ambassador/pom.xml @@ -1,31 +1,48 @@ + - - java-design-patterns - com.iluwatar - 1.20.0-SNAPSHOT - - 4.0.0 - - ambassador - - - org.testng - testng - RELEASE - test - - - junit - junit - - - org.junit.jupiter - junit-jupiter-api - - - - + + java-design-patterns + com.iluwatar + 1.20.0-SNAPSHOT + + 4.0.0 + ambassador + + + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.jupiter + junit-jupiter-engine + test + + \ No newline at end of file From 179a02bcdd3d6d7622715c045d28def548704ef1 Mon Sep 17 00:00:00 2001 From: Argyro-Sioziou Date: Thu, 14 Jun 2018 11:52:23 +0300 Subject: [PATCH 23/32] Removing default constructors --- .../src/main/java/com/iluwatar/acyclicvisitor/Hayes.java | 4 ---- .../src/main/java/com/iluwatar/acyclicvisitor/Zoom.java | 4 ---- 2 files changed, 8 deletions(-) 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 30408a758..657a669b0 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Hayes.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Hayes.java @@ -32,10 +32,6 @@ public class Hayes extends Modem { private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureForDosVisitor.class); - public Hayes() { - - } - /** * Accept visitor */ 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 176a22ea4..f14c9e7e6 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Zoom.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Zoom.java @@ -32,10 +32,6 @@ public class Zoom extends Modem { private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureForDosVisitor.class); - public Zoom() { - - } - /** * Accept visitor */ From 49feead13f73fda032da86a9dce7a449b4f52b93 Mon Sep 17 00:00:00 2001 From: Argyro-Sioziou Date: Thu, 14 Jun 2018 11:59:02 +0300 Subject: [PATCH 24/32] Removing interfaces' test classes --- .../acyclicvisitor/HayesVisitorTest.java | 30 ------------------- .../iluwatar/acyclicvisitor/ModemTest.java | 30 ------------------- .../acyclicvisitor/ModemVisitorTest.java | 30 ------------------- .../acyclicvisitor/ZoomVisitorTest.java | 30 ------------------- 4 files changed, 120 deletions(-) delete mode 100644 acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/HayesVisitorTest.java delete mode 100644 acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ModemTest.java delete mode 100644 acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ModemVisitorTest.java delete mode 100644 acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ZoomVisitorTest.java diff --git a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/HayesVisitorTest.java b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/HayesVisitorTest.java deleted file mode 100644 index 96244f879..000000000 --- a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/HayesVisitorTest.java +++ /dev/null @@ -1,30 +0,0 @@ -/** - * The MIT License - * Copyright (c) 2014-2016 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 test class - */ -public interface HayesVisitorTest { - -} \ No newline at end of file diff --git a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ModemTest.java b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ModemTest.java deleted file mode 100644 index 0f7d3d629..000000000 --- a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ModemTest.java +++ /dev/null @@ -1,30 +0,0 @@ -/** - * The MIT License - * Copyright (c) 2014-2016 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; - -/** - * Modem test class - */ -public abstract class ModemTest { - -} diff --git a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ModemVisitorTest.java b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ModemVisitorTest.java deleted file mode 100644 index 681925f05..000000000 --- a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ModemVisitorTest.java +++ /dev/null @@ -1,30 +0,0 @@ -/** - * The MIT License - * Copyright (c) 2014-2016 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; - -/** - * ModemVisitor test class - */ -public interface ModemVisitorTest { - -} diff --git a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ZoomVisitorTest.java b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ZoomVisitorTest.java deleted file mode 100644 index 88aab9636..000000000 --- a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ZoomVisitorTest.java +++ /dev/null @@ -1,30 +0,0 @@ -/** - * The MIT License - * Copyright (c) 2014-2016 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 test class - */ -public interface ZoomVisitorTest { - -} From 6167d06dce9116c9f07c91439e6d297ea72309f9 Mon Sep 17 00:00:00 2001 From: Argyro-Sioziou Date: Thu, 14 Jun 2018 12:02:34 +0300 Subject: [PATCH 25/32] Removing test case --- .../acyclicvisitor/ConfigureForUnixVisitorTest.java | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitorTest.java b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitorTest.java index 4346ffd30..449d242dc 100644 --- a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitorTest.java +++ b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitorTest.java @@ -62,14 +62,4 @@ public class ConfigureForUnixVisitorTest { assertThat(logger.getLoggingEvents()).extracting("level", "message").contains( tuple(INFO, zoom + " used with Unix configurator.")); } - - @Test - public void testVisitForHayes() { - ConfigureForUnixVisitor conUnix = new ConfigureForUnixVisitor(); - Hayes hayes = mock(Hayes.class); - - Assertions.assertThrows(ClassCastException.class, () -> { - ((HayesVisitor)conUnix).visit(hayes); - }); - } } From 8b1ed95d6b669c94d5b45b46a2e5d05234f597ad Mon Sep 17 00:00:00 2001 From: Argyro-Sioziou Date: Thu, 14 Jun 2018 12:14:35 +0300 Subject: [PATCH 26/32] Updating ConfigureForDosVisitorTest and ConfigureForUnixVisitorTest classes --- .../acyclicvisitor/ConfigureForDosVisitorTest.java | 8 ++++---- .../acyclicvisitor/ConfigureForUnixVisitorTest.java | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitorTest.java b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitorTest.java index 78d442caa..e8d6ba079 100644 --- a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitorTest.java +++ b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitorTest.java @@ -49,9 +49,9 @@ public class ConfigureForDosVisitorTest { @Test public void testVisitForZoom() { ConfigureForDosVisitor conDos = new ConfigureForDosVisitor(); - Zoom zoom = mock(Zoom.class); + Zoom zoom = new Zoom(); - ((ZoomVisitor)conDos).visit(zoom); + conDos.visit(zoom); assertThat(logger.getLoggingEvents()).extracting("level", "message").contains( tuple(INFO, zoom + " used with Dos configurator.")); @@ -60,9 +60,9 @@ public class ConfigureForDosVisitorTest { @Test public void testVisitForHayes() { ConfigureForDosVisitor conDos = new ConfigureForDosVisitor(); - Hayes hayes = mock(Hayes.class); + Hayes hayes = new Hayes(); - ((HayesVisitor)conDos).visit(hayes); + conDos.visit(hayes); assertThat(logger.getLoggingEvents()).extracting("level", "message").contains( tuple(INFO, hayes + " used with Dos configurator.")); diff --git a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitorTest.java b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitorTest.java index 449d242dc..0cee046a8 100644 --- a/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitorTest.java +++ b/acyclic-visitor/src/test/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitorTest.java @@ -55,9 +55,9 @@ public class ConfigureForUnixVisitorTest { @Test public void testVisitForZoom() { ConfigureForUnixVisitor conUnix = new ConfigureForUnixVisitor(); - Zoom zoom = mock(Zoom.class); + Zoom zoom = new Zoom(); - ((ZoomVisitor)conUnix).visit(zoom); + conUnix.visit(zoom); assertThat(logger.getLoggingEvents()).extracting("level", "message").contains( tuple(INFO, zoom + " used with Unix configurator.")); From 6a0fa747ad6789ecff06069ab0d07e3dca4955f3 Mon Sep 17 00:00:00 2001 From: Argyro-Sioziou Date: Thu, 14 Jun 2018 12:18:46 +0300 Subject: [PATCH 27/32] Fixing typos --- .../com/iluwatar/acyclicvisitor/ConfigureForDosVisitor.java | 2 +- .../com/iluwatar/acyclicvisitor/ConfigureForUnixVisitor.java | 2 +- .../src/main/java/com/iluwatar/acyclicvisitor/ModemVisitor.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitor.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitor.java index b71893474..4a33d26c1 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitor.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitor.java @@ -26,7 +26,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * CongigureForDosVisitor class implements both zoom's and hayes' visit method + * ConfigureForDosVisitor class implements both zoom's and hayes' visit method * for Dos manufacturer */ public class ConfigureForDosVisitor implements ModemVisitor, HayesVisitor, ZoomVisitor { 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 9e35ea0a6..d3ef3bf6c 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitor.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitor.java @@ -26,7 +26,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * CongigureForDosVisitor class implements both zoom's visit method for Unix + * ConfigureForUnixVisitor class implements zoom's visit method for Unix * manufacturer */ public class ConfigureForUnixVisitor implements ModemVisitor, ZoomVisitor { diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ModemVisitor.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ModemVisitor.java index f5668756c..a9aaad6d4 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ModemVisitor.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ModemVisitor.java @@ -23,7 +23,7 @@ package com.iluwatar.acyclicvisitor; /** - * ZoomVisitor interface + * ModemVisitor interface */ public interface ModemVisitor { // Visitor is a degenerate base class for all visitors. From 6636fb151b2620102733c8689ec0f2916d83ad6c Mon Sep 17 00:00:00 2001 From: Argyro-Sioziou Date: Thu, 14 Jun 2018 12:40:41 +0300 Subject: [PATCH 28/32] Adding AllModemVisitor interface --- .../acyclicvisitor/AllModemVisitor.java | 27 +++++++++++++++++++ .../ConfigureForDosVisitor.java | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/AllModemVisitor.java diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/AllModemVisitor.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/AllModemVisitor.java new file mode 100644 index 000000000..8a0c29b8e --- /dev/null +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/AllModemVisitor.java @@ -0,0 +1,27 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 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; + +public interface AllModemVisitor extends ModemVisitor, ZoomVisitor, HayesVisitor{ + +} diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitor.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitor.java index 4a33d26c1..a6d66b1db 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitor.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitor.java @@ -29,7 +29,7 @@ import org.slf4j.LoggerFactory; * ConfigureForDosVisitor class implements both zoom's and hayes' visit method * for Dos manufacturer */ -public class ConfigureForDosVisitor implements ModemVisitor, HayesVisitor, ZoomVisitor { +public class ConfigureForDosVisitor implements AllModemVisitor { private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureForDosVisitor.class); From 04d80f05c8e4ce5a9a2b0124a4011baf3c7215b1 Mon Sep 17 00:00:00 2001 From: Argyro-Sioziou Date: Thu, 14 Jun 2018 17:56:14 +0300 Subject: [PATCH 29/32] Adding consequences in README.md --- acyclic-visitor/README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/acyclic-visitor/README.md b/acyclic-visitor/README.md index 33c894ce7..939fe38b9 100644 --- a/acyclic-visitor/README.md +++ b/acyclic-visitor/README.md @@ -22,6 +22,16 @@ This pattern can be used: * 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. +## Consequences +The good: +* No dependency cycles between class hierarchies. +* No need to recompile all the visitors if a new one is added. +* Does not cause compilation failure in existing visitors if class hierarchy has a new member. + +The bad: +* Violates the principle of least surprise or Liskov's 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 * [Visitor Pattern](../visitor/README.md) From 51659fe9c55aa3aadd2fd7931da39f56e85ac95c Mon Sep 17 00:00:00 2001 From: Argyro-Sioziou Date: Thu, 14 Jun 2018 18:04:30 +0300 Subject: [PATCH 30/32] Updating HayesVisitor and ZoomVisitor --- .../src/main/java/com/iluwatar/acyclicvisitor/HayesVisitor.java | 2 +- .../src/main/java/com/iluwatar/acyclicvisitor/ZoomVisitor.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 90e8a2a1b..1731622f2 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/HayesVisitor.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/HayesVisitor.java @@ -25,6 +25,6 @@ package com.iluwatar.acyclicvisitor; /** * HayesVisitor interface */ -public interface HayesVisitor { +public interface HayesVisitor extends ModemVisitor { void visit(Hayes hayes); } 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 85646921c..b4c949ed7 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ZoomVisitor.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ZoomVisitor.java @@ -25,6 +25,6 @@ package com.iluwatar.acyclicvisitor; /** * ZoomVisitor interface */ -public interface ZoomVisitor { +public interface ZoomVisitor extends ModemVisitor{ void visit(Zoom zoom); } From 57f60c5ebfdbf538823c88323bc9841505dc3ec4 Mon Sep 17 00:00:00 2001 From: Argyro-Sioziou Date: Thu, 14 Jun 2018 18:30:46 +0300 Subject: [PATCH 31/32] Updating javadoc comments --- .../java/com/iluwatar/acyclicvisitor/AllModemVisitor.java | 3 +++ .../src/main/java/com/iluwatar/acyclicvisitor/App.java | 2 +- .../src/main/java/com/iluwatar/acyclicvisitor/Hayes.java | 2 +- .../main/java/com/iluwatar/acyclicvisitor/ModemVisitor.java | 4 +++- .../src/main/java/com/iluwatar/acyclicvisitor/Zoom.java | 2 +- 5 files changed, 9 insertions(+), 4 deletions(-) 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 8a0c29b8e..e6f583446 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/AllModemVisitor.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/AllModemVisitor.java @@ -22,6 +22,9 @@ */ package com.iluwatar.acyclicvisitor; +/** + * All ModemVisitor interface extends all visitor interfaces + */ public interface AllModemVisitor extends ModemVisitor, ZoomVisitor, HayesVisitor{ } diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/App.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/App.java index 97fdddf14..06e4bd2f2 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/App.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/App.java @@ -30,7 +30,7 @@ package com.iluwatar.acyclicvisitor; *

* In this example the visitor base class is {@link ModemVisitor}. The base class of the * visited hierarchy is {@link Modem} and has two children {@link Hayes} and {@link Zoom} - * each one having its own visitor interface {@link Hayes} and {@link ZoomVisitor} + * each one having its own visitor interface {@link HayesVisitor} and {@link ZoomVisitor} * respectively. {@link ConfigureForUnixVisitor} and {@link ConfigureForDosVisitor} * implement each derivative's visit method only if it is required */ 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 657a669b0..1128057bf 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Hayes.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Hayes.java @@ -33,7 +33,7 @@ public class Hayes extends Modem { private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureForDosVisitor.class); /** - * Accept visitor + * Accepts all visitors but honors only HayesVisitor */ @Override public void accept(ModemVisitor modemVisitor) { diff --git a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ModemVisitor.java b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ModemVisitor.java index a9aaad6d4..7189a6bba 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ModemVisitor.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ModemVisitor.java @@ -23,7 +23,9 @@ package com.iluwatar.acyclicvisitor; /** - * ModemVisitor interface + * ModemVisitor interface does not contain any visit methods so that it does not + * depend on the visited hierarchy. Each derivative's visit method is declared in + * its own visitor interface */ public interface ModemVisitor { // Visitor is a degenerate base class for all visitors. 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 f14c9e7e6..7091d79df 100644 --- a/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Zoom.java +++ b/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Zoom.java @@ -33,7 +33,7 @@ public class Zoom extends Modem { private static final Logger LOGGER = LoggerFactory.getLogger(ConfigureForDosVisitor.class); /** - * Accept visitor + * Accepts all visitors but honors only ZoomVisitor */ @Override public void accept(ModemVisitor modemVisitor) { From 0453bf9063a8518ce1dd56dcde4eeed106dd4954 Mon Sep 17 00:00:00 2001 From: Ovidijus Okinskas Date: Sat, 23 Jun 2018 13:24:07 +0100 Subject: [PATCH 32/32] General cleanup. Simplifying code. Replacing all prints with appropriate Logger. --- ambassador/README.md | 19 ++++++++----------- .../java/com/iluwatar/ambassador/Client.java | 13 +++++++------ .../iluwatar/ambassador/RemoteService.java | 12 +++++++----- .../ambassador/ServiceAmbassador.java | 10 +++------- .../ambassador/RemoteServiceTest.java | 5 +---- .../ambassador/ServiceAmbassadorTest.java | 3 +-- 6 files changed, 27 insertions(+), 35 deletions(-) diff --git a/ambassador/README.md b/ambassador/README.md index 44d0a8de8..f23d1e13c 100644 --- a/ambassador/README.md +++ b/ambassador/README.md @@ -10,7 +10,7 @@ tags: --- ## Intent -Provide a helper service that sends network requests on behalf of a client and offload common additional connectivity tasks. +Provide a helper service instance on a client and offload common functionality away from a shared resource. ## Explanation Real world example @@ -42,7 +42,8 @@ A remote services represented as a singleton. ```java public class RemoteService implements RemoteServiceInterface { - private static RemoteService service = null; + private static final Logger LOGGER = LoggerFactory.getLogger(RemoteService.class); + private static RemoteService service = null;2 static synchronized RemoteService getRemoteService() { if (service == null) { @@ -51,9 +52,7 @@ public class RemoteService implements RemoteServiceInterface { return service; } - private RemoteService() { - - } + private RemoteService() {} @Override public long doRemoteFunction(int value) { @@ -63,7 +62,7 @@ public class RemoteService implements RemoteServiceInterface { try { sleep(waitTime); } catch (InterruptedException e) { - e.printStackTrace(); + LOGGER.error("Thread sleep interrupted", e) } return waitTime >= 200 ? value * 10 : -1; } @@ -79,9 +78,7 @@ public class ServiceAmbassador implements RemoteServiceInterface { private static final int RETRIES = 3; private static final int DELAY_MS = 3000; - ServiceAmbassador() { - - } + ServiceAmbassador() {} @Override public long doRemoteFunction(int value) { @@ -116,7 +113,7 @@ public class ServiceAmbassador implements RemoteServiceInterface { try { sleep(DELAY_MS); } catch (InterruptedException e) { - e.printStackTrace(); + LOGGER.error("Thread sleep state interrupted", e); } } else { break; @@ -140,7 +137,7 @@ public class Client { long useService(int value) { long result = serviceAmbassador.doRemoteFunction(value); - System.out.println(result); + LOGGER.info("Service result: " + result) return result; } } diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/Client.java b/ambassador/src/main/java/com/iluwatar/ambassador/Client.java index eb02b9ee3..983226530 100644 --- a/ambassador/src/main/java/com/iluwatar/ambassador/Client.java +++ b/ambassador/src/main/java/com/iluwatar/ambassador/Client.java @@ -22,20 +22,21 @@ */ package com.iluwatar.ambassador; +import org.slf4j.LoggerFactory; + +import org.slf4j.Logger; + /** * A simple Client */ public class Client { - private ServiceAmbassador serviceAmbassador; - - Client() { - serviceAmbassador = new ServiceAmbassador(); - } + private static final Logger LOGGER = LoggerFactory.getLogger(Client.class); + private final ServiceAmbassador serviceAmbassador = new ServiceAmbassador(); long useService(int value) { long result = serviceAmbassador.doRemoteFunction(value); - System.out.println(result); + LOGGER.info("Service result: " + result); return result; } } diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java b/ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java index cd34ae398..225e033d8 100644 --- a/ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java +++ b/ambassador/src/main/java/com/iluwatar/ambassador/RemoteService.java @@ -22,6 +22,9 @@ */ package com.iluwatar.ambassador; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import static java.lang.Thread.sleep; /** @@ -29,6 +32,7 @@ import static java.lang.Thread.sleep; */ public class RemoteService implements RemoteServiceInterface { + private static final Logger LOGGER = LoggerFactory.getLogger(RemoteService.class); private static RemoteService service = null; static synchronized RemoteService getRemoteService() { @@ -38,13 +42,11 @@ public class RemoteService implements RemoteServiceInterface { return service; } - private RemoteService() { - - } + private RemoteService() {} /** * Remote function takes a value and multiplies it by 10 taking a random amount of time. - * Will sometimes return -1. This immitates connectivity issues a client might have to account for. + * Will sometimes return -1. This imitates connectivity issues a client might have to account for. * @param value integer value to be multiplied. * @return if waitTime is more than 200ms, it returns value * 10, otherwise -1. */ @@ -56,7 +58,7 @@ public class RemoteService implements RemoteServiceInterface { try { sleep(waitTime); } catch (InterruptedException e) { - e.printStackTrace(); + LOGGER.error("Thread sleep state interrupted", e); } return waitTime >= 200 ? value * 10 : -1; } diff --git a/ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java b/ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java index 54426f6cc..4be1dfcbf 100644 --- a/ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java +++ b/ambassador/src/main/java/com/iluwatar/ambassador/ServiceAmbassador.java @@ -40,20 +40,16 @@ public class ServiceAmbassador implements RemoteServiceInterface { private static final int RETRIES = 3; private static final int DELAY_MS = 3000; - ServiceAmbassador() { - - } + ServiceAmbassador() {} @Override public long doRemoteFunction(int value) { - return safeCall(value); } private long checkLatency(int value) { - RemoteService service = RemoteService.getRemoteService(); long startTime = System.currentTimeMillis(); - long result = service.doRemoteFunction(value); + long result = RemoteService.getRemoteService().doRemoteFunction(value); long timeTaken = System.currentTimeMillis() - startTime; LOGGER.info("Time taken (ms): " + timeTaken); @@ -77,7 +73,7 @@ public class ServiceAmbassador implements RemoteServiceInterface { try { sleep(DELAY_MS); } catch (InterruptedException e) { - e.printStackTrace(); + LOGGER.error("Thread sleep state interrupted", e); } } else { break; diff --git a/ambassador/src/test/java/com/iluwatar/ambassador/RemoteServiceTest.java b/ambassador/src/test/java/com/iluwatar/ambassador/RemoteServiceTest.java index e2e0e9be9..e0b297af4 100644 --- a/ambassador/src/test/java/com/iluwatar/ambassador/RemoteServiceTest.java +++ b/ambassador/src/test/java/com/iluwatar/ambassador/RemoteServiceTest.java @@ -31,10 +31,7 @@ public class RemoteServiceTest { @Test public void test() { - - RemoteService remoteService = RemoteService.getRemoteService(); - long result = remoteService.doRemoteFunction(10); - + long result = RemoteService.getRemoteService().doRemoteFunction(10); assert result == 100 || result == -1; } } diff --git a/ambassador/src/test/java/com/iluwatar/ambassador/ServiceAmbassadorTest.java b/ambassador/src/test/java/com/iluwatar/ambassador/ServiceAmbassadorTest.java index ba701d426..432df9fd1 100644 --- a/ambassador/src/test/java/com/iluwatar/ambassador/ServiceAmbassadorTest.java +++ b/ambassador/src/test/java/com/iluwatar/ambassador/ServiceAmbassadorTest.java @@ -31,8 +31,7 @@ public class ServiceAmbassadorTest { @Test public void test() { - ServiceAmbassador ambassador = new ServiceAmbassador(); - long result = ambassador.doRemoteFunction(10); + long result = new ServiceAmbassador().doRemoteFunction(10); assert result == 100 || result == -1; } }