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 commitc5075e6ba7. * Revert "Update pom.xml" This reverts commit7dd02df1ae. * Revert "Revert "Update pom.xml"" This reverts commita02a1f67d1. * Update pom.xml * Revert "Update pom.xml" This reverts commit7dd02df1ae. * 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:
@@ -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();
|
||||
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.instanceofalternative.model;
|
||||
|
||||
public class Dinosaur {
|
||||
|
||||
public String move() {
|
||||
return "default movement";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.instanceofalternative.visitorspattern;
|
||||
|
||||
public interface Dino {
|
||||
|
||||
String move(Visitor dinoMove);
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.instanceofalternative.visitorspattern;
|
||||
|
||||
public interface Visitor {
|
||||
|
||||
String visit(Anatotitan anatotitan);
|
||||
|
||||
String visit(Euraptor euraptor);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user