diff --git a/core-java-modules/core-java-16/pom.xml b/core-java-modules/core-java-16/pom.xml index 4adc3ee6d1..a2c0d4855b 100644 --- a/core-java-modules/core-java-16/pom.xml +++ b/core-java-modules/core-java-16/pom.xml @@ -63,4 +63,4 @@ 3.0.0-M5 - \ No newline at end of file + diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/enumallt/DinosaurEnum.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/enumallt/DinosaurEnum.java new file mode 100644 index 0000000000..a50f79947c --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/enumallt/DinosaurEnum.java @@ -0,0 +1,19 @@ +package com.baeldung.instanceofalternative.enumallt; + +public enum DinosaurEnum { + Anatotitan { + @Override + public String move() { + return "running"; + } + }, + Euraptor { + @Override + public String move() { + return "flying"; + } + }; + + public abstract String move(); + +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/model/Anatotitan.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/model/Anatotitan.java new file mode 100644 index 0000000000..ce59b58ad7 --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/model/Anatotitan.java @@ -0,0 +1,15 @@ +package com.baeldung.instanceofalternative.model; + +public class Anatotitan extends Dinosaur { + // polymorphism + @Override + public String move() { + return "running"; + } + + // non-polymorphism + public String run() { + return "running"; + } + +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/model/Dinosaur.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/model/Dinosaur.java new file mode 100644 index 0000000000..38055054f5 --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/model/Dinosaur.java @@ -0,0 +1,9 @@ +package com.baeldung.instanceofalternative.model; + +public class Dinosaur { + + public String move() { + return "default movement"; + } + +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/model/Euraptor.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/model/Euraptor.java new file mode 100644 index 0000000000..1de5257a0d --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/model/Euraptor.java @@ -0,0 +1,15 @@ +package com.baeldung.instanceofalternative.model; + +public class Euraptor extends Dinosaur { + // polymorphism + @Override + public String move() { + return "flying"; + } + + // non-polymorphism + public String flies() { + return "flying"; + } + +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Anatotitan.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Anatotitan.java new file mode 100644 index 0000000000..84d93e7350 --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Anatotitan.java @@ -0,0 +1,14 @@ +package com.baeldung.instanceofalternative.visitorspattern; + +public class Anatotitan implements Dino { + + String run() { + return "running"; + } + + @Override + public String move(Visitor dinobehave) { + return dinobehave.visit(this); + } + +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Dino.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Dino.java new file mode 100644 index 0000000000..ef33baf2a4 --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Dino.java @@ -0,0 +1,7 @@ +package com.baeldung.instanceofalternative.visitorspattern; + +public interface Dino { + + String move(Visitor dinoMove); + +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/DinoVisitorImpl.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/DinoVisitorImpl.java new file mode 100644 index 0000000000..6fd71374fa --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/DinoVisitorImpl.java @@ -0,0 +1,15 @@ +package com.baeldung.instanceofalternative.visitorspattern; + +public class DinoVisitorImpl implements Visitor { + + @Override + public String visit(Anatotitan anatotitan) { + return anatotitan.run(); + } + + @Override + public String visit(Euraptor euraptor) { + return euraptor.flies(); + } + +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Euraptor.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Euraptor.java new file mode 100644 index 0000000000..fdce1e6c0b --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Euraptor.java @@ -0,0 +1,14 @@ +package com.baeldung.instanceofalternative.visitorspattern; + +public class Euraptor implements Dino { + + String flies() { + return "flying"; + } + + @Override + public String move(Visitor dinobehave) { + return dinobehave.visit(this); + } + +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Visitor.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Visitor.java new file mode 100644 index 0000000000..75fada3533 --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/instanceof_alternatives/visitorspattern/Visitor.java @@ -0,0 +1,9 @@ +package com.baeldung.instanceofalternative.visitorspattern; + +public interface Visitor { + + String visit(Anatotitan anatotitan); + + String visit(Euraptor euraptor); + +} diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/EnumUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/EnumUnitTest.java new file mode 100644 index 0000000000..73faa2a1ef --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/EnumUnitTest.java @@ -0,0 +1,26 @@ +package com.baeldung.instanceoftest; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.baeldung.instanceofalternative.enumallt.*; + +public class EnumUnitTest { + @Test + public void givenADinosaurSpecie_whenUsingEnum_thenGetMovementOfEuraptor() { + + assertEquals("flying", moveDinosaurUsingEnum(DinosaurEnum.Euraptor)); + } + + @Test + public void givenADinosaurSpecie_whenUsingEnum_thenGetMovementOfAnatotitan() { + assertEquals("running", moveDinosaurUsingEnum(DinosaurEnum.Anatotitan)); + } + + public static String moveDinosaurUsingEnum(DinosaurEnum dinosaurenum) { + return dinosaurenum.move(); + + } + +} \ No newline at end of file diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/ExampleSetupUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/ExampleSetupUnitTest.java new file mode 100644 index 0000000000..c42f77849b --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/ExampleSetupUnitTest.java @@ -0,0 +1,35 @@ +package com.baeldung.instanceoftest; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.baeldung.instanceofalternative.model.*; + +public class ExampleSetupUnitTest { + + @Test + public void givenADinosaurSpecie_whenUsingInstancof_thenGetMovementOfAnatotitan() { + + assertEquals("running", moveDinosaurUsingInstanceof(new Anatotitan())); + } + + @Test + public void givenADinosaurSpecie_whenUsingInstanceof_thenGetMovementOfEuraptor() { + assertEquals("flying", moveDinosaurUsingInstanceof(new Euraptor())); + } + + public static String moveDinosaurUsingInstanceof(Dinosaur dinosaur) { + + if (dinosaur instanceof Anatotitan) { + + Anatotitan anatotitan = (Anatotitan) dinosaur; + return anatotitan.run(); + } else if (dinosaur instanceof Euraptor) { + Euraptor euraptor = (Euraptor) dinosaur; + return euraptor.flies(); + } + return ""; + } + +} diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/GetClassUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/GetClassUnitTest.java new file mode 100644 index 0000000000..6a4886c8a3 --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/GetClassUnitTest.java @@ -0,0 +1,36 @@ +package com.baeldung.instanceoftest; + +import static org.junit.Assert.*; +import org.junit.Test; + +import com.baeldung.instanceofalternative.model.*; + +public class GetClassUnitTest { + + @Test + public void givenADinosaurSpecie_whenUsingGetClass_thenGetMovementOfAnatotitan() { + + assertEquals("running", moveDinosaurUsingGetClass(new Anatotitan())); + } + + @Test + public void givenADinosaurSpecie_whenUsingGetClass_thenGetMovementOfEuraptor() { + assertEquals("flying", moveDinosaurUsingGetClass(new Euraptor())); + } + + public static String moveDinosaurUsingGetClass(Dinosaur dinosaur) { + + if (dinosaur.getClass() + .equals(Anatotitan.class)) { + + Anatotitan anatotitan = (Anatotitan) dinosaur; + return anatotitan.run(); + } else if (dinosaur.getClass() + .equals(Euraptor.class)) { + Euraptor euraptor = (Euraptor) dinosaur; + return euraptor.flies(); + } + return ""; + } + +} diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/PolymorphismUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/PolymorphismUnitTest.java new file mode 100644 index 0000000000..960ed34c82 --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/PolymorphismUnitTest.java @@ -0,0 +1,27 @@ +package com.baeldung.instanceoftest; + +import static org.junit.Assert.*; + +import org.junit.BeforeClass; +import org.junit.Test; + +import com.baeldung.instanceofalternative.model.*; + +public class PolymorphismUnitTest { + + @Test + public void givenADinosaurSpecie_whenUsingPolymorphism_thenGetMovementOfAnatotitan() { + + assertEquals("running", moveDinosaurUsingPolymorphism(new Anatotitan())); + } + + @Test + public void givenADinosaurSpecie_whenUsingPolymorphism_thenGetMovementOfEuraptor() { + assertEquals("flying", moveDinosaurUsingPolymorphism(new Euraptor())); + } + + public static String moveDinosaurUsingPolymorphism(Dinosaur dinosaur) { + return dinosaur.move(); + } + +} diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/VisitorsPatternUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/VisitorsPatternUnitTest.java new file mode 100644 index 0000000000..287f7df798 --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/instanceof_alternative_test/VisitorsPatternUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.instanceoftest; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.baeldung.instanceofalternative.visitorspattern.*; + +public class VisitorsPatternUnitTest { + + @Test + public void givenADinosaurSpecie_whenUsingVisitorPattern_thenGetMovementOfAnatotitan() { + + assertEquals("running", moveDinosaurUsingVisitorPattern((Dino) new Anatotitan())); + } + + @Test + public void givenADinosaurSpecie_whenUsingVisitorPattern_thenGetMovementOfEuraptor() { + + assertEquals("flying", moveDinosaurUsingVisitorPattern((Dino) new Euraptor())); + } + + public static String moveDinosaurUsingVisitorPattern(Dino dinosaur) { + Visitor visitor = new DinoVisitorImpl(); + + return dinosaur.move(visitor); + } + +} diff --git a/core-java-modules/core-java-lang-operators-2/pom.xml b/core-java-modules/core-java-lang-operators-2/pom.xml index c36250f1ae..9d925c553a 100644 --- a/core-java-modules/core-java-lang-operators-2/pom.xml +++ b/core-java-modules/core-java-lang-operators-2/pom.xml @@ -38,4 +38,4 @@ - \ No newline at end of file +