BAEL-5979 Instanceof Alternatives (#13082)

* simple-thymeleaf-expression

* expression-update

* instanceof

* instanceof

* instanceof

* update

* Update pom.xml

* Update pom.xml

* instanceof

* update

* instanceofnew

* instanceofnew

* instanceofnew

* instanceofnew

* instanceofnew

* instanceofnew

* update

* pom

* Revert "pom"

This reverts commit c5075e6ba7e1e91159864d57bcbc18dd90314e16.

* Revert "Update pom.xml"

This reverts commit 7dd02df1aea201572fb64d8bc4a2990a94b6b4c0.

* Revert "Revert "Update pom.xml""

This reverts commit a02a1f67d16eb0bc781cc588af48f36fcc93d601.

* Update pom.xml

* Revert "Update pom.xml"

This reverts commit 7dd02df1aea201572fb64d8bc4a2990a94b6b4c0.

* Update pom.xml

* JAVA-8436: Remove AssertJ depenendency from the child modules - part 4

* Update pom.xml

* JAVA-8436: Remove AssertJ depenendency from the child modules - part 4

* BAEL-5979 Instanceof Alternatives
This commit is contained in:
Michael Olayemi 2022-12-13 02:56:43 +00:00 committed by GitHub
parent 9c8d1d127d
commit abd9f8a38b
16 changed files with 272 additions and 2 deletions

View File

@ -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();
}

View File

@ -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";
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.instanceofalternative.model;
public class Dinosaur {
public String move() {
return "default movement";
}
}

View File

@ -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";
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,7 @@
package com.baeldung.instanceofalternative.visitorspattern;
public interface Dino {
String move(Visitor dinoMove);
}

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.instanceofalternative.visitorspattern;
public interface Visitor {
String visit(Anatotitan anatotitan);
String visit(Euraptor euraptor);
}

View File

@ -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();
}
}

View File

@ -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 "";
}
}

View File

@ -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 "";
}
}

View File

@ -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();
}
}

View File

@ -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);
}
}